chore: scrobbling
This commit is contained in:
parent
a88bccf305
commit
b6ce1ab12c
1 changed files with 17 additions and 66 deletions
|
@ -8,12 +8,17 @@ const sanitizeMediaString = (string) => {
|
||||||
|
|
||||||
const weekStop = () => {
|
const weekStop = () => {
|
||||||
const currentDate = DateTime.now()
|
const currentDate = DateTime.now()
|
||||||
let nextSunday = currentDate.plus({ days: (7 - currentDate.weekday) % 7 })
|
let nextSunday = currentDate.plus({ 7: (7 - currentDate.weekday) % 7 })
|
||||||
nextSunday = nextSunday.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
|
nextSunday = nextSunday.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
|
||||||
console.log(nextSunday.toMillis());
|
|
||||||
return nextSunday.toMillis()
|
return nextSunday.toMillis()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const twoWeeksAgo = DateTime.now().minus({ weeks: 2 });
|
||||||
|
const filterOldScrobbles = (scrobbles) => scrobbles.filter(scrobble => {
|
||||||
|
let timestamp = DateTime.fromISO(scrobble.timestamp)
|
||||||
|
return timestamp.diff(twoWeeksAgo).as('weeks') <= -2
|
||||||
|
});
|
||||||
|
|
||||||
export default async (request) => {
|
export default async (request) => {
|
||||||
const ACCOUNT_ID_PLEX = Netlify.env.get("ACCOUNT_ID_PLEX");
|
const ACCOUNT_ID_PLEX = Netlify.env.get("ACCOUNT_ID_PLEX");
|
||||||
const MUSIC_KEY = Netlify.env.get("API_KEY_LASTFM");
|
const MUSIC_KEY = Netlify.env.get("API_KEY_LASTFM");
|
||||||
|
@ -105,70 +110,16 @@ export default async (request) => {
|
||||||
timestamp,
|
timestamp,
|
||||||
artistUrl
|
artistUrl
|
||||||
}
|
}
|
||||||
const scrobbleData = await scrobbles.get(weekStop(), { type: 'json'})
|
const scrobbleData = await scrobbles.get(weekStop(1), { type: 'json'})
|
||||||
let scrobbles = scrobbleData;
|
const windowData = await scrobbles.get('window', { type: 'json'})
|
||||||
scrobbleData.setJSON('now-playing', JSON.stringify(trackScrobbleData))
|
let scrobbleUpdate = scrobbleData
|
||||||
const trackKey = `${sanitizeMediaString(track).replace(/\s+/g, '-').toLowerCase()}-${sanitizeMediaString(artist).replace(/\s+/g, '-').toLowerCase()}`
|
let windowUpdate = windowData;
|
||||||
const artistInit = {
|
if (scrobbleUpdate) scrobbleUpdate['data'].push(trackScrobbleData)
|
||||||
name: artist,
|
if (!scrobbleUpdate) scrobbleUpdate = { data: [trackScrobbleData] }
|
||||||
artistUrl,
|
if (windowData) windowUpdate['data'].push(trackScrobbleData)
|
||||||
count: 1
|
if (!windowData) windowUpdate = { data: [trackScrobbleData] }
|
||||||
}
|
scrobbleData.setJSON('now-playing', JSON.stringify(scrobbleUpdate))
|
||||||
const albumInit = {
|
windowData.setJSON('window-data', JSON.stringify(filterOldScrobbles(windowUpdate)))
|
||||||
name: album,
|
|
||||||
count: 1
|
|
||||||
}
|
|
||||||
const trackInit = {
|
|
||||||
key: trackKey,
|
|
||||||
data: {
|
|
||||||
timestamp,
|
|
||||||
total: 1,
|
|
||||||
artist,
|
|
||||||
title,
|
|
||||||
artistUrl,
|
|
||||||
art: `https://cdn.coryd.dev/albums/${encodeURIComponent(sanitizeMediaString(artist).replace(/\s+/g, '-').toLowerCase())}-${encodeURIComponent(sanitizeMediaString(album.replace(/[:\/\\,'']+/g, '').replace(/\s+/g, '-').toLowerCase()))}.jpg`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1st scrobble! Let's set up
|
|
||||||
if (!scrobbles) {
|
|
||||||
scrobbles = {
|
|
||||||
chart: {
|
|
||||||
total: 1,
|
|
||||||
artists: [artistInit],
|
|
||||||
albums: [albumInit],
|
|
||||||
tracks: [trackInit]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// increment total
|
|
||||||
scrobbles['chart']['total']++
|
|
||||||
|
|
||||||
// update artist plays
|
|
||||||
if (scrobbles['chart']['artists'].find(a => a.name === artist)) {
|
|
||||||
scrobbles['chart']['artists'][scrobbles['chart']['artists'].findIndex(a => a.name === artist)]['count']++
|
|
||||||
} else {
|
|
||||||
scrobbles['chart']['artists'].push(artistInit)
|
|
||||||
}
|
|
||||||
|
|
||||||
// update album plays
|
|
||||||
if (scrobbles['chart']['albums'].find(a => a.name === album)) {
|
|
||||||
scrobbles['chart']['albums'][scrobbles['chart']['albums'].findIndex(a => a.name === album)]['count']++
|
|
||||||
} else {
|
|
||||||
scrobbles['chart']['albums'].push(albumInit)
|
|
||||||
}
|
|
||||||
|
|
||||||
// update track plays
|
|
||||||
if (scrobbles['chart']['tracks'].find(t => t.key === trackKey)) {
|
|
||||||
const track = scrobbles['chart']['tracks'][scrobbles['chart']['tracks'].findIndex(t => t.key === key)]
|
|
||||||
track[total]++
|
|
||||||
track[timestamp] = timestamp
|
|
||||||
} else {
|
|
||||||
scrobbles['chart']['tracks'].push(trackInit)
|
|
||||||
}
|
|
||||||
|
|
||||||
scrobbleData.setJSON(weekStop(), JSON.stringify(scrobbles))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Response(JSON.stringify({
|
return new Response(JSON.stringify({
|
||||||
|
|
Reference in a new issue