diff --git a/.gitignore b/.gitignore index 1bd0165b..398406c6 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ node_modules # system files .DS_Store + +# netlify +.netlify diff --git a/netlify/edge-functions/now-playing.js b/netlify/edge-functions/now-playing.js new file mode 100644 index 00000000..297ab4d1 --- /dev/null +++ b/netlify/edge-functions/now-playing.js @@ -0,0 +1,40 @@ +const emojiMap = (genre, artist) => { + if (artist === 'David Bowie') return '👨🎤' + if (genre.includes('metal')) return '🤘' + if (genre.includes('emo')) return '😢' + if (genre.includes('alternative')) return '🎸' + if (genre.includes('grind') || genre.includes('powerviolence')) return '🫨' + if (genre.includes('country') || genre.includes('americana') || genre.includes('bluegrass')) + return '🪕' + if (genre.includes('post-punk')) return '😔' + if (genre.includes('punk') || genre.includes('hardcore')) return '✊' + if (genre.includes('hip hop')) return '🎤' + if (genre.includes('dance') || genre.includes('electronic')) return '💻' + return '🎧' +} + +export default async () => { + // eslint-disable-next-line no-undef + const MUSIC_KEY = Netlify.env.get('API_KEY_LASTFM') + const trackUrl = `https://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=cdrn_&api_key=${MUSIC_KEY}&limit=1&format=json` + const trackRes = await fetch(trackUrl, { + type: 'json', + }).catch() + const trackData = await trackRes.json() + const track = trackData['recenttracks']['track'][0] + const genreUrl = `https://musicbrainz.org/ws/2/artist/${track['artist']['mbid']}?inc=aliases+genres&fmt=json` + const genreRes = await fetch(genreUrl, { + type: 'json', + }).catch() + const genreData = await genreRes.json() + const genre = genreData.genres.sort((a, b) => b.count - a.count)[0]['name'] + + return Response.json({ + artist: track['artist']['#text'], + title: track['name'], + genre, + emoji: emojiMap(genre, track['artist']['#text']), + }) +} + +export const config = { path: '/api/now-playing' } diff --git a/src/_includes/base.liquid b/src/_includes/base.liquid index c6d1f057..e204436c 100644 --- a/src/_includes/base.liquid +++ b/src/_includes/base.liquid @@ -69,5 +69,11 @@
{{ content }} + {% capture js %} + {% render "../assets/scripts/script.js" %} + {% endcapture %} +