From 5b1e13481bfc2d42635651fad1a40436ed2b2ec1 Mon Sep 17 00:00:00 2001 From: Atridad Lahiji Date: Tue, 3 Jun 2025 14:46:08 -0600 Subject: [PATCH] oops --- src/components/SpotifyIcon.tsx | 45 +----------- src/pages/api/spotify/current.ts | 117 ------------------------------- 2 files changed, 3 insertions(+), 159 deletions(-) delete mode 100644 src/pages/api/spotify/current.ts diff --git a/src/components/SpotifyIcon.tsx b/src/components/SpotifyIcon.tsx index 60b8db5..367b14e 100644 --- a/src/components/SpotifyIcon.tsx +++ b/src/components/SpotifyIcon.tsx @@ -83,18 +83,14 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: } } catch (err) { console.error('Error parsing SSE data:', err); - // Fail silently + // Fail silently - will revert to static mode } }; eventSource.onerror = (event) => { console.error('Spotify SSE error:', event); - - // Fallback to polling if SSE fails - if (eventSource.readyState === EventSource.CLOSED) { - console.log('SSE connection closed, falling back to polling'); - startPolling(); - } + // If SSE fails, we just fall back to static mode + console.log('Spotify SSE failed, reverting to static mode'); }; // Cleanup on unmount @@ -103,41 +99,6 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }: }; }; - // Fallback polling function if SSE fails - const startPolling = () => { - const fetchCurrentTrack = async () => { - try { - const response = await fetch('/api/spotify/current', { - credentials: 'include' - }); - - if (!response.ok) { - throw new Error('Failed to fetch Spotify data'); - } - - const data: SpotifyResponse = await response.json(); - - if (data.is_playing && data.item) { - currentTrack.value = data.item; - isPlaying.value = data.is_playing; - } else { - currentTrack.value = null; - isPlaying.value = false; - } - } catch (err) { - // Fail silently - } - }; - - // Fetch immediately - fetchCurrentTrack(); - - // Then fetch every 10 seconds (fallback polling) - const interval = setInterval(fetchCurrentTrack, 10000); - - return () => clearInterval(interval); - }; - const getTooltipText = () => { if (isDynamicEnabled.value && currentTrack.value && isPlaying.value) { const artists = currentTrack.value.artists.map(artist => artist.name).join(', '); diff --git a/src/pages/api/spotify/current.ts b/src/pages/api/spotify/current.ts deleted file mode 100644 index 98f2067..0000000 --- a/src/pages/api/spotify/current.ts +++ /dev/null @@ -1,117 +0,0 @@ -import type { APIRoute } from 'astro'; - -// Helper function to refresh the access token -async function refreshSpotifyToken(refreshToken: string, clientId: string, clientSecret: string) { - const response = await fetch('https://accounts.spotify.com/api/token', { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - 'Authorization': `Basic ${btoa(`${clientId}:${clientSecret}`)}`, - }, - body: new URLSearchParams({ - grant_type: 'refresh_token', - refresh_token: refreshToken, - }), - }); - - if (!response.ok) { - throw new Error('Failed to refresh token'); - } - - return await response.json(); -} - -export const GET: APIRoute = async ({ request }) => { - try { - const clientId = import.meta.env.SPOTIFY_CLIENT_ID; - const clientSecret = import.meta.env.SPOTIFY_CLIENT_SECRET; - let accessToken = import.meta.env.SPOTIFY_ACCESS_TOKEN; - const refreshToken = import.meta.env.SPOTIFY_REFRESH_TOKEN; - - if (!clientId || !clientSecret || !refreshToken) { - throw new Error('Missing Spotify credentials. Make sure SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, and SPOTIFY_REFRESH_TOKEN are set.'); - } - - // Try to fetch current track with existing token - let spotifyResponse = await fetch('https://api.spotify.com/v1/me/player/currently-playing', { - headers: { - 'Authorization': `Bearer ${accessToken}`, - 'Content-Type': 'application/json', - }, - }); - - // If token is expired (401), refresh it - if (spotifyResponse.status === 401) { - console.log('Access token expired, refreshing...'); - - try { - const tokenData = await refreshSpotifyToken(refreshToken, clientId, clientSecret); - accessToken = tokenData.access_token; - - // Note: In a real app, you'd want to save this new token somewhere - // For now, it will work until the next restart - console.log('Token refreshed successfully'); - - // Retry the request with new token - spotifyResponse = await fetch('https://api.spotify.com/v1/me/player/currently-playing', { - headers: { - 'Authorization': `Bearer ${accessToken}`, - 'Content-Type': 'application/json', - }, - }); - } catch (refreshError) { - console.error('Failed to refresh token:', refreshError); - throw new Error('Token refresh failed'); - } - } - - if (spotifyResponse.status === 204) { - // Nothing is currently playing - return new Response(JSON.stringify({ - is_playing: false, - item: null - }), { - status: 200, - headers: { - 'Content-Type': 'application/json', - }, - }); - } - - if (!spotifyResponse.ok) { - throw new Error(`Spotify API returned ${spotifyResponse.status}`); - } - - const data = await spotifyResponse.json(); - - return new Response(JSON.stringify({ - is_playing: data.is_playing, - item: data.item ? { - name: data.item.name, - artists: data.item.artists, - is_playing: data.is_playing, - external_urls: data.item.external_urls - } : null - }), { - status: 200, - headers: { - 'Content-Type': 'application/json', - }, - }); - - } catch (error) { - console.error('Spotify API Error:', error); - - // Return a fallback response - return new Response(JSON.stringify({ - is_playing: false, - item: null, - error: 'Unable to fetch Spotify data' - }), { - status: 200, // Return 200 to avoid client-side errors - headers: { - 'Content-Type': 'application/json', - }, - }); - } -}; \ No newline at end of file