import { getStore } from '@netlify/blobs' const emojiMap = (genre, artist) => { const DEFAULT = "🎧"; const normalizedArtist = artist?.toLowerCase(); if (normalizedArtist === "afi") return "â›ĩī¸đŸŒ…"; if (normalizedArtist === "agalloch") return "🏔ī¸"; if (normalizedArtist === "augury") return "☄ī¸"; if (normalizedArtist === "autopsy") return "🧟"; if (normalizedArtist === "balance and composure") return "đŸĒ‚"; if (normalizedArtist === "bedsore") return "🛏ī¸"; if (normalizedArtist === "birds in row") return "đŸĻ…đŸĻ‰đŸĻĸ"; if (normalizedArtist === "black flag") return "🏴"; if (normalizedArtist === "blink-182") return "đŸ˜ĩ"; if (normalizedArtist === "blood incantation") return "đŸ‘Ŋ"; if (normalizedArtist === "bolt thrower") return "⚔ī¸" if (normalizedArtist === "bruce springsteen") return "đŸ‡ē🇸"; if (normalizedArtist === "carcass") return "đŸĨŧ"; if (normalizedArtist === "counting crows") return "đŸĻ‍âŦ›"; if (normalizedArtist === "david bowie") return "👨đŸģ‍🎤"; if (normalizedArtist === "cevoid of thought") return "đŸšĢ💭"; if (normalizedArtist === "drug church") return "💊â›Ēī¸"; if (normalizedArtist === "fleshwater") return "🐤"; if (normalizedArtist === "full of hell & nothing") return "đŸĢ¨đŸŽ¸"; if (normalizedArtist === "imperial triumphant") return "🎭"; if (normalizedArtist === "mastodon") return "🐋"; if (normalizedArtist === "minor threat") return "👨đŸģ‍đŸĻ˛"; if (normalizedArtist === "nomeansno") return "đŸĩ🐮đŸšŦ"; if (normalizedArtist === "nothing") return "đŸŗī¸"; if (normalizedArtist === "panopticon") return "đŸĒ•đŸĒĻ"; if (normalizedArtist === "radiohead") return "đŸ“ģ"; if (normalizedArtist === "taylor swift") return "👸đŸŧ"; if (normalizedArtist === "the mars volta") return "💡😮"; if (normalizedArtist === "thrice") return "👨‍🎨🚑"; if (normalizedArtist === "webbed wing") return "🤡"; // early return for bad input if (!genre) return DEFAULT; if (genre.includes("death metal") || genre.includes("death-doom")) return "💀"; if (genre.includes("black metal") || genre.includes("blackgaze")) return "đŸĒĻ"; if (genre.includes("metal")) return "🤘"; if (genre.includes("emo") || genre.includes("blues")) return "đŸ˜ĸ"; if (genre.includes("grind") || genre.includes("powerviolence")) return "đŸĢ¨"; if ( genre.includes("country") || genre.includes("americana") || genre.includes("bluegrass") || genre.includes("folk") || genre.includes("songwriter") ) return "đŸĒ•"; if (genre.includes("post-punk")) return "😔"; if (genre.includes("dance-punk")) return "đŸĒŠ"; if (genre.includes("punk") || genre.includes("hardcore")) return "✊"; if (genre.includes("hip hop")) return "🎤"; if (genre.includes("progressive") || genre.includes("experimental")) return "🤓"; if (genre.includes("jazz")) return "đŸŽē"; if (genre.includes("psychedelic")) return "💊"; if (genre.includes("dance") || genre.includes("electronic")) return "đŸ’ģ"; if (genre.includes("ambient")) return "đŸ¤Ģ"; if ( genre.includes("alternative") || genre.includes("rock") || genre.includes("shoegaze") || genre.includes("screamo") || genre.includes("grunge") ) return "🎸"; return DEFAULT; }; export default async () => { const TV_KEY = Netlify.env.get("API_KEY_TRAKT"); const scrobbles = getStore('scrobbles') const headers = { "Content-Type": "application/json", "Cache-Control": "public, s-maxage=360, stale-while-revalidate=1080", }; const traktRes = await fetch("https://api.trakt.tv/users/cdransf/watching", { headers: { "Content-Type": "application/json", "trakt-api-version": 2, "trakt-api-key": TV_KEY, }, }) .then((data) => { if (data.ok) return data?.json(); throw new Error('Something went wrong with the Trakt endpoint.'); }) .catch(err => { console.log(err); return {} }); if (Object.keys(traktRes).length) { if (traktRes["type"] === "episode") { return new Response(JSON.stringify({ content: `đŸ“ē ${traktRes["show"]["title"]} â€ĸ ${traktRes["episode"]["title"]}`, }), { headers } ) } if (traktRes["type"] === "movie") { return new Response(JSON.stringify({ content: `đŸŽĨ ${traktRes["movie"]["title"]}`, }), { headers } ) } } const scrobbleData = await scrobbles.get('now-playing', { type: 'json'}) if (!scrobbleData) return new Response(JSON.stringify({}, { headers })) return new Response(JSON.stringify({ content: `${emojiMap( scrobbleData['genre'], scrobbleData['artist'] )} ${scrobbleData['track']} by ${ scrobbleData['artist'] }`, }), { headers } ) }; export const config = { cache: "manual", path: "/api/now-playing" };