feat: normalize data _before_ aggregating
This commit is contained in:
parent
0c07436bff
commit
ecaf56ea43
1 changed files with 36 additions and 25 deletions
|
@ -1,20 +1,29 @@
|
||||||
const { AssetCache } = require('@11ty/eleventy-fetch')
|
const { AssetCache } = require('@11ty/eleventy-fetch')
|
||||||
const artistAliases = require('./json/artist-aliases.json')
|
const artistAliases = require('./json/artist-aliases.json')
|
||||||
|
|
||||||
const aliasArtists = (array) => {
|
const aliasArtist = (artist) => {
|
||||||
array.forEach((a) => {
|
const aliased = artistAliases.aliases.find((alias) => alias.aliases.includes(artist))
|
||||||
const aliased = artistAliases.aliases.find((alias) => alias.aliases.includes(a.artist))
|
if (aliased) artist = aliased.artist
|
||||||
if (aliased) a.artist = aliased.artist
|
return artist
|
||||||
})
|
|
||||||
return array
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const sanitizeAlbums = (array) => {
|
const sanitizeAlbum = (album) => {
|
||||||
const denyList = /(\[|\()(Deluxe Edition|Special Edition|Remastered)(\]|\))/i
|
const denyList = /(\[|\()(Deluxe Edition|Special Edition|Remastered)(\]|\))/i
|
||||||
array.forEach((a) => {
|
return album.replace(denyList, '')
|
||||||
a.name = a.name.replace(denyList, '')
|
}
|
||||||
})
|
|
||||||
return array
|
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)
|
const sort = (array) => Object.values(array).sort((a, b) => b.plays - a.plays)
|
||||||
|
@ -66,42 +75,44 @@ module.exports = async function () {
|
||||||
CURRENT_PAGE++
|
CURRENT_PAGE++
|
||||||
}
|
}
|
||||||
res.forEach((track) => {
|
res.forEach((track) => {
|
||||||
if (!response.artists[track.attributes['artistName']]) {
|
const artist = titleCase(aliasArtist(track.attributes['artistName']))
|
||||||
response.artists[track.attributes['artistName']] = {
|
const album = titleCase(sanitizeAlbum(track.attributes['albumName']))
|
||||||
artist: track.attributes['artistName'],
|
if (!response.artists[artist]) {
|
||||||
|
response.artists[artist] = {
|
||||||
|
artist,
|
||||||
plays: 1,
|
plays: 1,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
response.artists[track.attributes['artistName']].plays++
|
response.artists[artist].plays++
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.albums[track.attributes['albumName']]) {
|
if (!response.albums[album]) {
|
||||||
response.albums[track.attributes['albumName']] = {
|
response.albums[album] = {
|
||||||
name: track.attributes['albumName'],
|
name: album,
|
||||||
artist: track.attributes['artistName'],
|
artist,
|
||||||
art: track.attributes.artwork.url.replace('{w}', '300').replace('{h}', '300'),
|
art: track.attributes.artwork.url.replace('{w}', '300').replace('{h}', '300'),
|
||||||
url: track['relationships']
|
url: track['relationships']
|
||||||
? `https://song.link/${track['relationships'].albums.data.pop().attributes.url}`
|
? `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,
|
plays: 1,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
response.albums[track.attributes['albumName']].plays++
|
response.albums[album].plays++
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.tracks[track.attributes.name]) {
|
if (!response.tracks[track.attributes.name]) {
|
||||||
response.tracks[track.attributes.name] = {
|
response.tracks[track.attributes.name] = {
|
||||||
name: track.attributes.name,
|
name: track.attributes.name,
|
||||||
artist: track.attributes['artistName'],
|
artist,
|
||||||
plays: 1,
|
plays: 1,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
response.tracks[track.attributes.name].plays++
|
response.tracks[track.attributes.name].plays++
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
response.artists = aliasArtists(sort(response.artists)).splice(0, 8)
|
response.artists = sort(response.artists).splice(0, 8)
|
||||||
response.albums = sanitizeAlbums(aliasArtists(sort(response.albums)).splice(0, 8))
|
response.albums = sort(response.albums).splice(0, 8)
|
||||||
response.tracks = aliasArtists(sort(response.tracks)).splice(0, 5)
|
response.tracks = sort(response.tracks).splice(0, 5)
|
||||||
await asset.save(response, 'json')
|
await asset.save(response, 'json')
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue