chore: scrobbling

This commit is contained in:
Cory Dransfeldt 2024-03-30 15:29:52 -07:00
parent 146e8d942a
commit 29cddaeab7
No known key found for this signature in database
3 changed files with 49 additions and 4 deletions

3
.env
View file

@ -9,4 +9,5 @@ API_TOKEN_READWISE=
SECRET_FEED_ALBUM_RELEASES=
COOKIE_STORYGRAPH=
LISTENBRAINZ_TOKEN=
ACCOUNT_ID_PLEX =
ACCOUNT_ID_PLEX=
API_KEY_MUSIC=

44
api/music.js Normal file
View file

@ -0,0 +1,44 @@
import { getStore } from '@netlify/blobs'
export default async (request) => {
const API_KEY_MUSIC = Netlify.env.get('API_KEY_MUSIC');
const params = new URL(request['url']).searchParams
const key = params.get('key')
const weeks = params.get('key').split(',')
if (!key) return new Response(JSON.stringify({
status: 'Bad request',
}),
{ headers: { "Content-Type": "application/json" } }
)
if (key !== API_KEY_MUSIC) return new Response(JSON.stringify({
status: 'Forbidden',
}),
{ headers: { "Content-Type": "application/json" } }
)
const scrobbles = getStore('scrobbles')
const weekKey = () => {
const currentDate = DateTime.now();
return `${currentDate.year}-${currentDate.weekNumber}`
}
const scrobbleData = []
if (weeks) {
weeks.forEach(async (week) => {
const weekData = await scrobbles.get(week, { type: 'json'})
scrobbleData.push(weekData['data'])
})
} else {
const weekData = await scrobbles.get(weekKey(), { type: 'json'})
scrobbleData.push(weekData['data'])
}
return new Response(JSON.stringify({ data: scrobbleData }),
{ headers: { "Content-Type": "application/json" } }
)
}
export const config = {
path: '/api/music',
}

View file

@ -25,8 +25,8 @@ const filterOldScrobbles = (scrobbles) => {
}
export default async (request) => {
const ACCOUNT_ID_PLEX = Netlify.env.get("ACCOUNT_ID_PLEX");
const MUSIC_KEY = Netlify.env.get("API_KEY_LASTFM");
const ACCOUNT_ID_PLEX = Netlify.env.get('ACCOUNT_ID_PLEX');
const MUSIC_KEY = Netlify.env.get('API_KEY_LASTFM');
const params = new URL(request['url']).searchParams
const id = params.get('id')
@ -162,5 +162,5 @@ export default async (request) => {
}
export const config = {
path: "/api/scrobble",
path: '/api/scrobble',
}