oops
This commit is contained in:
@ -83,18 +83,14 @@ export default function SpotifyIcon({ profileUrl = "https://open.spotify.com" }:
|
|||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error parsing SSE data:', err);
|
console.error('Error parsing SSE data:', err);
|
||||||
// Fail silently
|
// Fail silently - will revert to static mode
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
eventSource.onerror = (event) => {
|
eventSource.onerror = (event) => {
|
||||||
console.error('Spotify SSE error:', event);
|
console.error('Spotify SSE error:', event);
|
||||||
|
// If SSE fails, we just fall back to static mode
|
||||||
// Fallback to polling if SSE fails
|
console.log('Spotify SSE failed, reverting to static mode');
|
||||||
if (eventSource.readyState === EventSource.CLOSED) {
|
|
||||||
console.log('SSE connection closed, falling back to polling');
|
|
||||||
startPolling();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Cleanup on unmount
|
// 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 = () => {
|
const getTooltipText = () => {
|
||||||
if (isDynamicEnabled.value && currentTrack.value && isPlaying.value) {
|
if (isDynamicEnabled.value && currentTrack.value && isPlaying.value) {
|
||||||
const artists = currentTrack.value.artists.map(artist => artist.name).join(', ');
|
const artists = currentTrack.value.artists.map(artist => artist.name).join(', ');
|
||||||
|
@ -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',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
Reference in New Issue
Block a user