chore: last.fm -> apple music
This commit is contained in:
parent
fe4388d5fb
commit
80b2f7da77
6 changed files with 125 additions and 61 deletions
3
.env
3
.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=
|
||||
|
|
|
@ -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: '🎧',
|
||||
})
|
||||
}
|
||||
|
||||
|
|
104
src/_data/music.js
Normal file
104
src/_data/music.js
Normal file
|
@ -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
|
||||
}
|
|
@ -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' %}
|
||||
|
|
|
@ -3,6 +3,6 @@
|
|||
{% tablericon 'loader-2' 'Loading...' %}
|
||||
</span>
|
||||
<span id="now-playing-display">
|
||||
<span id="now-playing-emoji"></span> <a href id="now-playing-content"></a>
|
||||
<span id="now-playing-emoji"></span> <span id="now-playing-content"></span>
|
||||
</span>
|
||||
</p>
|
|
@ -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')
|
||||
|
|
Reference in a new issue