chore: scrobbling

This commit is contained in:
Cory Dransfeldt 2024-03-30 16:31:02 -07:00
parent e9c16f8fbe
commit ffa9ef637a
No known key found for this signature in database
2 changed files with 54 additions and 1 deletions

View file

@ -13,4 +13,43 @@ export const sanitizeMediaString = (string) => {
.replace(/[\(\)\[\]\{\}]/g, '')
}
export const mbidMap = (artist) => mbidPatches[artist.toLowerCase()] || ''
export const mbidMap = (artist) => mbidPatches[artist.toLowerCase()] || ''
export const buildChart = (tracks) => {
const artistsData = {}
const albumsData = {}
const tracksData = {}
const objectToArray = (inputObject) => {
return Object.keys(inputObject).map(key => ({
name: key,
count: inputObject[key]
}));
};
tracks.forEach(track => {
if (!tracksData[track['track']]) {
tracksData[track['track']] = 1
} else {
tracksData[track['track']]++
}
if (!artistsData[track['artist']]) {
artistsData[track['artist']] = 1
} else {
artistsData[track['artist']]++
}
if (!albumsData[track['album']]) {
albumsData[track['album']] = 1
} else {
albumsData[track['album']]++
}
})
return {
artists: objectToArray(artistsData).sort((a, b) => b.count - a.count),
albums: objectToArray(albumsData).sort((a, b) => b.count - a.count),
tracks: objectToArray(tracksData).sort((a, b) => b.count - a.count),
}
}

View file

@ -0,0 +1,14 @@
import EleventyFetch from '@11ty/eleventy-fetch';
import { artistCapitalization, buildChart } from './helpers/music.js'
export default async function () {
const API_KEY_MUSIC = process.env.API_KEY_MUSIC;
const url = `https://coryd.dev/api/music?key=${API_KEY_MUSIC}`;
const res = EleventyFetch(url, {
duration: '1h',
type: 'json',
}).catch();
const resObj = await res;
console.log(buildChart(resObj['data']))
return buildChart(resObj['data'])
}