From 80b2f7da7785d87d521336ff0616b1b7e402380c Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sat, 7 Oct 2023 11:00:47 -0700 Subject: [PATCH] chore: last.fm -> apple music --- .env | 3 + netlify/edge-functions/now-playing.js | 72 ++++----------- src/_data/music.js | 104 ++++++++++++++++++++++ src/_includes/now.liquid | 4 +- src/_includes/partials/now/playing.liquid | 2 +- src/assets/scripts/script.js | 1 - 6 files changed, 125 insertions(+), 61 deletions(-) create mode 100644 src/_data/music.js diff --git a/.env b/.env index 2a21cec0..afc0c984 100644 --- a/.env +++ b/.env @@ -2,6 +2,9 @@ API_KEY_LASTFM= API_KEY_TRAKT= API_KEY_MOVIEDB= API_KEY_WEBMENTIONS_CORYD_DEV= +API_APPLE_MUSIC_DEVELOPER_TOKEN= +API_APPLE_MUSIC_USER_TOKEN= +APPLE_RENEW_TOKEN_URL= SITE_ID_CLICKY= SITE_KEY_CLICKY= SECRET_FEED_ALBUM_RELEASES= diff --git a/netlify/edge-functions/now-playing.js b/netlify/edge-functions/now-playing.js index 6e8f62a0..b92f7e41 100644 --- a/netlify/edge-functions/now-playing.js +++ b/netlify/edge-functions/now-playing.js @@ -1,66 +1,24 @@ -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 (artist === 'Bruce Springsteen') 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 API_APPLE_MUSIC_DEVELOPER_TOKEN = Netlify.env.get('API_APPLE_MUSIC_DEVELOPER_TOKEN') + // eslint-disable-next-line no-undef + const API_APPLE_MUSIC_USER_TOKEN = Netlify.env.get('API_APPLE_MUSIC_USER_TOKEN') + const trackRes = await fetch('https://api.music.apple.com/v1/me/recent/played/tracks?limit=1', { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${API_APPLE_MUSIC_DEVELOPER_TOKEN}`, + 'music-user-token': `${API_APPLE_MUSIC_USER_TOKEN}`, + }, + }) + .then((data) => data.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'] || '' - } + const track = trackData['attributes'][0] return Response.json({ - artist: track['artist']['#text'], + artist: track['artistName'], title: track['name'], - url: track['url'], - genre, - emoji: emojiMap(genre, track['artist']['#text']), + emoji: '🎧', }) } diff --git a/src/_data/music.js b/src/_data/music.js new file mode 100644 index 00000000..b669e800 --- /dev/null +++ b/src/_data/music.js @@ -0,0 +1,104 @@ +const { AssetCache } = require('@11ty/eleventy-fetch') + +const sortTrim = (array, length = 8) => + Object.values(array) + .sort((a, b) => b.plays - a.plays) + .splice(0, length) + +module.exports = async function () { + const API_APPLE_MUSIC_DEVELOPER_TOKEN = process.env.API_APPLE_MUSIC_DEVELOPER_TOKEN + const API_APPLE_MUSIC_USER_TOKEN = process.env.API_APPLE_MUSIC_USER_TOKEN + const APPLE_RENEW_TOKEN_URL = process.env.APPLE_RENEW_TOKEN_URL + const asset = new AssetCache('recent_tracks_data') + const PAGE_SIZE = 30 + const PAGES = 8 + const response = { + artists: {}, + albums: {}, + tracks: {}, + } + + const RENEWED_MUSIC_TOKEN = await fetch(APPLE_RENEW_TOKEN_URL, { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${API_APPLE_MUSIC_DEVELOPER_TOKEN}`, + 'X-Apple-Music-User-Token': `${API_APPLE_MUSIC_USER_TOKEN}`, + }, + }) + .then((data) => data.json()) + .catch() + + let CURRENT_PAGE = 0 + let res = [] + + if (asset.isCacheValid('1h')) return await asset.getCachedValue() + + while (CURRENT_PAGE < PAGES) { + const URL = `https://api.music.apple.com/v1/me/recent/played/tracks?limit=${PAGE_SIZE}&offset=${ + PAGE_SIZE * CURRENT_PAGE + }` + const tracks = await fetch(URL, { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${API_APPLE_MUSIC_DEVELOPER_TOKEN}`, + 'music-user-token': `${RENEWED_MUSIC_TOKEN['music-token']}`, + }, + }) + .then((data) => data.json()) + .catch() + res = [...res, ...tracks.data] + CURRENT_PAGE++ + } + + res.forEach((track) => { + if (!response['artists'][track['attributes']['artistName']]) { + response['artists'][track['attributes']['artistName']] = { + title: track['attributes']['artistName'], + image: + `https://cdn.coryd.dev/artists/${track['attributes']['artistName'] + .replace(/\s+/g, '-') + .toLowerCase()}.jpg` || 'https://cdn.coryd.dev/artists/missing-artist.jpg', + url: `https://musicbrainz.org/search?query=${track['attributes']['artistName'].replace( + /\s+/g, + '+' + )}&type=artist`, + plays: 1, + type: 'artist', + } + } else { + response['artists'][track['attributes']['artistName']].plays++ + } + + // aggregate albums + if (!response.albums[track['attributes']['albumName']]) { + response.albums[track['attributes']['albumName']] = { + title: track['attributes']['albumName'], + artist: track['attributes']['artistName'], + image: track['attributes']['artwork']['url'].replace('{w}', '300').replace('{h}', '300'), + url: `https://musicbrainz.org/search?query=${track['attributes']['albumName'].replace( + /\s+/g, + '+' + )}&type=recording`, + plays: 1, + type: 'album', + } + } else { + response.albums[track['attributes']['albumName']].plays++ + } + + // aggregate tracks + if (!response.tracks[track['attributes']['name']]) { + response.tracks[track['attributes']['name']] = { + name: track['attributes']['name'], + plays: 1, + } + } else { + response.tracks[track['attributes']['name']].plays++ + } + }) + response.artists = sortTrim(response.artists) + response.albums = sortTrim(response.albums) + response.tracks = sortTrim(response.tracks, 5) + await asset.save(response, 'json') + return response +} diff --git a/src/_includes/now.liquid b/src/_includes/now.liquid index 2c1f8f7a..bec50182 100644 --- a/src/_includes/now.liquid +++ b/src/_includes/now.liquid @@ -3,8 +3,8 @@ layout: main --- {% render "partials/header.liquid", site: site, page: page, nav: nav %} {{ content }} -{% render "partials/now/media-grid.liquid", data:artists, icon: "microphone-2", title: "Artists", shape: "square", count: 8, loading: 'eager' %} -{% render "partials/now/media-grid.liquid", data:albums, icon: "vinyl", title: "Albums", shape: "square", count: 8, loading: 'lazy' %} +{% render "partials/now/media-grid.liquid", data:music.artists, icon: "microphone-2", title: "Artists", shape: "square", count: 8, loading: 'eager' %} +{% render "partials/now/media-grid.liquid", data:music.albums, icon: "vinyl", title: "Albums", shape: "square", count: 8, loading: 'lazy' %} {% render "partials/now/albumReleases.liquid", albumReleases:albumReleases %} {% render "partials/now/media-grid.liquid", data:books, icon: "books", title: "Books", shape: "vertical", count: 6, loading: 'lazy' %} {% render "partials/now/media-grid.liquid", data:movies, icon: "movie", title: "Movies", shape: "vertical", count: 6, loading: 'lazy' %} diff --git a/src/_includes/partials/now/playing.liquid b/src/_includes/partials/now/playing.liquid index a7d6b1d3..eebdb0ec 100644 --- a/src/_includes/partials/now/playing.liquid +++ b/src/_includes/partials/now/playing.liquid @@ -3,6 +3,6 @@ {% tablericon 'loader-2' 'Loading...' %} - +

\ No newline at end of file diff --git a/src/assets/scripts/script.js b/src/assets/scripts/script.js index 7dac00d6..9b647c2c 100644 --- a/src/assets/scripts/script.js +++ b/src/assets/scripts/script.js @@ -7,7 +7,6 @@ const populateNowPlaying = (data) => { loading.style.display = 'none' emoji.innerText = data.emoji - content.href = data.url content.innerText = `${data.title} by ${data.artist}` emoji.classList.remove('hidden') content.classList.remove('hidden')