feat: pull my own artist charts

This commit is contained in:
Cory Dransfeldt 2024-04-08 16:45:39 -07:00
parent 5fb630aaf8
commit 982e479812
No known key found for this signature in database
6 changed files with 27 additions and 58 deletions

View file

@ -1,29 +1,24 @@
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const chartData = require('./json/weekly-artist-charts.json')
const charts = chartData['charts']
import { readFile } from 'fs/promises'
import { buildChart } from './helpers/music.js'
import { DateTime } from 'luxon'
export default async function () {
return charts.map((chart) => {
const artists = chart['weeklyartistchart']['artist'].splice(0, 8)
const date = parseInt(chart['weeklyartistchart']['@attr']['to']) * 1000
let content = 'My top artists for the week: '
artists.forEach((artist, index) => {
const artistName = artist['name'].replace('&', 'and')
content += `${artistName} @ ${artist['playcount']} play${
parseInt(artist['playcount']) > 1 ? 's' : ''
}`
if (index !== artists.length - 1) content += ', '
})
content += ' #Music #LastFM'
return {
title: content,
url: `https://coryd.dev/now?ts=${date}#artists`,
date: new Date(date),
description: `My top artists for the last week, ending ${
new Date(date).toLocaleString().split(',')[0]
}.<br/><br/>`,
}
const currentDate = DateTime.now()
const artists = JSON.parse(await readFile('./src/_data/json/artists-map.json', 'utf8'));
const albums = JSON.parse(await readFile('./src/_data/json/albums-map.json', 'utf8'));
const chartData = JSON.parse(await readFile('./src/_data/json/weekly-top-artists-chart.json', 'utf8'))
const artistChart = buildChart(chartData['data'], artists, albums)['artists'].splice(0, 8)
let content = 'My top artists for the week: '
artistChart.forEach((artist, index) => {
content += `${artist['title']} @ ${artist['plays']} play${parseInt(artist['plays']) > 1 ? 's' : ''}`
if (index !== artistChart.length - 1) content += ', '
})
}
content += ' #Music'
return [{
title: content,
url: `https://coryd.dev/now?ts=${currentDate.toMillis()}#artists`,
date: DateTime.fromMillis(parseInt(chartData['timestamp'])).toISO(),
description: `My top artists for the last week ending ${currentDate.minus({ days: 1 }).toLocaleString(DateTime.DATE_FULL)}.<br/><br/>`
}]
}