From 881f13cb48ed01ad0463b3e1faf8a58627c2b049 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 11 Jan 2026 18:36:54 +0100 Subject: [PATCH] Consolidate API - remove v1/v2 duplication, v2 is now main --- mod.ts | 712 ++++++++++++--------------------------------------------- ui.ts | 162 +++---------- 2 files changed, 178 insertions(+), 696 deletions(-) diff --git a/mod.ts b/mod.ts index 189c3a4..f80da5b 100644 --- a/mod.ts +++ b/mod.ts @@ -1,6 +1,6 @@ /** * Virome API for Deno - * A consolidated YouTube Music, YouTube Search, JioSaavn, and Last.fm API + * YouTube Music, YouTube Search, and Last.fm API * * Run with: deno run --allow-net --allow-env --allow-read mod.ts * Or deploy to Deno Deploy @@ -12,18 +12,15 @@ import { html as uiHtml } from "./ui.ts"; const PORT = parseInt(Deno.env.get("PORT") || "8000"); -// Initialize clients const ytmusic = new YTMusic(); const youtubeSearch = new YouTubeSearch(); -// CORS headers const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization", }; -// Helper functions function json(data: unknown, status = 200): Response { return new Response(JSON.stringify(data), { status, @@ -35,13 +32,10 @@ function error(message: string, status = 400): Response { return json({ error: message }, status); } -// URL pattern matching function matchRoute(pathname: string, pattern: string): Record | null { const patternParts = pattern.split("/"); const pathParts = pathname.split("/"); - if (patternParts.length !== pathParts.length) return null; - const params: Record = {}; for (let i = 0; i < patternParts.length; i++) { if (patternParts[i].startsWith(":")) { @@ -53,584 +47,269 @@ function matchRoute(pathname: string, pattern: string): Record | return params; } -// Country code to language mapping const countryLanguageMap: Record = { TN: "ar", DZ: "ar", MA: "ar", EG: "ar", SA: "ar", AE: "ar", KW: "ar", QA: "ar", BH: "ar", OM: "ar", JO: "ar", LB: "ar", IQ: "ar", LY: "ar", SD: "ar", YE: "ar", SY: "ar", PS: "ar", FR: "fr", BE: "fr", CH: "fr", CA: "fr", SN: "fr", CI: "fr", ML: "fr", BF: "fr", NE: "fr", TG: "fr", BJ: "fr", CM: "fr", MG: "fr", - DE: "de", AT: "de", - ES: "es", MX: "es", AR: "es", CO: "es", PE: "es", VE: "es", CL: "es", EC: "es", GT: "es", CU: "es", BO: "es", DO: "es", HN: "es", PY: "es", SV: "es", NI: "es", CR: "es", PA: "es", UY: "es", - PT: "pt", BR: "pt", AO: "pt", MZ: "pt", - IT: "it", - NL: "nl", - RU: "ru", BY: "ru", KZ: "ru", - TR: "tr", - JP: "ja", - KR: "ko", - CN: "zh", TW: "zh", HK: "zh", - IN: "hi", - TH: "th", - VN: "vi", - ID: "id", - PL: "pl", - UA: "uk", - RO: "ro", - GR: "el", - CZ: "cs", - SE: "sv", - NO: "no", - DK: "da", - FI: "fi", - HU: "hu", - IL: "he", - IR: "fa", - PK: "ur", - BD: "bn", - PH: "tl", - MY: "ms", + DE: "de", AT: "de", ES: "es", MX: "es", AR: "es", CO: "es", PE: "es", VE: "es", CL: "es", EC: "es", GT: "es", CU: "es", BO: "es", DO: "es", HN: "es", PY: "es", SV: "es", NI: "es", CR: "es", PA: "es", UY: "es", + PT: "pt", BR: "pt", AO: "pt", MZ: "pt", IT: "it", NL: "nl", RU: "ru", BY: "ru", KZ: "ru", TR: "tr", JP: "ja", KR: "ko", CN: "zh", TW: "zh", HK: "zh", IN: "hi", TH: "th", VN: "vi", ID: "id", PL: "pl", UA: "uk", RO: "ro", GR: "el", CZ: "cs", SE: "sv", NO: "no", DK: "da", FI: "fi", HU: "hu", IL: "he", IR: "fa", PK: "ur", BD: "bn", PH: "tl", MY: "ms", }; -// Detect region from IP using Cloudflare/Deno Deploy headers or fallback to IP lookup async function detectRegionFromIP(req: Request): Promise<{ country: string; language: string } | null> { try { - // Try Cloudflare/Deno Deploy headers first (fastest) const cfCountry = req.headers.get("cf-ipcountry") || req.headers.get("x-country"); if (cfCountry && cfCountry !== "XX") { - const language = countryLanguageMap[cfCountry] || "en"; - return { country: cfCountry, language }; + return { country: cfCountry, language: countryLanguageMap[cfCountry] || "en" }; } - - // Get client IP const forwardedFor = req.headers.get("x-forwarded-for"); const clientIP = forwardedFor ? forwardedFor.split(",")[0].trim() : null; - - if (!clientIP || clientIP === "127.0.0.1" || clientIP.startsWith("192.168.") || clientIP.startsWith("10.")) { - return null; - } - - // Fallback to IP geolocation API (ip-api.com is free, no key needed) + if (!clientIP || clientIP === "127.0.0.1" || clientIP.startsWith("192.168.") || clientIP.startsWith("10.")) return null; const geoResponse = await fetch(`http://ip-api.com/json/${clientIP}?fields=countryCode`); if (geoResponse.ok) { const geoData = await geoResponse.json(); - if (geoData.countryCode) { - const language = countryLanguageMap[geoData.countryCode] || "en"; - return { country: geoData.countryCode, language }; - } + if (geoData.countryCode) return { country: geoData.countryCode, language: countryLanguageMap[geoData.countryCode] || "en" }; } - return null; - } catch { - return null; - } + } catch { return null; } } -// Main request handler async function handler(req: Request): Promise { const url = new URL(req.url); const { pathname, searchParams } = url; - // Handle CORS preflight - if (req.method === "OPTIONS") { - return new Response(null, { headers: corsHeaders }); - } + if (req.method === "OPTIONS") return new Response(null, { headers: corsHeaders }); try { - // Root - API Documentation UI - if (pathname === "/") { - return new Response(uiHtml, { headers: { "Content-Type": "text/html", ...corsHeaders } }); - } + // Root - UI + if (pathname === "/") return new Response(uiHtml, { headers: { "Content-Type": "text/html", ...corsHeaders } }); - // Serve logo from assets + // Logo if (pathname === "/assets/logo.png" || pathname === "/assets/Logo.png") { try { const logoPath = new URL("./assets/Logo.png", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1"); const logo = await Deno.readFile(logoPath); - return new Response(logo, { - headers: { "Content-Type": "image/png", ...corsHeaders } - }); - } catch { - return new Response("Logo not found", { status: 404 }); - } + return new Response(logo, { headers: { "Content-Type": "image/png", ...corsHeaders } }); + } catch { return new Response("Logo not found", { status: 404 }); } } - // Favicon - if (pathname === "/favicon.ico") { - return new Response(null, { status: 204 }); - } + if (pathname === "/favicon.ico") return new Response(null, { status: 204 }); + if (pathname === "/health") return json({ status: "ok" }); - // Health check - if (pathname === "/health") { - return json({ status: "ok" }); - } - - // ============ SEARCH ENDPOINTS ============ + // ============ SEARCH ============ - // YouTube Music Search with YouTube fallback video IDs if (pathname === "/api/search") { const query = searchParams.get("q"); const filter = searchParams.get("filter") || undefined; const continuationToken = searchParams.get("continuationToken") || undefined; const ignoreSpelling = searchParams.get("ignore_spelling") === "true"; - const withFallback = searchParams.get("fallback") !== "0"; // Enable by default + const withFallback = searchParams.get("fallback") !== "0"; - // Get region from param or detect from IP let region = searchParams.get("region") || searchParams.get("gl") || undefined; let language = searchParams.get("language") || searchParams.get("hl") || undefined; - // Auto-detect region from IP if not provided if (!region) { - const detectedRegion = await detectRegionFromIP(req); - if (detectedRegion) { - region = detectedRegion.country; - if (!language) language = detectedRegion.language; - } + const detected = await detectRegionFromIP(req); + if (detected) { region = detected.country; if (!language) language = detected.language; } } - if (!query && !continuationToken) { - return error("Missing required query parameter 'q' or 'continuationToken'"); - } + if (!query && !continuationToken) return error("Missing 'q' or 'continuationToken'"); const results = await ytmusic.search(query || "", filter, continuationToken, ignoreSpelling, region, language); - // For songs, add fallback YouTube video IDs (embeddable versions) + // Add fallback YouTube IDs for songs if (withFallback && filter === "songs" && results.results?.length > 0) { - // Get YouTube video alternatives for the top results - const enhancedResults = await Promise.all( + const enhanced = await Promise.all( results.results.slice(0, 10).map(async (song: any) => { try { - // Search YouTube for official video - const searchQuery = `${song.title} ${song.artists?.[0]?.name || ''} official`; - const ytResults = await youtubeSearch.searchVideos(searchQuery); - - // Find a non-Topic channel video - const alternative = ytResults.results?.find((v: any) => - v.channel?.name && !v.channel.name.includes('Topic') && v.id - ); - - if (alternative) { - return { - ...song, - fallbackVideoId: alternative.id, - fallbackTitle: alternative.title, - }; - } - } catch { - // Ignore errors, just return original - } + const ytResults = await youtubeSearch.searchVideos(`${song.title} ${song.artists?.[0]?.name || ''} official`); + const alt = ytResults.results?.find((v: any) => v.channel?.name && !v.channel.name.includes('Topic') && v.id); + if (alt) return { ...song, fallbackVideoId: alt.id, fallbackTitle: alt.title }; + } catch {} return song; }) ); - - // Replace first 10 results with enhanced ones, keep the rest - results.results = [ - ...enhancedResults, - ...results.results.slice(10) - ]; + results.results = [...enhanced, ...results.results.slice(10)]; } return json({ query, filter, region, language, ...results }); } - // Search suggestions if (pathname === "/api/search/suggestions") { const query = searchParams.get("q"); + if (!query) return error("Missing 'q'"); const music = searchParams.get("music"); - - if (!query) return error("Missing required query parameter 'q'"); - - if (music === "1") { - const suggestions = await ytmusic.getSearchSuggestions(query); - return json({ suggestions, source: "youtube_music" }); - } else { - const suggestions = await youtubeSearch.getSuggestions(query); - return json({ suggestions, source: "youtube" }); - } + const suggestions = music === "1" ? await ytmusic.getSearchSuggestions(query) : await youtubeSearch.getSuggestions(query); + return json({ suggestions, source: music === "1" ? "youtube_music" : "youtube" }); } - // YouTube Search if (pathname === "/api/yt_search") { const query = searchParams.get("q"); const filter = searchParams.get("filter") || "all"; const continuationToken = searchParams.get("continuationToken") || undefined; - if (!query && !continuationToken) { - return error("Missing required query parameter 'q' or 'continuationToken'"); - } + if (!query && !continuationToken) return error("Missing 'q' or 'continuationToken'"); const results: unknown[] = []; let nextToken: string | null = null; if (continuationToken) { - if (filter === "videos") { - const r = await youtubeSearch.searchVideos(null, continuationToken); - results.push(...r.results); - nextToken = r.continuationToken; - } else if (filter === "channels") { - const r = await youtubeSearch.searchChannels(null, continuationToken); - results.push(...r.results); - nextToken = r.continuationToken; - } else if (filter === "playlists") { - const r = await youtubeSearch.searchPlaylists(null, continuationToken); - results.push(...r.results); - nextToken = r.continuationToken; - } + if (filter === "videos") { const r = await youtubeSearch.searchVideos(null, continuationToken); results.push(...r.results); nextToken = r.continuationToken; } + else if (filter === "channels") { const r = await youtubeSearch.searchChannels(null, continuationToken); results.push(...r.results); nextToken = r.continuationToken; } + else if (filter === "playlists") { const r = await youtubeSearch.searchPlaylists(null, continuationToken); results.push(...r.results); nextToken = r.continuationToken; } } else if (query) { - if (filter === "videos" || filter === "all") { - const r = await youtubeSearch.searchVideos(query); - results.push(...r.results); - nextToken = r.continuationToken; - } - if (filter === "channels" || filter === "all") { - const r = await youtubeSearch.searchChannels(query); - results.push(...r.results); - if (!nextToken) nextToken = r.continuationToken; - } - if (filter === "playlists" || filter === "all") { - const r = await youtubeSearch.searchPlaylists(query); - results.push(...r.results); - if (!nextToken) nextToken = r.continuationToken; - } + if (filter === "videos" || filter === "all") { const r = await youtubeSearch.searchVideos(query); results.push(...r.results); nextToken = r.continuationToken; } + if (filter === "channels" || filter === "all") { const r = await youtubeSearch.searchChannels(query); results.push(...r.results); if (!nextToken) nextToken = r.continuationToken; } + if (filter === "playlists" || filter === "all") { const r = await youtubeSearch.searchPlaylists(query); results.push(...r.results); if (!nextToken) nextToken = r.continuationToken; } } return json({ filter, query, results, continuationToken: nextToken }); } - // ============ ENTITY ENDPOINTS ============ + // ============ ENTITIES (Complete data with links) ============ - // Get song details let params = matchRoute(pathname, "/api/songs/:videoId"); - if (params) { - const data = await ytmusic.getSong(params.videoId); - return json(data); - } + if (params) return json(await getSongComplete(params.videoId, ytmusic)); - // Get album details params = matchRoute(pathname, "/api/albums/:browseId"); - if (params) { - const data = await ytmusic.getAlbum(params.browseId); - return json(data); - } + if (params) return json(await getAlbumComplete(params.browseId, ytmusic)); params = matchRoute(pathname, "/api/album/:id"); - if (params) { - const data = await ytmusic.getAlbum(params.id); - return json(data); - } + if (params) return json(await getAlbumComplete(params.id, ytmusic)); - // Get artist details (skip if it's /api/artist/info) params = matchRoute(pathname, "/api/artists/:browseId"); - if (params) { - const data = await ytmusic.getArtist(params.browseId); - return json(data); - } + if (params) return json(await getArtistComplete(params.browseId, ytmusic)); - // Skip /api/artist/info - handled later by Last.fm endpoint if (pathname !== "/api/artist/info") { params = matchRoute(pathname, "/api/artist/:artistId"); if (params) { const country = searchParams.get("country") || "US"; - const data = await ytmusic.getArtistSummary(params.artistId, country); - return json(data); + return json(await ytmusic.getArtistSummary(params.artistId, country)); } } - // Get playlist details params = matchRoute(pathname, "/api/playlists/:playlistId"); - if (params) { - const data = await ytmusic.getPlaylist(params.playlistId); - return json(data); - } + if (params) return json(await ytmusic.getPlaylist(params.playlistId)); params = matchRoute(pathname, "/api/playlist/:id"); - if (params) { - const data = await ytmusic.getPlaylist(params.id); - return json(data); - } + if (params) return json(await ytmusic.getPlaylist(params.id)); - // Get related videos params = matchRoute(pathname, "/api/related/:id"); - if (params) { - const data = await ytmusic.getRelated(params.id); - return json({ success: true, data }); - } + if (params) return json({ success: true, data: await ytmusic.getRelated(params.id) }); - // ============ EXPLORE ENDPOINTS ============ + // Full chain: song -> artist -> albums + params = matchRoute(pathname, "/api/chain/:videoId"); + if (params) return json(await getFullChain(params.videoId, ytmusic)); - // Charts - if (pathname === "/api/charts") { - const country = searchParams.get("country") || undefined; - const data = await ytmusic.getCharts(country); - return json(data); - } + // ============ EXPLORE ============ - // Moods - if (pathname === "/api/moods") { - const data = await ytmusic.getMoodCategories(); - return json(data); - } + if (pathname === "/api/charts") return json(await ytmusic.getCharts(searchParams.get("country") || undefined)); + + if (pathname === "/api/moods") return json(await ytmusic.getMoodCategories()); params = matchRoute(pathname, "/api/moods/:categoryId"); - if (params) { - const data = await ytmusic.getMoodPlaylists(params.categoryId); - return json(data); - } + if (params) return json(await ytmusic.getMoodPlaylists(params.categoryId)); - // Watch playlist if (pathname === "/api/watch_playlist") { const videoId = searchParams.get("videoId") || undefined; const playlistId = searchParams.get("playlistId") || undefined; - const radio = searchParams.get("radio") === "true"; - const shuffle = searchParams.get("shuffle") === "true"; - const limit = parseInt(searchParams.get("limit") || "25"); - - if (!videoId && !playlistId) { - return error("Provide either videoId or playlistId"); - } - - const data = await ytmusic.getWatchPlaylist(videoId, playlistId, radio, shuffle, limit); - return json(data); + if (!videoId && !playlistId) return error("Provide videoId or playlistId"); + return json(await ytmusic.getWatchPlaylist(videoId, playlistId, searchParams.get("radio") === "true", searchParams.get("shuffle") === "true", parseInt(searchParams.get("limit") || "25"))); } - // ============ STREAMING ENDPOINTS ============ + // ============ STREAMING ============ - // Find song if (pathname === "/api/music/find") { - const name = searchParams.get("name"); - const artist = searchParams.get("artist"); + const name = searchParams.get("name"), artist = searchParams.get("artist"); + if (!name || !artist) return error("Missing name and artist"); - if (!name || !artist) { - return error("Missing required parameters: name and artist are required"); - } - - const query = `${name} ${artist}`; - const searchResults = await ytmusic.search(query, "songs"); - - if (!searchResults.results?.length) { - return json({ success: false, error: "Song not found" }, 404); - } + const searchResults = await ytmusic.search(`${name} ${artist}`, "songs"); + if (!searchResults.results?.length) return json({ success: false, error: "Song not found" }, 404); const normalize = (s: string) => s.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").replace(/[^a-z0-9]+/gi, "").toLowerCase(); const nName = normalize(name); const artistsList = artist.split(",").map(a => normalize(a)); - const bestMatch = searchResults.results.find((song: any) => { + const match = searchResults.results.find((song: any) => { const nSongName = normalize(song.title || ""); const songArtists = (song.artists || []).map((a: any) => normalize(a.name || "")); - const titleMatch = nSongName.includes(nName) || nName.includes(nSongName); - const artistMatch = artistsList.some(a => songArtists.some((sa: string) => sa.includes(a) || a.includes(sa))); - return titleMatch && artistMatch; + return (nSongName.includes(nName) || nName.includes(nSongName)) && artistsList.some(a => songArtists.some((sa: string) => sa.includes(a) || a.includes(sa))); }); - if (bestMatch) { - return json({ success: true, data: bestMatch }); - } - return json({ success: false, error: "Song not found after filtering" }, 404); + return match ? json({ success: true, data: match }) : json({ success: false, error: "Song not found" }, 404); } - // Stream endpoint - multi-source if (pathname === "/api/stream") { const id = searchParams.get("id"); + if (!id) return error("Missing id"); - if (!id) { - return error("Missing required parameter: id"); - } + const piped = await fetchFromPiped(id); + if (piped.success) return json({ success: true, service: "piped", instance: piped.instance, streamingUrls: piped.streamingUrls, metadata: piped.metadata, requestedId: id, timestamp: new Date().toISOString() }); - // Try Piped first - const pipedResult = await fetchFromPiped(id); - if (pipedResult.success) { - return json({ - success: true, - service: "piped", - instance: pipedResult.instance, - streamingUrls: pipedResult.streamingUrls, - metadata: pipedResult.metadata, - requestedId: id, - timestamp: new Date().toISOString(), - }); - } + const invidious = await fetchFromInvidious(id); + if (invidious.success) return json({ success: true, service: "invidious", instance: invidious.instance, streamingUrls: invidious.streamingUrls, metadata: invidious.metadata, requestedId: id, timestamp: new Date().toISOString() }); - // Try Invidious - const invidiousResult = await fetchFromInvidious(id); - if (invidiousResult.success) { - return json({ - success: true, - service: "invidious", - instance: invidiousResult.instance, - streamingUrls: invidiousResult.streamingUrls, - metadata: invidiousResult.metadata, - requestedId: id, - timestamp: new Date().toISOString(), - }); - } - - return json({ success: false, error: "No streaming data found from any source" }, 404); + return json({ success: false, error: "No streaming data found" }, 404); } - // ============ SIMILAR TRACKS ============ - - if (pathname === "/api/similar") { - const title = searchParams.get("title"); - const artist = searchParams.get("artist"); - const limit = searchParams.get("limit") || "5"; - - if (!title || !artist) { - return error("Missing title or artist parameter"); - } - - const similarTracks = await LastFM.getSimilarTracks(title, artist, limit); - if ("error" in similarTracks) { - return json({ error: similarTracks.error }, 500); - } - - const ytResults = await Promise.all( - similarTracks.map(async (t: any) => { - const r = await youtubeSearch.searchVideos(`${t.title} ${t.artist}`); - return r.results[0] || null; - }) - ); - - return json(ytResults.filter(Boolean)); - } - - // ============ FEED ENDPOINTS ============ - - // Audio proxy to bypass CORS if (pathname === "/api/proxy") { const audioUrl = searchParams.get("url"); - if (!audioUrl) { - return error("Missing url parameter"); - } + if (!audioUrl) return error("Missing url"); return proxyAudio(audioUrl, req); } + // ============ LYRICS & INFO ============ + + if (pathname === "/api/lyrics") { + const title = searchParams.get("title"), artist = searchParams.get("artist"); + if (!title || !artist) return error("Missing title and artist"); + return json(await getLyrics(title, artist, searchParams.get("duration") ? parseInt(searchParams.get("duration")!) : undefined)); + } + + if (pathname === "/api/similar") { + const title = searchParams.get("title"), artist = searchParams.get("artist"); + if (!title || !artist) return error("Missing title or artist"); + const similar = await LastFM.getSimilarTracks(title, artist, searchParams.get("limit") || "5"); + if ("error" in similar) return json({ error: similar.error }, 500); + const ytResults = await Promise.all(similar.map(async (t: any) => { const r = await youtubeSearch.searchVideos(`${t.title} ${t.artist}`); return r.results[0] || null; })); + return json(ytResults.filter(Boolean)); + } + + if (pathname === "/api/trending") return json(await getTrendingMusic(searchParams.get("country") || "United States", ytmusic)); + + if (pathname === "/api/radio") { + const videoId = searchParams.get("videoId"); + if (!videoId) return error("Missing videoId"); + return json(await getRadio(videoId, ytmusic)); + } + + if (pathname === "/api/top/artists") return json(await getTopArtists(searchParams.get("country") || undefined, parseInt(searchParams.get("limit") || "20"), ytmusic)); + if (pathname === "/api/top/tracks") return json(await getTopTracks(searchParams.get("country") || undefined, parseInt(searchParams.get("limit") || "20"), ytmusic)); + + if (pathname === "/api/artist/info") { + const artist = searchParams.get("artist"); + if (!artist) return error("Missing artist"); + return json(await getArtistInfo(artist)); + } + + if (pathname === "/api/track/info") { + const title = searchParams.get("title"), artist = searchParams.get("artist"); + if (!title || !artist) return error("Missing title and artist"); + return json(await getTrackInfo(title, artist)); + } + + // ============ FEED ============ + if (pathname === "/api/feed/unauthenticated" || pathname.startsWith("/api/feed/channels=")) { let channelsParam = searchParams.get("channels"); - - if (pathname.startsWith("/api/feed/channels=")) { - channelsParam = pathname.replace("/api/feed/channels=", "").split("?")[0]; - } - - if (!channelsParam) { - return error("No valid channel IDs provided"); - } + if (pathname.startsWith("/api/feed/channels=")) channelsParam = pathname.replace("/api/feed/channels=", "").split("?")[0]; + if (!channelsParam) return error("No channel IDs provided"); const channelIds = channelsParam.split(",").map(s => s.trim()).filter(Boolean); const preview = searchParams.get("preview") === "1"; - const results: any[] = []; - for (const channelId of channelIds) { - const items = await fetchChannelVideos(channelId, preview ? 5 : undefined); - results.push(...items); - } - - // Filter shorts and sort by upload date - const filtered = results - .filter(item => !item.isShort) - .sort((a, b) => Number(b.uploaded) - Number(a.uploaded)); - - return json(filtered); + for (const channelId of channelIds) results.push(...await fetchChannelVideos(channelId, preview ? 5 : undefined)); + return json(results.filter(item => !item.isShort).sort((a, b) => Number(b.uploaded) - Number(a.uploaded))); } - // ============ NEW FEATURES ============ - - // Lyrics - if (pathname === "/api/lyrics") { - const title = searchParams.get("title"); - const artist = searchParams.get("artist"); - const duration = searchParams.get("duration"); - - if (!title || !artist) { - return error("Missing required parameters: title and artist"); - } - - const result = await getLyrics(title, artist, duration ? parseInt(duration) : undefined); - return json(result); - } - - // Trending Music - if (pathname === "/api/trending") { - const country = searchParams.get("country") || "United States"; - const result = await getTrendingMusic(country, ytmusic); - return json(result); - } - - // Radio (infinite mix based on a song) - if (pathname === "/api/radio") { - const videoId = searchParams.get("videoId"); - if (!videoId) { - return error("Missing required parameter: videoId"); - } - const result = await getRadio(videoId, ytmusic); - return json(result); - } - - // Top Artists (by country using YouTube Music search) - if (pathname === "/api/top/artists") { - const country = searchParams.get("country") || undefined; - const limit = parseInt(searchParams.get("limit") || "20"); - const result = await getTopArtists(country, limit, ytmusic); - return json(result); - } - - // Top Tracks (by country using YouTube Music search) - if (pathname === "/api/top/tracks") { - const country = searchParams.get("country") || undefined; - const limit = parseInt(searchParams.get("limit") || "20"); - const result = await getTopTracks(country, limit, ytmusic); - return json(result); - } - - // Artist Info (detailed from Last.fm) - if (pathname === "/api/artist/info") { - const artist = searchParams.get("artist"); - if (!artist) { - return error("Missing required parameter: artist"); - } - const result = await getArtistInfo(artist); - return json(result); - } - - // Track Info (detailed from Last.fm) - if (pathname === "/api/track/info") { - const title = searchParams.get("title"); - const artist = searchParams.get("artist"); - if (!title || !artist) { - return error("Missing required parameters: title and artist"); - } - const result = await getTrackInfo(title, artist); - return json(result); - } - - // ============ UNIFIED ENTITY ENDPOINTS ============ - - // Get complete song with artist/album links - params = matchRoute(pathname, "/api/v2/songs/:videoId"); - if (params) { - const data = await getSongComplete(params.videoId, ytmusic); - return json(data); - } - - // Get complete album with artist link and tracks - params = matchRoute(pathname, "/api/v2/albums/:browseId"); - if (params) { - const data = await getAlbumComplete(params.browseId, ytmusic); - return json(data); - } - - // Get complete artist with discography - params = matchRoute(pathname, "/api/v2/artists/:browseId"); - if (params) { - const data = await getArtistComplete(params.browseId, ytmusic); - return json(data); - } - - // Get full chain: song -> artist -> albums (navigation helper) - params = matchRoute(pathname, "/api/v2/chain/:videoId"); - if (params) { - const data = await getFullChain(params.videoId, ytmusic); - return json(data); - } - - // 404 return json({ error: "Route not found", path: pathname }, 404); } catch (err) { @@ -639,32 +318,17 @@ async function handler(req: Request): Promise { } } -// Audio proxy endpoint to bypass CORS with range request support async function proxyAudio(url: string, req: Request): Promise { try { const headers: Record = { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36", - "Accept": "*/*", - "Accept-Language": "en-US,en;q=0.9", - "Referer": "https://www.youtube.com/", - "Origin": "https://www.youtube.com", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36", + "Accept": "*/*", "Referer": "https://www.youtube.com/", "Origin": "https://www.youtube.com", }; - - // Forward range header for seeking support const rangeHeader = req.headers.get("Range"); - if (rangeHeader) { - headers["Range"] = rangeHeader; - } + if (rangeHeader) headers["Range"] = rangeHeader; const response = await fetch(url, { headers }); - - if (!response.ok && response.status !== 206) { - console.error(`[Proxy] Upstream error: ${response.status} ${response.statusText}`); - return new Response(`Failed to fetch audio: ${response.status}`, { - status: 502, - headers: corsHeaders - }); - } + if (!response.ok && response.status !== 206) return new Response(`Failed: ${response.status}`, { status: 502, headers: corsHeaders }); const responseHeaders = new Headers(); responseHeaders.set("Access-Control-Allow-Origin", "*"); @@ -672,139 +336,59 @@ async function proxyAudio(url: string, req: Request): Promise { responseHeaders.set("Access-Control-Allow-Headers", "Range, Content-Type"); responseHeaders.set("Access-Control-Expose-Headers", "Content-Length, Content-Range, Accept-Ranges"); responseHeaders.set("Cache-Control", "public, max-age=3600"); + responseHeaders.set("Content-Type", response.headers.get("Content-Type") || "audio/mp4"); + if (response.headers.get("Content-Length")) responseHeaders.set("Content-Length", response.headers.get("Content-Length")!); + if (response.headers.get("Content-Range")) responseHeaders.set("Content-Range", response.headers.get("Content-Range")!); + responseHeaders.set("Accept-Ranges", response.headers.get("Accept-Ranges") || "bytes"); - // Copy important headers from upstream - const contentType = response.headers.get("Content-Type"); - if (contentType) { - responseHeaders.set("Content-Type", contentType); - } else { - responseHeaders.set("Content-Type", "audio/mp4"); - } - - const contentLength = response.headers.get("Content-Length"); - if (contentLength) { - responseHeaders.set("Content-Length", contentLength); - } - - const contentRange = response.headers.get("Content-Range"); - if (contentRange) { - responseHeaders.set("Content-Range", contentRange); - } - - const acceptRanges = response.headers.get("Accept-Ranges"); - if (acceptRanges) { - responseHeaders.set("Accept-Ranges", acceptRanges); - } else { - responseHeaders.set("Accept-Ranges", "bytes"); - } - - return new Response(response.body, { - status: response.status, - headers: responseHeaders - }); + return new Response(response.body, { status: response.status, headers: responseHeaders }); } catch (err) { - console.error("[Proxy] Error:", err); - return new Response("Proxy error: " + String(err), { - status: 502, - headers: corsHeaders - }); + return new Response("Proxy error: " + String(err), { status: 502, headers: corsHeaders }); } } -// Fetch channel videos using YouTube Browse API async function fetchChannelVideos(channelId: string, limit?: number): Promise { try { - const url = "https://www.youtube.com/youtubei/v1/browse?prettyPrint=false"; - const payload = { - browseId: channelId, - context: { - client: { - clientName: "WEB", - clientVersion: "2.20251013.01.00", - hl: "en", - gl: "US", - }, - }, - }; - - const response = await fetch(url, { + const response = await fetch("https://www.youtube.com/youtubei/v1/browse?prettyPrint=false", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify(payload), + body: JSON.stringify({ browseId: channelId, context: { client: { clientName: "WEB", clientVersion: "2.20251013.01.00", hl: "en", gl: "US" } } }), }); - const data = await response.json(); const items: any[] = []; + const channelName = data?.header?.c4TabbedHeaderRenderer?.title || data?.metadata?.channelMetadataRenderer?.title || ""; - // Extract channel name - let channelName = data?.header?.c4TabbedHeaderRenderer?.title || - data?.metadata?.channelMetadataRenderer?.title || ""; - - // Extract videos from response const extractVideos = (contents: any[]) => { if (!contents) return; for (const item of contents) { - const video = item?.richItemRenderer?.content?.videoRenderer || - item?.videoRenderer || - item?.gridVideoRenderer; - if (video?.videoId) { - items.push(parseVideo(video, channelId, channelName)); - } - // Handle nested content - if (item?.shelfRenderer?.content?.expandedShelfContentsRenderer?.items) { - extractVideos(item.shelfRenderer.content.expandedShelfContentsRenderer.items); - } - if (item?.itemSectionRenderer?.contents) { - extractVideos(item.itemSectionRenderer.contents); - } + const video = item?.richItemRenderer?.content?.videoRenderer || item?.videoRenderer || item?.gridVideoRenderer; + if (video?.videoId) items.push(parseVideo(video, channelId, channelName)); + if (item?.shelfRenderer?.content?.expandedShelfContentsRenderer?.items) extractVideos(item.shelfRenderer.content.expandedShelfContentsRenderer.items); + if (item?.itemSectionRenderer?.contents) extractVideos(item.itemSectionRenderer.contents); if (limit && items.length >= limit) return; } }; - // Try different response structures - const tabs = data?.contents?.twoColumnBrowseResultsRenderer?.tabs || - data?.contents?.singleColumnBrowseResultsRenderer?.tabs || []; - - for (const tab of tabs) { - const contents = tab?.tabRenderer?.content?.sectionListRenderer?.contents || - tab?.tabRenderer?.content?.richGridRenderer?.contents || []; - extractVideos(contents); - } - + const tabs = data?.contents?.twoColumnBrowseResultsRenderer?.tabs || data?.contents?.singleColumnBrowseResultsRenderer?.tabs || []; + for (const tab of tabs) extractVideos(tab?.tabRenderer?.content?.sectionListRenderer?.contents || tab?.tabRenderer?.content?.richGridRenderer?.contents || []); return items.slice(0, limit || items.length); - } catch (err) { - console.error("Channel fetch error:", err); - return []; - } + } catch { return []; } } function parseVideo(video: any, channelId: string, channelName: string): any { const id = video?.videoId || ""; const title = video?.title?.runs?.[0]?.text || video?.title?.simpleText || ""; - - // Parse duration let duration = 0; - const durationText = video?.lengthText?.simpleText || - video?.thumbnailOverlays?.[0]?.thumbnailOverlayTimeStatusRenderer?.text?.simpleText || ""; + const durationText = video?.lengthText?.simpleText || video?.thumbnailOverlays?.[0]?.thumbnailOverlayTimeStatusRenderer?.text?.simpleText || ""; if (durationText) { const parts = durationText.split(":").map((p: string) => parseInt(p) || 0); if (parts.length === 2) duration = parts[0] * 60 + parts[1]; else if (parts.length === 3) duration = parts[0] * 3600 + parts[1] * 60 + parts[2]; } - - // Parse views let views = 0; const viewText = video?.viewCountText?.simpleText || ""; const match = viewText.match(/([\d,\.]+)([KMB]?)/); - if (match) { - let num = parseFloat(match[1].replace(/,/g, "")); - if (match[2] === "K") num *= 1000; - else if (match[2] === "M") num *= 1000000; - else if (match[2] === "B") num *= 1000000000; - views = Math.floor(num); - } - - // Parse published time + if (match) { let num = parseFloat(match[1].replace(/,/g, "")); if (match[2] === "K") num *= 1000; else if (match[2] === "M") num *= 1000000; else if (match[2] === "B") num *= 1000000000; views = Math.floor(num); } let uploaded = Date.now(); const timeText = (video?.publishedTimeText?.simpleText || "").toLowerCase(); if (timeText.includes("hour")) uploaded -= parseInt(timeText.match(/(\d+)/)?.[1] || "1") * 3600000; @@ -812,24 +396,8 @@ function parseVideo(video: any, channelId: string, channelName: string): any { else if (timeText.includes("week")) uploaded -= parseInt(timeText.match(/(\d+)/)?.[1] || "1") * 604800000; else if (timeText.includes("month")) uploaded -= parseInt(timeText.match(/(\d+)/)?.[1] || "1") * 2592000000; else if (timeText.includes("year")) uploaded -= parseInt(timeText.match(/(\d+)/)?.[1] || "1") * 31536000000; - - const isShort = duration > 0 && duration <= 60; - - return { - id, - authorId: channelId, - duration: duration.toString(), - author: channelName, - views: views.toString(), - uploaded: uploaded.toString(), - title, - isShort, - thumbnail: video?.thumbnail?.thumbnails?.slice(-1)[0]?.url || "", - }; + return { id, authorId: channelId, duration: duration.toString(), author: channelName, views: views.toString(), uploaded: uploaded.toString(), title, isShort: duration > 0 && duration <= 60, thumbnail: video?.thumbnail?.thumbnails?.slice(-1)[0]?.url || "" }; } -// Start server console.log(`Virome API running on http://localhost:${PORT}`); -console.log(`Endpoints: /api/search, /api/stream, /api/charts, etc.`); - serve(handler, { port: PORT }); diff --git a/ui.ts b/ui.ts index c48d8b1..0b2a25f 100644 --- a/ui.ts +++ b/ui.ts @@ -14,45 +14,27 @@ export const html = ` *{margin:0;padding:0;box-sizing:border-box} :root{--accent:#10b981;--accent-dim:rgba(16,185,129,.15);--bg:#0a0a0a;--surface:#111;--surface2:#1a1a1a;--border:#222;--text:#fff;--muted:#888;--dim:#555} body{font-family:'Inter',system-ui,sans-serif;min-height:100vh;color:var(--text);background:var(--bg)} - - /* Subtle gradient background */ .bg{position:fixed;inset:0;z-index:-1;background:radial-gradient(ellipse 80% 50% at 50% -20%,rgba(16,185,129,.08),transparent)} - .container{max-width:900px;margin:0 auto;padding:60px 24px 180px} - - /* Hero */ .hero{text-align:center;margin-bottom:80px} .logo{width:160px;height:160px;margin-bottom:32px;filter:drop-shadow(0 20px 50px rgba(16,185,129,.4));animation:float 6s ease-in-out infinite} @keyframes float{0%,100%{transform:translateY(0)}50%{transform:translateY(-10px)}} .title{font-size:3rem;font-weight:700;margin-bottom:12px;letter-spacing:-1px} .subtitle{color:var(--muted);font-size:1.1rem;font-weight:400} - - /* Navigation */ .nav{display:flex;justify-content:center;gap:8px;margin-bottom:48px} - .nav-btn{padding:12px 28px;background:transparent;border:1px solid var(--border);color:var(--muted);font-size:.9rem;font-weight:500;cursor:pointer;border-radius:10px;transition:all .3s cubic-bezier(.4,0,.2,1);font-family:inherit;position:relative;overflow:hidden} - .nav-btn::before{content:'';position:absolute;inset:0;background:var(--accent);opacity:0;transition:opacity .3s} + .nav-btn{padding:12px 28px;background:transparent;border:1px solid var(--border);color:var(--muted);font-size:.9rem;font-weight:500;cursor:pointer;border-radius:10px;transition:all .3s;font-family:inherit} .nav-btn:hover{color:var(--text);border-color:#444;transform:translateY(-2px)} .nav-btn.active{color:var(--accent);border-color:var(--accent);background:var(--accent-dim);transform:translateY(-2px)} - - /* Tabs with transitions */ - .tab{display:none;opacity:0;transform:translateY(20px);transition:opacity .4s ease,transform .4s ease} + .tab{display:none;opacity:0;transform:translateY(20px);transition:opacity .4s,transform .4s} .tab.active{display:block;opacity:1;transform:translateY(0)} - .tab.fade-out{opacity:0;transform:translateY(-20px)} - .tab.fade-in{display:block} - - /* Section */ .section{margin-bottom:40px} .section-title{font-size:.7rem;text-transform:uppercase;letter-spacing:1.5px;color:var(--accent);margin-bottom:16px;font-weight:600} - - /* API Cards */ .api-list{display:flex;flex-direction:column;gap:8px} .api-item{display:flex;align-items:center;gap:16px;padding:16px 20px;background:var(--surface);border:1px solid var(--border);border-radius:12px;transition:all .15s;cursor:pointer} .api-item:hover{border-color:#333;transform:translateX(4px)} .method{font-size:.65rem;font-weight:700;padding:5px 10px;border-radius:6px;background:var(--accent-dim);color:var(--accent);min-width:42px;text-align:center} .path{font-family:'SF Mono',Monaco,monospace;font-size:.85rem;flex:1} .desc{font-size:.75rem;color:var(--dim);max-width:280px;text-align:right} - - /* Search */ .search-row{display:flex;gap:12px;margin-bottom:24px} .input{flex:1;background:var(--surface);border:1px solid var(--border);padding:14px 18px;border-radius:10px;color:var(--text);font-size:.95rem;font-family:inherit} .input:focus{outline:none;border-color:var(--accent)} @@ -63,8 +45,6 @@ export const html = ` .btn{background:var(--accent);color:#000;border:none;padding:14px 28px;border-radius:10px;font-size:.9rem;font-weight:600;font-family:inherit;cursor:pointer;transition:all .15s} .btn:hover{opacity:.9} .btn:disabled{opacity:.4} - - /* Results */ .results{max-height:50vh;overflow-y:auto} .result{display:flex;align-items:center;gap:14px;padding:12px;border-radius:10px;cursor:pointer;transition:all .15s} .result:hover{background:var(--surface)} @@ -76,14 +56,10 @@ export const html = ` .dur{font-size:.75rem;color:var(--dim);font-family:monospace} .empty{padding:48px;text-align:center;color:var(--dim)} .loading{display:none;padding:48px;text-align:center;color:var(--accent)} - - /* API Tester */ .tester-row{display:flex;gap:12px;margin-bottom:16px;flex-wrap:wrap} .url-preview{font-family:monospace;font-size:.8rem;color:var(--muted);padding:12px 16px;background:var(--surface);border-radius:8px;margin-bottom:16px;border:1px solid var(--border)} .response{background:var(--surface);border:1px solid var(--border);border-radius:12px;padding:20px;margin-top:20px;max-height:400px;overflow:auto} .response pre{font-family:'SF Mono',Monaco,monospace;font-size:.75rem;color:var(--accent);white-space:pre-wrap;word-break:break-all} - - /* Player */ .player{position:fixed;bottom:0;left:0;right:0;background:rgba(10,10,10,.95);backdrop-filter:blur(20px);border-top:1px solid var(--border);padding:16px 24px;display:none;z-index:100} .player.visible{display:block} .player-inner{max-width:900px;margin:0 auto} @@ -100,14 +76,7 @@ export const html = ` .time{font-size:.7rem;color:var(--muted);min-width:40px;font-family:monospace} .bar{flex:1;height:4px;background:var(--surface2);border-radius:2px;cursor:pointer} .fill{height:100%;background:var(--accent);border-radius:2px;width:0%} - - @media(max-width:600px){ - .container{padding:40px 16px 180px} - .logo{width:80px;height:80px} - .title{font-size:2rem} - .desc{display:none} - .search-row{flex-direction:column} - } + @media(max-width:600px){.container{padding:40px 16px 180px}.logo{width:80px;height:80px}.title{font-size:2rem}.desc{display:none}.search-row{flex-direction:column}} @@ -125,33 +94,24 @@ export const html = ` -
Search
GET/api/searchSearch songs, albums, artists
GET/api/search/suggestionsAutocomplete suggestions
+
GET/api/yt_searchYouTube video search
-
Content (v1)
+
Content
-
GET/api/songs/:videoIdSong details
-
GET/api/albums/:browseIdAlbum with tracks
-
GET/api/artists/:browseIdArtist profile
+
GET/api/songs/:videoIdSong + artist/album links
+
GET/api/albums/:browseIdAlbum + tracks + artist
+
GET/api/artists/:browseIdArtist + discography
GET/api/playlists/:playlistIdPlaylist tracks
-
-
- -
-
Unified Content (v2) — Linked Entities
-
-
GET/api/v2/songs/:videoIdSong + artist/album links
-
GET/api/v2/albums/:browseIdAlbum + artist + numbered tracks
-
GET/api/v2/artists/:browseIdArtist + full discography
-
GET/api/v2/chain/:videoIdSong → Artist → Discography
+
GET/api/chain/:videoIdSong -> Artist -> Albums
@@ -160,7 +120,10 @@ export const html = `
GET/api/related/:videoIdRelated songs
GET/api/radio?videoId=Generate radio mix
+
GET/api/similar?title=&artist=Similar tracks
GET/api/charts?country=Music charts
+
GET/api/trending?country=Trending music
+
GET/api/moodsMood categories
@@ -168,12 +131,22 @@ export const html = `
Streaming & Lyrics
GET/api/stream?id=Audio stream URLs
+
GET/api/proxy?url=Audio proxy (CORS)
GET/api/lyrics?title=&artist=Synced lyrics (LRC)
+ +
+
Info
+
+
GET/api/artist/info?artist=Artist bio (Last.fm)
+
GET/api/track/info?title=&artist=Track info (Last.fm)
+
GET/api/top/artists?country=Top artists
+
GET/api/top/tracks?country=Top tracks
+
+
-
@@ -198,10 +170,7 @@ export const html = ` - - - - + @@ -215,7 +184,6 @@ export const html = `
-
@@ -241,38 +209,16 @@ export const html = `