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.status) 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"
};