feat: normalize data _before_ aggregating

This commit is contained in:
Cory Dransfeldt 2023-07-17 19:15:59 -07:00
parent 0c07436bff
commit ecaf56ea43
No known key found for this signature in database

View file

@ -1,20 +1,29 @@
const { AssetCache } = require('@11ty/eleventy-fetch')
const artistAliases = require('./json/artist-aliases.json')
const aliasArtists = (array) => {
array.forEach((a) => {
const aliased = artistAliases.aliases.find((alias) => alias.aliases.includes(a.artist))
if (aliased) a.artist = aliased.artist
})
return array
const aliasArtist = (artist) => {
const aliased = artistAliases.aliases.find((alias) => alias.aliases.includes(artist))
if (aliased) artist = aliased.artist
return artist
}
const sanitizeAlbums = (array) => {
const sanitizeAlbum = (album) => {
const denyList = /(\[|\()(Deluxe Edition|Special Edition|Remastered)(\]|\))/i
array.forEach((a) => {
a.name = a.name.replace(denyList, '')
})
return array
return album.replace(denyList, '')
}
const titleCase = (string) => {
const exceptions = ['of', 'the', 'and']
if (!string) return ''
return string
.toLowerCase()
.split(' ')
.map((word, i) => {
return exceptions.includes(word) && i !== 0
? word
: word.charAt(0).toUpperCase().concat(word.substring(1))
})
.join(' ')
}
const sort = (array) => Object.values(array).sort((a, b) => b.plays - a.plays)
@ -66,42 +75,44 @@ module.exports = async function () {
CURRENT_PAGE++
}
res.forEach((track) => {
if (!response.artists[track.attributes['artistName']]) {
response.artists[track.attributes['artistName']] = {
artist: track.attributes['artistName'],
const artist = titleCase(aliasArtist(track.attributes['artistName']))
const album = titleCase(sanitizeAlbum(track.attributes['albumName']))
if (!response.artists[artist]) {
response.artists[artist] = {
artist,
plays: 1,
}
} else {
response.artists[track.attributes['artistName']].plays++
response.artists[artist].plays++
}
if (!response.albums[track.attributes['albumName']]) {
response.albums[track.attributes['albumName']] = {
name: track.attributes['albumName'],
artist: track.attributes['artistName'],
if (!response.albums[album]) {
response.albums[album] = {
name: album,
artist,
art: track.attributes.artwork.url.replace('{w}', '300').replace('{h}', '300'),
url: track['relationships']
? `https://song.link/${track['relationships'].albums.data.pop().attributes.url}`
: `https://rateyourmusic.com/search?searchtype=l&searchterm=${track.attributes['albumName']}%20${track.attributes['artistName']}`,
: `https://rateyourmusic.com/search?searchtype=l&searchterm=${album}%20${artist}`,
plays: 1,
}
} else {
response.albums[track.attributes['albumName']].plays++
response.albums[album].plays++
}
if (!response.tracks[track.attributes.name]) {
response.tracks[track.attributes.name] = {
name: track.attributes.name,
artist: track.attributes['artistName'],
artist,
plays: 1,
}
} else {
response.tracks[track.attributes.name].plays++
}
})
response.artists = aliasArtists(sort(response.artists)).splice(0, 8)
response.albums = sanitizeAlbums(aliasArtists(sort(response.albums)).splice(0, 8))
response.tracks = aliasArtists(sort(response.tracks)).splice(0, 5)
response.artists = sort(response.artists).splice(0, 8)
response.albums = sort(response.albums).splice(0, 8)
response.tracks = sort(response.tracks).splice(0, 5)
await asset.save(response, 'json')
return response
}