feat: beta scrobble to site

This commit is contained in:
Cory Dransfeldt 2024-03-30 21:51:12 -07:00
parent 178546935c
commit 05dfeb612e
No known key found for this signature in database
5 changed files with 40 additions and 22 deletions

View file

@ -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, '')

View file

@ -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": {

View file

@ -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']++
}
})

View file

@ -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'])
}

View file

@ -8,8 +8,8 @@ layout: default
{% endcapture %}
<style>{{ css }}</style>
{{ 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 %}