chore: scrobbling

This commit is contained in:
Cory Dransfeldt 2024-03-29 16:03:19 -07:00
parent 9c9331c743
commit e442cf7e1c
No known key found for this signature in database

View file

@ -24,10 +24,6 @@ export default async (request) => {
const MUSIC_KEY = Netlify.env.get("API_KEY_LASTFM"); const MUSIC_KEY = Netlify.env.get("API_KEY_LASTFM");
const params = new URL(request['url']).searchParams const params = new URL(request['url']).searchParams
const id = params.get('id') const id = params.get('id')
const data = await request.formData()
const payload = JSON.parse(data.get('payload'))
const artists = getStore('artists')
const scrobbles = getStore('scrobbles')
if (!id) return new Response(JSON.stringify({ if (!id) return new Response(JSON.stringify({
status: 'Bad request', status: 'Bad request',
@ -41,6 +37,11 @@ export default async (request) => {
{ headers: { "Content-Type": "application/json" } } { headers: { "Content-Type": "application/json" } }
) )
const data = await request.formData()
const payload = JSON.parse(data.get('payload'))
const artists = getStore('artists')
const scrobbles = getStore('scrobbles')
if (payload?.event === 'media.scrobble') { if (payload?.event === 'media.scrobble') {
const artist = payload['Metadata']['grandparentTitle'] const artist = payload['Metadata']['grandparentTitle']
const album = payload['Metadata']['parentTitle'] const album = payload['Metadata']['parentTitle']
@ -101,19 +102,16 @@ export default async (request) => {
// scrobble logic // scrobble logic
artistInfo = await artists.get(artistKey, { type: 'json'}) artistInfo = await artists.get(artistKey, { type: 'json'})
console.log(JSON.parse(artistInfo))
const artistUrl = `https://musicbrainz.org/artist/${artistInfo?.['mbid']}`
const trackScrobbleData = { const trackScrobbleData = {
track, track,
album, album,
artist, artist,
trackNumber, trackNumber,
timestamp, timestamp
artistUrl
} }
const scrobbleData = await scrobbles.get(`${weekStop()}`, { type: 'json'}) const scrobbleData = await scrobbles.get(`${weekStop()}`, { type: 'json'})
const windowData = await scrobbles.get('window', { type: 'json'}) const windowData = await scrobbles.get('window', { type: 'json'})
await scrobbles.setJSON('now-playing', trackScrobbleData) await scrobbles.setJSON('now-playing', {...trackScrobbleData, ...{ url: `https://musicbrainz.org/artist/${artistInfo?.['mbid']}`}})
let scrobbleUpdate = scrobbleData let scrobbleUpdate = scrobbleData
let windowUpdate = windowData; let windowUpdate = windowData;
if (scrobbleUpdate['data']) scrobbleUpdate['data'].push(trackScrobbleData) if (scrobbleUpdate['data']) scrobbleUpdate['data'].push(trackScrobbleData)