feat: pull my own artist charts
This commit is contained in:
parent
5fb630aaf8
commit
982e479812
6 changed files with 27 additions and 58 deletions
30
.github/workflows/fetch-artist-charts.yaml
vendored
30
.github/workflows/fetch-artist-charts.yaml
vendored
|
@ -1,30 +0,0 @@
|
|||
name: Fetch weekly artist charts
|
||||
on:
|
||||
workflow_dispatch:
|
||||
schedule:
|
||||
- cron: '00 09 * * 5'
|
||||
jobs:
|
||||
FetchArtistCharts:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
steps:
|
||||
- name: Checkout out this repo
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
ref: ${{ github.head_ref }}
|
||||
- name: Fetch charts
|
||||
run: |
|
||||
echo "CHART_DATA=$(curl 'https://ws.audioscrobbler.com/2.0/?method=user.getweeklyartistchart&user=coryd_&api_key=${{ secrets.LASTFM_API_KEY }}&format=json')" >> "$GITHUB_ENV"
|
||||
- name: Update charts
|
||||
run: |
|
||||
echo "CHARTS=$(cat src/_data/json/weekly-artist-charts.json | jq -c --argjson jq_data "$CHART_DATA" '.charts += [$jq_data]')" >> "$GITHUB_ENV"
|
||||
- name: Write charts
|
||||
run: |
|
||||
echo $CHARTS > src/_data/json/weekly-artist-charts.json
|
||||
- name: Commit
|
||||
uses: stefanzweifel/git-auto-commit-action@v5
|
||||
with:
|
||||
commit_message: Update artist charts for the week.
|
||||
commit_user_name: cdransf
|
||||
commit_user_email: coryd@hey.com
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "coryd.dev",
|
||||
"version": "9.8.4",
|
||||
"version": "9.9.0",
|
||||
"description": "The source for my personal site. Built using 11ty.",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
|
|
@ -34,6 +34,8 @@ export const onPreBuild = async ({ constants }) => {
|
|||
token: constants.NETLIFY_API_TOKEN,
|
||||
})
|
||||
|
||||
const currentDate = DateTime.now()
|
||||
const lastWeek = currentDate.minus({ weeks: 1 })
|
||||
const monthKeys = getKeys()
|
||||
const monthChartData = { data: [] }
|
||||
const threeMonthKeys = getKeys(3)
|
||||
|
@ -41,6 +43,7 @@ export const onPreBuild = async ({ constants }) => {
|
|||
const scrobbles = getStore('scrobbles')
|
||||
const artists = getStore('artists')
|
||||
const albums = getStore('albums')
|
||||
const weeklyChartData = await scrobbles.get(`${lastWeek.year}-${lastWeek.weekNumber}`)
|
||||
const windowData = await scrobbles.get('window', { type: 'json'})
|
||||
const artistsMap = await artists.get('artists-map', { type: 'json' })
|
||||
const albumsMap = await albums.get('albums-map', { type: 'json' })
|
||||
|
@ -56,6 +59,7 @@ export const onPreBuild = async ({ constants }) => {
|
|||
threeMonthChartData['data'].push(...scrobbleData['data'])
|
||||
}
|
||||
|
||||
if (currentDate.weekday === 1) fs.writeFileSync('./src/_data/json/weekly-top-artists-chart.json', JSON.stringify({...weeklyChartData, timestamp: `${lastWeek.toMillis()}` }))
|
||||
fs.writeFileSync('./src/_data/json/scrobbles-window.json', JSON.stringify(windowData))
|
||||
fs.writeFileSync('./src/_data/json/artists-map.json', JSON.stringify(artistsMap))
|
||||
fs.writeFileSync('./src/_data/json/albums-map.json', JSON.stringify(albumsMap))
|
||||
|
|
File diff suppressed because one or more lines are too long
1
src/_data/json/weekly-top-artists-chart.json
Normal file
1
src/_data/json/weekly-top-artists-chart.json
Normal file
File diff suppressed because one or more lines are too long
|
@ -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/>`
|
||||
}]
|
||||
}
|
Reference in a new issue