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 === "tom waits") 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.status === 204) return {}
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"
};