From c6f9ec7ced7c10dfb92222cde41e0b522a204edf Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sat, 30 Mar 2024 09:12:06 -0700 Subject: [PATCH] chore: url fixes --- api/scrobble.js | 15 ++++++++++----- src/_data/albums.js | 4 ++-- src/_data/artists.js | 2 +- src/_data/helpers/music.js | 9 +++++++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/api/scrobble.js b/api/scrobble.js index a2651b6c..e0feccc3 100644 --- a/api/scrobble.js +++ b/api/scrobble.js @@ -3,8 +3,13 @@ import { DateTime } from 'luxon' const sanitizeMediaString = (string) => { const normalizedStr = string.normalize('NFD'); - return normalizedStr.replace(/[\u0300-\u036f]/g, '').replace(/\.{3}/g, ''); -}; + return normalizedStr + .replace(/[\u0300-\u036f]/g, '') + .replace(/[\u2010]/g, '-') + .replace(/\.{3}/g, '') + .replace(/\?/g, '') + .replace(/[\(\)\[\]\{\}]/g, '') +} const weekStop = () => { const currentDate = DateTime.now() @@ -59,7 +64,7 @@ export default async (request) => { // if there is no artist blob, populate one if (!artistInfo) { const artistRes = await fetch( - `https://ws.audioscrobbler.com/2.0/?method=artist.getInfo&api_key=${MUSIC_KEY}&artist=${encodeURIComponent(sanitizeMediaString(artist).replace(/\s+/g, '+').toLowerCase())}&format=json`, + `https://ws.audioscrobbler.com/2.0/?method=artist.getInfo&api_key=${MUSIC_KEY}&artist=${sanitizeMediaString(artist).replace(/\s+/g, '+').toLowerCase()}&format=json`, { type: "json", } @@ -123,8 +128,8 @@ export default async (request) => { const mbid = albumRes['album']['mbid'] || '' const albumObj = { mbid, - image: `https://cdn.coryd.dev/albums/${encodeURIComponent(sanitizeMediaString(artist).replace(/\s+/g, '-').toLowerCase())}-${encodeURIComponent(sanitizeMediaString(album.replace(/[:\/\\,'']+/g - , '').replace(/\s+/g, '-').toLowerCase()))}.jpg` + image: `https://cdn.coryd.dev/albums/${sanitizeMediaString(artist).replace(/\s+/g, '-').toLowerCase()}-${sanitizeMediaString(album.replace(/[:\/\\,'']+/g + , '').replace(/\s+/g, '-').toLowerCase())}.jpg` } await albums.setJSON(albumKey, albumObj) } diff --git a/src/_data/albums.js b/src/_data/albums.js index a162756c..da630611 100644 --- a/src/_data/albums.js +++ b/src/_data/albums.js @@ -10,8 +10,8 @@ export default async function () { artist: artistCapitalization(album['artist']['name']), plays: album['playcount'], rank: album['@attr']['rank'], - image: `https://cdn.coryd.dev/albums/${encodeURIComponent(sanitizeMediaString(album['artist']['name']).replace(/\s+/g, '-').toLowerCase())}-${encodeURIComponent(sanitizeMediaString(album['name'].replace(/[:\/\\,'']+/g - , '').replace(/\s+/g, '-').toLowerCase()))}.jpg`, + image: `https://cdn.coryd.dev/albums/${sanitizeMediaString(album['artist']['name']).replace(/\s+/g, '-').toLowerCase()}-${sanitizeMediaString(album['name'].replace(/[:\/\\,'']+/g + , '').replace(/\s+/g, '-').toLowerCase())}.jpg`, url: album['mbid'] ? `https://musicbrainz.org/album/${album['mbid']}` : `https://musicbrainz.org/taglookup/index?tag-lookup.artist=${album['artist'][ diff --git a/src/_data/artists.js b/src/_data/artists.js index 91827a8a..a0f7a605 100644 --- a/src/_data/artists.js +++ b/src/_data/artists.js @@ -14,7 +14,7 @@ export default async function () { title: artistCapitalization(artist['name']), plays: artist['playcount'], rank: artist['@attr']['rank'], - image: `https://cdn.coryd.dev/artists/${encodeURIComponent(sanitizeMediaString(artist['name']).replace(/\s+/g, '-').toLowerCase())}.jpg`, + image: `https://cdn.coryd.dev/artists/${sanitizeMediaString(artist['name']).replace(/\s+/g, '-').toLowerCase()}.jpg`, url: mbid ? `https://musicbrainz.org/artist/${mbid}` : `https://musicbrainz.org/search?query=${artist['name'].replace( diff --git a/src/_data/helpers/music.js b/src/_data/helpers/music.js index b9a69dff..a365bdce 100644 --- a/src/_data/helpers/music.js +++ b/src/_data/helpers/music.js @@ -5,7 +5,12 @@ export const artistCapitalization = (artist) => artistCapitalizationPatches[arti export const sanitizeMediaString = (string) => { const normalizedStr = string.normalize('NFD'); - return normalizedStr.replace(/[\u0300-\u036f]/g, '').replace(/\.{3}/g, ''); -}; + return normalizedStr + .replace(/[\u0300-\u036f]/g, '') + .replace(/[\u2010]/g, '-') + .replace(/\.{3}/g, '') + .replace(/\?/g, '') + .replace(/[\(\)\[\]\{\}]/g, '') +} export const mbidMap = (artist) => mbidPatches[artist.toLowerCase()] || '' \ No newline at end of file