const emojiMap = (genre, artist) => { const DEFAULT = '🎧' if (!genre) return DEFAULT // early return for bad input if (artist === 'David Bowie') return 'ðŸ‘ĻðŸŧ‍ðŸŽĪ' if (artist === 'Minor Threat') return 'ðŸ‘ĻðŸŧ‍ðŸĶē' if (genre.includes('death metal')) return '💀' if (genre.includes('black metal')) 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') ) 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('alternative') || genre.includes('rock') || genre.includes('shoegaze') || genre.includes('screamo') ) return 'ðŸŽļ' return DEFAULT } 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 mbid = track['artist']['mbid'] let genre = '' if (mbid && mbid !== '') { const genreUrl = `https://musicbrainz.org/ws/2/artist/${mbid}?inc=aliases+genres&fmt=json` const genreRes = await fetch(genreUrl, { type: 'json', }).catch() const genreData = await genreRes.json() 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' }