feat: beta scrobble to site
This commit is contained in:
parent
178546935c
commit
05dfeb612e
5 changed files with 40 additions and 22 deletions
|
@ -6,6 +6,7 @@ const sanitizeMediaString = (string) => {
|
||||||
return normalizedStr
|
return normalizedStr
|
||||||
.replace(/[\u0300-\u036f]/g, '')
|
.replace(/[\u0300-\u036f]/g, '')
|
||||||
.replace(/[\u2010]/g, '-')
|
.replace(/[\u2010]/g, '-')
|
||||||
|
.replace(/—/g, '-')
|
||||||
.replace(/\.{3}/g, '')
|
.replace(/\.{3}/g, '')
|
||||||
.replace(/\?/g, '')
|
.replace(/\?/g, '')
|
||||||
.replace(/[\(\)\[\]\{\}]/g, '')
|
.replace(/[\(\)\[\]\{\}]/g, '')
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "8.7.12",
|
"version": "9.0.0",
|
||||||
"description": "The source for my personal site. Built using 11ty.",
|
"description": "The source for my personal site. Built using 11ty.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
@ -8,6 +8,7 @@ export const sanitizeMediaString = (string) => {
|
||||||
return normalizedStr
|
return normalizedStr
|
||||||
.replace(/[\u0300-\u036f]/g, '')
|
.replace(/[\u0300-\u036f]/g, '')
|
||||||
.replace(/[\u2010]/g, '-')
|
.replace(/[\u2010]/g, '-')
|
||||||
|
.replace(/—/g, '-')
|
||||||
.replace(/\.{3}/g, '')
|
.replace(/\.{3}/g, '')
|
||||||
.replace(/\?/g, '')
|
.replace(/\?/g, '')
|
||||||
.replace(/[\(\)\[\]\{\}]/g, '')
|
.replace(/[\(\)\[\]\{\}]/g, '')
|
||||||
|
@ -15,35 +16,55 @@ export const sanitizeMediaString = (string) => {
|
||||||
|
|
||||||
export const mbidMap = (artist) => mbidPatches[artist.toLowerCase()] || ''
|
export const mbidMap = (artist) => mbidPatches[artist.toLowerCase()] || ''
|
||||||
|
|
||||||
export const buildChart = (tracks) => {
|
export const buildChart = (tracks, artists, albums) => {
|
||||||
const artistsData = {}
|
const artistsData = {}
|
||||||
const albumsData = {}
|
const albumsData = {}
|
||||||
const tracksData = {}
|
const tracksData = {}
|
||||||
|
const artistSanitizedKey = (artist) => `${sanitizeMediaString(artist).replace(/\s+/g, '-').toLowerCase()}`
|
||||||
const objectToArraySorted = (inputObject) => {
|
const albumSanitizedKey = (album) => `${sanitizeMediaString(album).replace(/\s+/g, '-').toLowerCase()}-${sanitizeMediaString(album.replace(/[:\/\\,'']+/g
|
||||||
return Object.keys(inputObject).map(key => ({
|
, '').replace(/\s+/g, '-').toLowerCase())}`
|
||||||
name: key,
|
const objectToArraySorted = (inputObject) => Object.values(inputObject).sort((a, b) => b.plays - a.plays)
|
||||||
count: inputObject[key]
|
|
||||||
})).sort((a, b) => b.count - a.count);
|
|
||||||
};
|
|
||||||
|
|
||||||
tracks.forEach(track => {
|
tracks.forEach(track => {
|
||||||
if (!tracksData[track['track']]) {
|
if (!tracksData[track['track']]) {
|
||||||
tracksData[track['track']] = 1
|
tracksData[track['track']] = {
|
||||||
|
title: track['track'],
|
||||||
|
plays: 1,
|
||||||
|
type: 'track'
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
tracksData[track['track']]++
|
tracksData[track['track']]['plays']++
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!artistsData[artistCapitalization(track['artist'])]) {
|
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 {
|
} else {
|
||||||
artistsData[artistCapitalization(track['artist'])]++
|
artistsData[artistCapitalization(track['artist'])]['plays']++
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!albumsData[track['album']]) {
|
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 {
|
} else {
|
||||||
albumsData[track['album']]++
|
albumsData[track['album']]['plays']++
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -9,9 +9,5 @@ export default async function () {
|
||||||
type: 'json',
|
type: 'json',
|
||||||
}).catch();
|
}).catch();
|
||||||
const resObj = await res;
|
const resObj = await res;
|
||||||
return {
|
return buildChart(resObj['scrobbles'], resObj['artists'], resObj['albums'])
|
||||||
charts: buildChart(resObj['scrobbles']),
|
|
||||||
artists: resObj['artists'],
|
|
||||||
albums: resObj['albums']
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@ layout: default
|
||||||
{% endcapture %}
|
{% endcapture %}
|
||||||
<style>{{ css }}</style>
|
<style>{{ css }}</style>
|
||||||
{{ content }}
|
{{ 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:music.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.albums, icon: "vinyl", title: "Albums", shape: "square", count: 8 %}
|
||||||
{% render "partials/now/albumReleases.liquid", albumReleases:albumReleases %}
|
{% 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/now/media-grid.liquid", data:books, icon: "books", title: "Books", shape: "vertical", count: 6 %}
|
||||||
{% render "partials/recent-links.liquid", links:links %}
|
{% render "partials/recent-links.liquid", links:links %}
|
||||||
|
|
Reference in a new issue