From bd2aa7439c7b54f743f00d9084636292c795e953 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Tue, 20 Jun 2023 19:15:01 -0700 Subject: [PATCH] feat: last.fm -> apple music --- .env | 2 + config/mediaFilters.js | 10 - src/_data/heavyRotation.js | 20 ++ src/_data/recentTracks.js | 62 +++++ src/_data/site.json | 2 +- src/_includes/footer.liquid | 2 +- src/_includes/icons/apple-music.liquid | 13 + src/_includes/icons/lastfm.liquid | 13 - src/_includes/now.liquid | 368 ++++++++++++------------- 9 files changed, 280 insertions(+), 212 deletions(-) create mode 100644 src/_data/heavyRotation.js create mode 100644 src/_data/recentTracks.js create mode 100644 src/_includes/icons/apple-music.liquid delete mode 100644 src/_includes/icons/lastfm.liquid diff --git a/.env b/.env index e55e13be..ae91472e 100644 --- a/.env +++ b/.env @@ -1,5 +1,7 @@ API_KEY_LASTFM= API_KEY_TRAKT= API_KEY_WEBMENTIONS_CORYD_DEV= +API_BEARER_APPLE_MUSIC= +API_TOKEN_APPLE_MUSIC= SECRET_FEED_INSTAPAPER_FAVORITES= SECRET_FEED_ALBUM_RELEASES= \ No newline at end of file diff --git a/config/mediaFilters.js b/config/mediaFilters.js index caa9cc80..5b686ffb 100644 --- a/config/mediaFilters.js +++ b/config/mediaFilters.js @@ -1,16 +1,6 @@ -const ALBUM_DENYLIST = ['no-love-deep-web', 'unremittance'] - module.exports = { artist: (media) => `https://cdn.coryd.dev/artists/${media.replace(/\s+/g, '-').toLowerCase()}.jpg`, - album: (media) => { - return !ALBUM_DENYLIST.includes(media.name.replace(/\s+/g, '-').toLowerCase()) - ? media.image[media.image.length - 1]['#text'].replace( - 'https://lastfm.freetls.fastly.net', - 'https://albums.coryd.dev' - ) - : `https://cdn.coryd.dev/albums/${media.name.replace(/\s+/g, '-').toLowerCase()}.jpg` - }, tv: (episode) => `https://cdn.coryd.dev/tv/${episode.replace(':', '').replace(/\s+/g, '-').toLowerCase()}.jpg`, cdn: (url, host, cdn) => { diff --git a/src/_data/heavyRotation.js b/src/_data/heavyRotation.js new file mode 100644 index 00000000..2bd825b7 --- /dev/null +++ b/src/_data/heavyRotation.js @@ -0,0 +1,20 @@ +const EleventyFetch = require('@11ty/eleventy-fetch') + +module.exports = async function () { + const APPLE_BEARER = process.env.API_BEARER_APPLE_MUSIC + const APPLE_TOKEN = process.env.API_TOKEN_APPLE_MUSIC + const url = `https://api.music.apple.com/v1/me/history/heavy-rotation` + const res = EleventyFetch(url, { + duration: '1h', + type: 'json', + fetchOptions: { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${APPLE_BEARER}`, + 'music-user-token': `${APPLE_TOKEN}`, + }, + }, + }).catch() + const rotation = await res + return rotation.data +} diff --git a/src/_data/recentTracks.js b/src/_data/recentTracks.js new file mode 100644 index 00000000..d1abc7c6 --- /dev/null +++ b/src/_data/recentTracks.js @@ -0,0 +1,62 @@ +const { AssetCache } = require('@11ty/eleventy-fetch') + +const sortTrim = (array, length = 5) => + Object.values(array) + .sort((a, b) => b.plays - a.plays) + .splice(0, length) + +module.exports = async function () { + const APPLE_BEARER = process.env.API_BEARER_APPLE_MUSIC + const APPLE_TOKEN = process.env.API_TOKEN_APPLE_MUSIC + const PAGE_SIZE = 30 + let CURRENT_PAGE = 0 + const PAGES = 4 + let res = [] + const asset = new AssetCache('recent_tracks_data') + 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 ${APPLE_BEARER}`, + 'music-user-token': `${APPLE_TOKEN}`, + }, + }) + .then((data) => data.json()) + .catch() + res = [...res, ...tracks.data] + CURRENT_PAGE++ + } + const response = { + artists: {}, + tracks: {}, + } + res.forEach((track) => { + // aggregate artists + if (!response.artists[track.attributes.artistName]) { + response.artists[track.attributes.artistName] = { + name: track.attributes.artistName, + plays: 1, + } + } else { + response.artists[track.attributes.artistName].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, 4) + response.tracks = sortTrim(response.tracks) + await asset.save(response, 'json') + return response +} diff --git a/src/_data/site.json b/src/_data/site.json index 56f4c5cf..d1d6155a 100644 --- a/src/_data/site.json +++ b/src/_data/site.json @@ -8,7 +8,7 @@ "fastmail": "mailto:hi@coryd.dev", "github": "https://github.com/cdransf", "mastodon": "https://social.lol/@cory", - "lastfm": "https://last.fm/user/cdme_", + "applemusic": "https://music.apple.com/profile/cdransf", "listenbrainz": "https://listenbrainz.org/user/cdransf/", "instapaper": "https://www.instapaper.com/p/coryd", "letterboxd": "https://letterboxd.com/cdme", diff --git a/src/_includes/footer.liquid b/src/_includes/footer.liquid index 43815045..a544ebc2 100644 --- a/src/_includes/footer.liquid +++ b/src/_includes/footer.liquid @@ -3,7 +3,7 @@ {% include "icons/fastmail.liquid" %} {% include "icons/github.liquid" %} {% include "icons/mastodon.liquid" %} - {% include "icons/lastfm.liquid" %} + {% include "icons/apple-music.liquid" %} {% include "icons/instapaper.liquid" %} {% include "icons/letterboxd.liquid" %} {% include "icons/trakt.liquid" %} diff --git a/src/_includes/icons/apple-music.liquid b/src/_includes/icons/apple-music.liquid new file mode 100644 index 00000000..e3db63c6 --- /dev/null +++ b/src/_includes/icons/apple-music.liquid @@ -0,0 +1,13 @@ +{% if site.applemusic != "" %} + + Apple Music + + + +{% endif %} \ No newline at end of file diff --git a/src/_includes/icons/lastfm.liquid b/src/_includes/icons/lastfm.liquid deleted file mode 100644 index be4508a5..00000000 --- a/src/_includes/icons/lastfm.liquid +++ /dev/null @@ -1,13 +0,0 @@ -{% if site.lastfm != "" %} - - - Last.fm - -{% endif %} \ No newline at end of file diff --git a/src/_includes/now.liquid b/src/_includes/now.liquid index 545feaf8..d486b9fa 100644 --- a/src/_includes/now.liquid +++ b/src/_includes/now.liquid @@ -2,191 +2,185 @@ layout: main --- {% include "header.liquid" %} -