const emojiMap = (genre, artist) => {
const DEFAULT = "π§";
if (artist === "Augury") return "βοΈ";
if (artist === "Autopsy") return "π§";
if (artist === "Bedsore") return "ποΈ";
if (artist === "Black Flag") return "π΄";
if (artist === "Blood Incantation") return "π½";
if (artist === "Bolt Thrower") return "βοΈ"
if (artist === "Bruce Springsteen") return "πΊπΈ";
if (artist === "Carcass") return "π₯Ό";
if (artist === "Counting Crows") return "π¦ββ¬";
if (artist === "David Bowie") return "π¨π»βπ€";
if (artist === "Devoid of Thought") return "π";
if (artist === "Full of Hell & Nothing") return "π«¨πΈ";
if (artist === "Imperial Triumphant") return "π";
if (artist === "Mastodon") return "π";
if (artist === "Minor Threat") return "π¨π»βπ¦²";
if (artist === "Taylor Swift") return "πΈπΌ";
// early return for bad input
if (!genre) return DEFAULT;
if (genre.includes("death metal")) 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")
)
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 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 trackRes = await fetch(
`https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=coryd_&api_key=${MUSIC_KEY}&limit=1&format=json`,
{
type: "json",
}
).then((data) => {
if (data.ok) return data.json()
throw new Error('Something went wrong with the Last.fm endpoint.');
}).catch(err => {
console.log(err);
return {}
});
const mbidRes = await fetch("https://coryd.dev/api/mbids", {
type: "json",
}).then((data) => {
if (data.ok) return data.json()
throw new Error('Something went wrong with the mbid endpoint.');
}).catch(err => {
console.log(err);
return {}
});
const track = trackRes["recenttracks"]["track"][0];
const artist = track["artist"]["#text"];
let mbid = track["artist"]["mbid"];
let genre = "";
const mbidMap = (artist) => mbidRes[artist.toLowerCase()] || "";
// mbid mismatches
if (mbidMap(artist) !== "") mbid = mbidMap(artist);
const artistUrl = mbid
? `https://musicbrainz.org/artist/${mbid}`
: `https://musicbrainz.org/search?query=${track["artist"]["#text"].replace(
/\s+/g,
"+"
)}&type=artist`;
const trackUrl = track["mbid"]
? `https://musicbrainz.org/track/${track["mbid"]}`
: `https://musicbrainz.org/search?query=${track["artist"]["#text"].replace(
/\s+/g,
'+'
)}&type=artist`;
if (mbid && mbid !== "") {
const genreUrl = `https://musicbrainz.org/ws/2/artist/${mbid}?inc=aliases+genres&fmt=json`;
const genreRes = await fetch(genreUrl, {
type: "json",
}).then((data) => {
if (data.ok) return data.json()
throw new Error('Something went wrong with the MusicBrainz endpoint.');
}).catch(err => {
console.log(err);
return {}
});
genre = genreRes['genres'].sort((a, b) => b.count - a.count)[0]?.["name"] || "";
}
return new Response(JSON.stringify({
content: `${emojiMap(
genre,
track["artist"]["#text"]
)} ${track["name"]} by ${
track["artist"]["#text"]
}`,
}),
{ headers }
)
};
export const config = {
cache: "manual",
path: "/api/now-playing"
};