const emojiMap = (genre, artist) => {
const DEFAULT = "π§";
const normalizedArtist = artist?.toLowerCase();
if (normalizedArtist === "agalloch") return "ποΈ";
if (normalizedArtist === "augury") return "βοΈ";
if (normalizedArtist === "autopsy") return "π§";
if (normalizedArtist === "bedsore") 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 === "panopticon") return "πͺπͺ¦";
if (normalizedArtist === "taylor swift") 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 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 artistCapitalizationRes = await fetch("https://coryd.dev/api/artist-capitalization", {
type: "json",
}).then((data) => {
if (data.ok) return data.json()
throw new Error('Something went wrong with the artist capitalization endpoint.');
}).catch(err => {
console.log(err);
return {}
});
const track = trackRes["recenttracks"]["track"][0];
let mbid = track["artist"]["mbid"];
let genre = '';
const artistCapitalization = (artist) => artistCapitalizationRes[artist?.toLowerCase()] || artist
const artist = artistCapitalization(track["artist"]["#text"]);
const mbidMap = () => mbidRes[track["artist"]["#text"].toLowerCase()] || "";
// mbid mismatches
if (mbidMap() !== "") mbid = mbidMap();
const artistUrl = mbid
? `https://musicbrainz.org/artist/${mbid}`
: `https://last.fm/music/${track["artist"]["#text"].toLowerCase().replace(/\s+/g, "+")}`;
const trackUrl = track["mbid"]
? `https://musicbrainz.org/track/${track["mbid"]}`
: track['url'];
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,
artist
)} ${track["name"]} by ${
artist
}`,
}),
{ headers }
)
};
export const config = {
cache: "manual",
path: "/api/now-playing"
};