fix: helpers

This commit is contained in:
Cory Dransfeldt 2024-05-23 19:24:19 -07:00
parent d2156c9678
commit 7dfa8d5081
No known key found for this signature in database
2 changed files with 12 additions and 8 deletions

View file

@ -141,7 +141,7 @@ export default async () => {
content: `${emojiMap(
scrobbleData.artists.genre,
scrobbleData.artist_name
)} ${scrobbleData.track_name} by <a href="https://coryd.dev/music/artists/${slugifyString(scrobbleData.artist_name)}-${slugifyString(parseCountryField(scrobbleData.country))}">${
)} ${scrobbleData.track_name} by <a href="https://coryd.dev/music/artists/${sanitizeMediaString(scrobbleData.artist_name)}-${sanitizeMediaString(parseCountryField(scrobbleData.country))}">${
scrobbleData.artist_name
}</a>`,
}), { headers });

View file

@ -6,11 +6,15 @@ const SUPABASE_URL = process.env.SUPABASE_URL
const SUPABASE_KEY = process.env.SUPABASE_KEY
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
const slugifyString = (str) => slugify(str, {
const sanitizeMediaString = (str) => {
const sanitizedString = str.normalize('NFD').replace(/[\u0300-\u036f\u2010—\.\?\(\)\[\]\{\}]/g, '').replace(/\.{3}/g, '')
return slugify(sanitizedString, {
replacement: '-',
remove: /[#,&,+()$~%.'":*?<>{}]/g,
lower: true,
})
}
const regionNames = new Intl.DisplayNames(['en'], { type: 'region' })
const getCountryName = (countryCode) => regionNames.of(countryCode.trim()) || countryCode.trim()
@ -91,7 +95,7 @@ const aggregateData = (data, groupByField, groupByType) => {
title: item[groupByField],
plays: 0,
mbid: item['albums']['mbid'],
url: `https://coryd.dev/music/artists/${slugifyString(item['artist_name'])}-${slugifyString(parseCountryField(item['artists']['country']))}`,
url: `https://coryd.dev/music/artists/${sanitizeMediaString(item['artist_name'])}-${sanitizeMediaString(parseCountryField(item['artists']['country']))}`,
image: item['albums']?.['image'] || '',
timestamp: item['listened_at'],
type: groupByType,
@ -102,7 +106,7 @@ const aggregateData = (data, groupByField, groupByType) => {
title: item[groupByField],
plays: 0,
mbid: item[groupByType]?.['mbid'] || '',
url: `https://coryd.dev/music/artists/${slugifyString(item['artist_name'])}-${slugifyString(parseCountryField(item['artists']['country']))}`,
url: `https://coryd.dev/music/artists/${sanitizeMediaString(item['artist_name'])}-${sanitizeMediaString(parseCountryField(item['artists']['country']))}`,
image: item[groupByType]?.image || '',
type: groupByType,
genre: item['artists']?.['genre'] || ''