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 === "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 === "nothing") return "🏳️"; if (normalizedArtist === "panopticon") return "πŸͺ•πŸͺ¦"; if (normalizedArtist === "radiohead") return "πŸ“»"; if (normalizedArtist === "taylor swift") 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 MUSIC_KEY = Netlify.env.get("API_KEY_LASTFM"); 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'}) 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" };