feat: assorted music fixes
This commit is contained in:
parent
1c46e9b3e2
commit
521bd5d879
3 changed files with 78 additions and 32 deletions
20
src/_data/json/artist-aliases.json
Normal file
20
src/_data/json/artist-aliases.json
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"aliases": [
|
||||||
|
{
|
||||||
|
"artist": "Aesop Rock",
|
||||||
|
"aliases": ["Aesop Rock & Homeboy Sandman", "Aesop Rock & Blockhead"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"artist": "Fen",
|
||||||
|
"aliases": ["Sleepwalker & Fen"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"artist": "Osees",
|
||||||
|
"aliases": ["OCS", "The Ohsees", "Thee Oh Sees", "Thee Oh See's"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"artist": "Tom Waits",
|
||||||
|
"aliases": ["Tom Waits & Crystal Gayle", "Crystal Gayle"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -1,9 +1,5 @@
|
||||||
const { AssetCache } = require('@11ty/eleventy-fetch')
|
const { AssetCache } = require('@11ty/eleventy-fetch')
|
||||||
|
const { aliasArtist, sanitizeMedia, sortByPlays } = require('../utils/media')
|
||||||
const sortTrim = (array, length = 8) =>
|
|
||||||
Object.values(array)
|
|
||||||
.sort((a, b) => b.plays - a.plays)
|
|
||||||
.splice(0, length)
|
|
||||||
|
|
||||||
module.exports = async function () {
|
module.exports = async function () {
|
||||||
const API_APPLE_MUSIC_DEVELOPER_TOKEN = process.env.API_APPLE_MUSIC_DEVELOPER_TOKEN
|
const API_APPLE_MUSIC_DEVELOPER_TOKEN = process.env.API_APPLE_MUSIC_DEVELOPER_TOKEN
|
||||||
|
@ -15,7 +11,6 @@ module.exports = async function () {
|
||||||
const response = {
|
const response = {
|
||||||
artists: {},
|
artists: {},
|
||||||
albums: {},
|
albums: {},
|
||||||
tracks: {},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const RENEWED_MUSIC_TOKEN = await fetch(APPLE_RENEW_TOKEN_URL, {
|
const RENEWED_MUSIC_TOKEN = await fetch(APPLE_RENEW_TOKEN_URL, {
|
||||||
|
@ -53,11 +48,10 @@ module.exports = async function () {
|
||||||
res.forEach((track) => {
|
res.forEach((track) => {
|
||||||
if (!response['artists'][track['attributes']['artistName']]) {
|
if (!response['artists'][track['attributes']['artistName']]) {
|
||||||
response['artists'][track['attributes']['artistName']] = {
|
response['artists'][track['attributes']['artistName']] = {
|
||||||
title: track['attributes']['artistName'],
|
title: aliasArtist(track['attributes']['artistName']),
|
||||||
image:
|
image: `https://cdn.coryd.dev/artists/${track['attributes']['artistName']
|
||||||
`https://cdn.coryd.dev/artists/${track['attributes']['artistName']
|
.replace(/\s+/g, '-')
|
||||||
.replace(/\s+/g, '-')
|
.toLowerCase()}.jpg`,
|
||||||
.toLowerCase()}.jpg` || 'https://cdn.coryd.dev/artists/missing-artist.jpg',
|
|
||||||
url: `https://musicbrainz.org/search?query=${track['attributes']['artistName'].replace(
|
url: `https://musicbrainz.org/search?query=${track['attributes']['artistName'].replace(
|
||||||
/\s+/g,
|
/\s+/g,
|
||||||
'+'
|
'+'
|
||||||
|
@ -72,35 +66,26 @@ module.exports = async function () {
|
||||||
// aggregate albums
|
// aggregate albums
|
||||||
if (!response.albums[track['attributes']['albumName']]) {
|
if (!response.albums[track['attributes']['albumName']]) {
|
||||||
response.albums[track['attributes']['albumName']] = {
|
response.albums[track['attributes']['albumName']] = {
|
||||||
title: track['attributes']['albumName'],
|
title: sanitizeMedia(track['attributes']['albumName']),
|
||||||
artist: track['attributes']['artistName'],
|
artist: aliasArtist(track['attributes']['artistName']),
|
||||||
image: track['attributes']['artwork']['url'].replace('{w}', '500').replace('{h}', '500'),
|
image: track['attributes']['artwork']['url'].replace('{w}', '500').replace('{h}', '500'),
|
||||||
url: `https://musicbrainz.org/taglookup/index?tag-lookup.artist=${track['attributes'][
|
url:
|
||||||
'artistName'
|
track['relationships'] && track['relationships'].albums.data.length > 0
|
||||||
].replace(/\s+/g, '+')}&tag-lookup.release=${track['attributes']['albumName'].replace(
|
? `https://song.link/${track['relationships'].albums.data.pop().attributes.url}`
|
||||||
/\s+/g,
|
: `https://musicbrainz.org/taglookup/index?tag-lookup.artist=${track['attributes'][
|
||||||
'+'
|
'artistName'
|
||||||
)}`,
|
].replace(/\s+/g, '+')}&tag-lookup.release=${sanitizeMedia(
|
||||||
|
track['attributes']['albumName']
|
||||||
|
).replace(/\s+/g, '+')}`,
|
||||||
plays: 1,
|
plays: 1,
|
||||||
type: 'album',
|
type: 'album',
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
response.albums[track['attributes']['albumName']].plays++
|
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.artists = sortByPlays(response.artists)
|
||||||
response.albums = sortTrim(response.albums)
|
response.albums = sortByPlays(response.albums)
|
||||||
response.tracks = sortTrim(response.tracks, 5)
|
|
||||||
await asset.save(response, 'json')
|
await asset.save(response, 'json')
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
41
src/utils/media.js
Normal file
41
src/utils/media.js
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
const artistAliases = require('../_data/json/artist-aliases.json')
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
/**
|
||||||
|
* Accepts a string representing an artist name, checks to see if said artist name
|
||||||
|
* exists in an artist alias group of shape string[]. If so, replaces the provided
|
||||||
|
* artist name with the canonical artist name.
|
||||||
|
*
|
||||||
|
* @name aliasArtist
|
||||||
|
* @param {string} artist
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
aliasArtist: (artist) => {
|
||||||
|
const aliased = artistAliases.aliases.find((alias) => alias.aliases.includes(artist))
|
||||||
|
if (aliased) artist = aliased.artist
|
||||||
|
return artist
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accepts a media name represented as a string (album or song name) and replaces
|
||||||
|
* matches in the `denyList` with an empty string before returning the result.
|
||||||
|
*
|
||||||
|
* @name sanitizeMedia
|
||||||
|
* @param {string} media
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
sanitizeMedia: (media) => {
|
||||||
|
const denyList =
|
||||||
|
/-\s*(?:single|ep)\s*|(\[|\()(Deluxe Edition|Special Edition|Remastered|Full Dynamic Range Edition|Anniversary Edition|Expanded Edition)(\]|\))/gi
|
||||||
|
return media.replace(denyList, '').trim()
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts an array of media objects by the number of plays in descending order.
|
||||||
|
*
|
||||||
|
* @name sortByPlays
|
||||||
|
* @param {Array} array - an array of media objects.
|
||||||
|
* @returns {Array} - a new array sorted by the number of plays in descending order.
|
||||||
|
*/
|
||||||
|
sortByPlays: (array) => Object.values(array).sort((a, b) => b.plays - a.plays),
|
||||||
|
}
|
Reference in a new issue