Fix playlist bug

This commit is contained in:
2025-03-01 16:39:13 +01:00
parent a1193ea87f
commit 195980032c
5 changed files with 80 additions and 35 deletions

View File

@ -12,23 +12,30 @@ const PLAYLISTS = {
const API_KEY = process.env.YOUTUBE_API_KEY;
let cachedSongs = null;
let lastFetchTime = 0;
const cachedSongsByPlaylist = {};
const CACHE_TTL = 3600000; // 1 hour
/**
* Fetches songs from YouTube playlist and returns them
*/
const fetchPlaylistSongs = async () => {
const fetchPlaylistSongs = async (playlistId = null) => {
if (!playlistId) {
console.warn("No playlist ID provided, using default");
playlistId = PLAYLISTS.seventies; // default fallback
}
const now = Date.now();
if (cachedSongs && cachedSongs.length > 0 && (now - lastFetchTime) < CACHE_TTL) {
console.log("Returning cached YouTube songs:", cachedSongs.length);
return cachedSongs;
if (cachedSongsByPlaylist[playlistId] &&
cachedSongsByPlaylist[playlistId].songs.length > 0 &&
(now - cachedSongsByPlaylist[playlistId].timestamp) < CACHE_TTL) {
console.log(`Using cached songs for playlist ${playlistId}`);
return cachedSongsByPlaylist[playlistId].songs;
}
try {
console.log("Fetching songs from YouTube API...");
const playlistUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=50&playlistId=${PLAYLIST_ID}&key=${YOUTUBE_API_KEY}`;
console.log(`Fetching fresh songs from YouTube API for playlist ${playlistId}...`);
const playlistUrl = `https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults=50&playlistId=${playlistId}&key=${YOUTUBE_API_KEY}`;
const response = await fetch(playlistUrl);
const data = await response.json();
@ -38,7 +45,6 @@ const fetchPlaylistSongs = async () => {
}
if (!data.items || !data.items.length) {
console.error("No items found in YouTube playlist");
throw new Error("No songs found in the playlist");
}
@ -47,35 +53,24 @@ const fetchPlaylistSongs = async () => {
youtubeId: item.contentDetails.videoId,
title: item.snippet.title,
artist: item.snippet.videoOwnerChannelTitle || "Unknown Artist",
coverUrl: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url ||
"https://place-hold.it/500x500/333/fff?text=No%20Thumbnail",
coverUrl: item.snippet.thumbnails.high?.url || item.snippet.thumbnails.default?.url,
playlistId: playlistId
}));
cachedSongs = songs;
lastFetchTime = now;
console.log("Fetched and cached YouTube songs:", songs.length);
cachedSongsByPlaylist[playlistId] = {
songs,
timestamp: now
};
return songs;
} catch (error) {
console.error("Error fetching YouTube playlist:", error);
if (cachedSongs && cachedSongs.length > 0) {
console.log("Using last cached songs due to API error");
return cachedSongs;
}
return [{
id: 1,
title: "Could not load songs",
artist: "API Error",
coverUrl: "https://place-hold.it/500x500/f00/fff?text=Error",
youtubeId: "dQw4w9WgXcQ"
}];
console.error(`Error fetching YouTube playlist ${playlistId}:`, error);
return cachedSongsByPlaylist[playlistId]?.songs || getDefaultSongs();
}
};
const getAvailableSongIds = async () => {
const songs = await fetchPlaylistSongs();
const getAvailableSongIds = async (playlistId) => {
const songs = await fetchPlaylistSongs(playlistId);
return songs.map(song => song.id);
};