// functions/playlist/[id].js class ServerAPI { constructor() { this.INSTANCES_URLS = [ 'https://tidal-uptime.jiffy-puffs-1j.workers.dev/', 'https://tidal-uptime.props-76styles.workers.dev/', ]; this.apiInstances = null; } async getInstances() { if (this.apiInstances) return this.apiInstances; let data = null; const urls = [...this.INSTANCES_URLS].sort(() => Math.random() - 0.5); for (const url of urls) { try { const response = await fetch(url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); data = await response.json(); break; } catch (error) { console.warn(`Failed to fetch from ${url}:`, error); } } if (data) { this.apiInstances = (data.api || []).map((item) => item.url || item); return this.apiInstances; } console.error('Failed to load instances from all uptime APIs'); return [ 'https://eu-central.monochrome.tf', 'https://us-west.monochrome.tf', 'https://arran.monochrome.tf', 'https://triton.squid.wtf', 'https://api.monochrome.tf', 'https://monochrome-api.samidy.com', 'https://maus.qqdl.site', 'https://vogel.qqdl.site', 'https://katze.qqdl.site', 'https://hund.qqdl.site', 'https://tidal.kinoplus.online', 'https://wolf.qqdl.site', ]; } async fetchWithRetry(relativePath) { const instances = await this.getInstances(); if (instances.length === 0) { throw new Error('No API instances configured.'); } let lastError = null; for (const baseUrl of instances) { const url = baseUrl.endsWith('/') ? `${baseUrl}${relativePath.substring(1)}` : `${baseUrl}${relativePath}`; try { const response = await fetch(url); if (response.ok) { return response; } lastError = new Error(`Request failed with status ${response.status}`); } catch (error) { lastError = error; } } throw lastError || new Error(`All API instances failed for: ${relativePath}`); } async getPlaylistMetadata(id) { try { const response = await this.fetchWithRetry(`/playlist/${id}`); return await response.json(); } catch { // Fallback to query param style const response = await this.fetchWithRetry(`/playlist?id=${id}`); return await response.json(); } } getCoverUrl(id, size = '1080') { if (!id) return ''; const formattedId = String(id).replace(/-/g, '/'); return `https://resources.tidal.com/images/${formattedId}/${size}x${size}.jpg`; } } export async function onRequest(context) { const { request, params, env } = context; const userAgent = request.headers.get('User-Agent') || ''; const isBot = /discordbot|twitterbot|facebookexternalhit|bingbot|googlebot|slurp|whatsapp|pinterest|slackbot/i.test( userAgent ); const playlistId = params.id; if (isBot && playlistId) { try { const api = new ServerAPI(); const data = await api.getPlaylistMetadata(playlistId); const playlist = data.playlist || data.data || data; if (playlist && (playlist.title || playlist.name)) { const title = playlist.title || playlist.name; const trackCount = playlist.numberOfTracks; const description = `Playlist • ${trackCount} Tracks\nListen on Monochrome`; const imageId = playlist.squareImage || playlist.image; const imageUrl = imageId ? api.getCoverUrl(imageId, '1080') : 'https://monochrome.samidy.com/assets/appicon.png'; const pageUrl = new URL(request.url).href; const metaHtml = `
${description}