chore: drop unused tracks object; add genre but don't display

This commit is contained in:
Cory Dransfeldt 2023-07-24 15:28:25 -07:00
parent 846f41791d
commit ee3870bc6a
No known key found for this signature in database

View file

@ -23,7 +23,6 @@ module.exports = async function () {
const charts = { const charts = {
artists: {}, artists: {},
albums: {}, albums: {},
tracks: {},
} }
let CURRENT_PAGE = 0 let CURRENT_PAGE = 0
let res = [] let res = []
@ -34,7 +33,7 @@ module.exports = async function () {
while (CURRENT_PAGE < PAGES && hasNextPage) { while (CURRENT_PAGE < PAGES && hasNextPage) {
const URL = `https://api.music.apple.com/v1/me/recent/played/tracks?limit=${PAGE_SIZE}&offset=${ const URL = `https://api.music.apple.com/v1/me/recent/played/tracks?limit=${PAGE_SIZE}&offset=${
PAGE_SIZE * CURRENT_PAGE PAGE_SIZE * CURRENT_PAGE
}&include[songs]=albums&extend=artistUrl` }&include[songs]=albums,library&extend=artistUrl`
const tracks = await fetch(URL, { const tracks = await fetch(URL, {
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -51,12 +50,12 @@ module.exports = async function () {
res.forEach((track) => { res.forEach((track) => {
const formattedArtist = titleCase(aliasArtist(track.attributes['artistName'])) const formattedArtist = titleCase(aliasArtist(track.attributes['artistName']))
const formattedAlbum = titleCase(sanitizeMedia(track.attributes['albumName'])) const formattedAlbum = titleCase(sanitizeMedia(track.attributes['albumName']))
const formattedTrack = sanitizeMedia(track.attributes['name'])
if (!charts.artists[formattedArtist]) { if (!charts.artists[formattedArtist]) {
charts.artists[formattedArtist] = { charts.artists[formattedArtist] = {
artist: formattedArtist, artist: formattedArtist,
url: `https://rateyourmusic.com/search?searchterm=${encodeURI(formattedArtist)}`, url: `https://rateyourmusic.com/search?searchterm=${encodeURI(formattedArtist)}`,
genre: track['relationships']?.['library'].data[0]?.attributes['genreNames'][0] || '',
plays: 1, plays: 1,
} }
} else { } else {
@ -78,20 +77,10 @@ module.exports = async function () {
} else { } else {
charts.albums[formattedAlbum].plays++ charts.albums[formattedAlbum].plays++
} }
if (!charts.tracks[formattedTrack]) {
charts.tracks[formattedTrack] = {
name: formattedTrack,
artist: formattedArtist,
plays: 1,
}
} else {
charts.tracks[formattedTrack].plays++
}
}) })
charts.artists = sortByPlays(charts.artists).splice(0, 8) charts.artists = sortByPlays(charts.artists).splice(0, 8)
charts.albums = sortByPlays(charts.albums).splice(0, 8) charts.albums = sortByPlays(charts.albums).splice(0, 8)
charts.tracks = sortByPlays(charts.tracks).splice(0, 5)
await asset.save(charts, 'json') await asset.save(charts, 'json')
return charts return charts
} }