fix: handle diacritics

This commit is contained in:
Cory Dransfeldt 2024-01-26 17:52:37 -08:00
parent 9093a222d6
commit 35c34323d9
No known key found for this signature in database

View file

@ -1,35 +1,43 @@
import EleventyFetch from '@11ty/eleventy-fetch' import EleventyFetch from '@11ty/eleventy-fetch';
import mbidPatches from './json/mbid-patches.js' import mbidPatches from './json/mbid-patches.js';
const mbidMap = (artist) => { const mbidMap = (artist) => {
return mbidPatches[artist.toLowerCase()] || '' return mbidPatches[artist.toLowerCase()] || '';
} };
const removeAccents = (inputStr) => {
const normalizedStr = inputStr.normalize('NFD');
return normalizedStr.replace(/[\u0300-\u036f]/g, '');
};
export default async function () { export default async function () {
const MUSIC_KEY = process.env.API_KEY_LASTFM const MUSIC_KEY = process.env.API_KEY_LASTFM;
const url = `https://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=coryd_&api_key=${MUSIC_KEY}&limit=8&format=json&period=7day` const url = `https://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=coryd_&api_key=${MUSIC_KEY}&limit=8&format=json&period=7day`;
const res = EleventyFetch(url, { const res = EleventyFetch(url, {
duration: '1h', duration: '1h',
type: 'json', type: 'json',
}).catch() }).catch();
const data = await res const data = await res;
return data['topartists']['artist'].map((artist) => { return data['topartists']['artist'].map((artist) => {
let mbid = artist['mbid'] let mbid = artist['mbid'];
// mbid mismatches // mbid mismatches
if (mbidMap(artist['name']) !== '') mbid = mbidMap(artist['name']) if (mbidMap(artist['name']) !== '') mbid = mbidMap(artist['name']);
return { return {
title: artist['name'], title: artist['name'],
plays: artist['playcount'], plays: artist['playcount'],
rank: artist['@attr']['rank'], rank: artist['@attr']['rank'],
image: image: `https://cdn.coryd.dev/artists/${removeAccents(artist['name'])
`https://cdn.coryd.dev/artists/${artist['name'].replace(/\s+/g, '-').toLowerCase()}.jpg` || .replace(/\s+/g, '-')
'https://cdn.coryd.dev/artists/missing-artist.jpg', .toLowerCase()}.jpg`,
url: mbid url: mbid
? `https://musicbrainz.org/artist/${mbid}` ? `https://musicbrainz.org/artist/${mbid}`
: `https://musicbrainz.org/search?query=${artist['name'].replace(/\s+/g, '+')}&type=artist`, : `https://musicbrainz.org/search?query=${artist['name'].replace(
/\s+/g,
'+'
)}&type=artist`,
type: 'artist', type: 'artist',
} };
}) });
} }