From 05dfeb612ee95d7fc760536f847fbc07be43ff72 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sat, 30 Mar 2024 21:51:12 -0700 Subject: [PATCH] feat: beta scrobble to site --- api/scrobble.js | 1 + package.json | 2 +- src/_data/helpers/music.js | 49 +++++++++++++++++++++++++++----------- src/_data/music.js | 6 +---- src/_includes/now.liquid | 4 ++-- 5 files changed, 40 insertions(+), 22 deletions(-) diff --git a/api/scrobble.js b/api/scrobble.js index 29eb9581..11d13295 100644 --- a/api/scrobble.js +++ b/api/scrobble.js @@ -6,6 +6,7 @@ const sanitizeMediaString = (string) => { return normalizedStr .replace(/[\u0300-\u036f]/g, '') .replace(/[\u2010]/g, '-') + .replace(/—/g, '-') .replace(/\.{3}/g, '') .replace(/\?/g, '') .replace(/[\(\)\[\]\{\}]/g, '') diff --git a/package.json b/package.json index 4e97df17..bb9a4a14 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "8.7.12", + "version": "9.0.0", "description": "The source for my personal site. Built using 11ty.", "type": "module", "scripts": { diff --git a/src/_data/helpers/music.js b/src/_data/helpers/music.js index a8de70cd..97b2310d 100644 --- a/src/_data/helpers/music.js +++ b/src/_data/helpers/music.js @@ -8,6 +8,7 @@ export const sanitizeMediaString = (string) => { return normalizedStr .replace(/[\u0300-\u036f]/g, '') .replace(/[\u2010]/g, '-') + .replace(/—/g, '-') .replace(/\.{3}/g, '') .replace(/\?/g, '') .replace(/[\(\)\[\]\{\}]/g, '') @@ -15,35 +16,55 @@ export const sanitizeMediaString = (string) => { export const mbidMap = (artist) => mbidPatches[artist.toLowerCase()] || '' -export const buildChart = (tracks) => { +export const buildChart = (tracks, artists, albums) => { const artistsData = {} const albumsData = {} const tracksData = {} - - const objectToArraySorted = (inputObject) => { - return Object.keys(inputObject).map(key => ({ - name: key, - count: inputObject[key] - })).sort((a, b) => b.count - a.count); - }; + const artistSanitizedKey = (artist) => `${sanitizeMediaString(artist).replace(/\s+/g, '-').toLowerCase()}` + const albumSanitizedKey = (album) => `${sanitizeMediaString(album).replace(/\s+/g, '-').toLowerCase()}-${sanitizeMediaString(album.replace(/[:\/\\,'']+/g + , '').replace(/\s+/g, '-').toLowerCase())}` + const objectToArraySorted = (inputObject) => Object.values(inputObject).sort((a, b) => b.plays - a.plays) tracks.forEach(track => { if (!tracksData[track['track']]) { - tracksData[track['track']] = 1 + tracksData[track['track']] = { + title: track['track'], + plays: 1, + type: 'track' + } } else { - tracksData[track['track']]++ + tracksData[track['track']]['plays']++ } if (!artistsData[artistCapitalization(track['artist'])]) { - artistsData[artistCapitalization(track['artist'])] = 1 + artistsData[artistCapitalization(track['artist'])] = { + title: artistCapitalization(track['artist']), + plays: 1, + mbid: artists[artistSanitizedKey(track['artist'])]?.['mbid'] || '', + url: artists[artistSanitizedKey(track['artist'])]?.['url'] || `https://musicbrainz.org/search?query=${track['artist'].replace( + /\s+/g, + '+' + )}&type=artist`, + image: artists[artistSanitizedKey(track['artist'])]?.['image'] || `https://cdn.coryd.dev/artists/${sanitizeMediaString(track['artist']).replace(/\s+/g, '-').toLowerCase()}.jpg`, + type: 'artist' + } } else { - artistsData[artistCapitalization(track['artist'])]++ + artistsData[artistCapitalization(track['artist'])]['plays']++ } if (!albumsData[track['album']]) { - albumsData[track['album']] = 1 + albumsData[track['album']] = { + title: track['album'], + artist: track['artist'], + plays: 1, + mbid: albums[albumSanitizedKey(track['album'])]?.['mbid'] || '', + url: albums[albumSanitizedKey(track['album'])]?.['url'] || `https://musicbrainz.org/taglookup/index?tag-lookup.artist=${track['artist'].replace(/\s+/g, '+')}&tag-lookup.release=${track['album'].replace(/\s+/g, '+')}`, + image: albums[albumSanitizedKey(track['album'])]?.['image'] || `https://cdn.coryd.dev/albums/${sanitizeMediaString(track['artist']).replace(/\s+/g, '-').toLowerCase()}-${sanitizeMediaString(track['album'].replace(/[:\/\\,'']+/g + , '').replace(/\s+/g, '-').toLowerCase())}.jpg`, + type: 'album' + } } else { - albumsData[track['album']]++ + albumsData[track['album']]['plays']++ } }) diff --git a/src/_data/music.js b/src/_data/music.js index cd582cee..085d0aff 100644 --- a/src/_data/music.js +++ b/src/_data/music.js @@ -9,9 +9,5 @@ export default async function () { type: 'json', }).catch(); const resObj = await res; - return { - charts: buildChart(resObj['scrobbles']), - artists: resObj['artists'], - albums: resObj['albums'] - } + return buildChart(resObj['scrobbles'], resObj['artists'], resObj['albums']) } diff --git a/src/_includes/now.liquid b/src/_includes/now.liquid index d62e7b7c..6c0f3306 100644 --- a/src/_includes/now.liquid +++ b/src/_includes/now.liquid @@ -8,8 +8,8 @@ layout: default {% endcapture %} {{ content }} -{% render "partials/now/media-grid.liquid", data:artists, icon: "microphone-2", title: "Artists", shape: "square", count: 8, loading: "eager" %} -{% render "partials/now/media-grid.liquid", data:albums, icon: "vinyl", title: "Albums", shape: "square", count: 8 %} +{% render "partials/now/media-grid.liquid", data:music.artists, icon: "microphone-2", title: "Artists", shape: "square", count: 8, loading: "eager" %} +{% render "partials/now/media-grid.liquid", data:music.albums, icon: "vinyl", title: "Albums", shape: "square", count: 8 %} {% render "partials/now/albumReleases.liquid", albumReleases:albumReleases %} {% render "partials/now/media-grid.liquid", data:books, icon: "books", title: "Books", shape: "vertical", count: 6 %} {% render "partials/recent-links.liquid", links:links %}