diff --git a/utils.js b/utils.js index fd730716..b2844580 100644 --- a/utils.js +++ b/utils.js @@ -67,7 +67,7 @@ export function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } -export const buildTrackString = (playObj) => { +export const buildTrackString = (playObj, include = ['time']) => { const { data: { artist, @@ -77,7 +77,11 @@ export const buildTrackString = (playObj) => { } = {} } = playObj; - return `${artist} - ${track}, played at ${playDate.local().format()}` + let str = `${artist} - ${track}`; + if (include.includes('time')) { + str = `${str}, played at ${playDate.local().format()}` + } + return str; } // sorts playObj formatted objects by playDate in ascending (oldest first) order -- 2.51.2 From 5a1fb1c08e01ce2204b7af5d58f868360c88e28a Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Wed, 25 Nov 2020 18:49:23 -0500 Subject: [PATCH 2/2] Attempt to fix use-case when maloja history is empty and provide more logging for not scrobbling * Fix an empty maloja history always causing oldest scrobble to now() time, preventing time frame from ever being valid * Add debug logging to timeframe and existing scrobble checks for new tracks --- clients/AbstractScrobbleClient.js | 16 ++++++++++++---- clients/MalojaScrobbler.js | 18 ++++++++++++------ clients/ScrobbleClients.js | 7 ++++++- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/clients/AbstractScrobbleClient.js b/clients/AbstractScrobbleClient.js index 45084292..32aaa0dc 100644 --- a/clients/AbstractScrobbleClient.js +++ b/clients/AbstractScrobbleClient.js @@ -1,5 +1,5 @@ import dayjs from "dayjs"; -import {createLabelledLogger} from "../utils.js"; +import {buildTrackString, createLabelledLogger} from "../utils.js"; export default class AbstractScrobbleClient { @@ -33,8 +33,16 @@ export default class AbstractScrobbleClient { // time frame is valid as long as the play date for the source track is newer than the oldest play time from the scrobble client // ...this is assuming the scrobble client is returning "most recent" scrobbles - timeFrameIsValid = (playDate) => { - const oldest = this.oldestScrobbleTime ?? dayjs(); - return playDate.isAfter(oldest); + timeFrameIsValid = (playObj, log = false) => { + const { + data: { + playDate, + } = {}, + } = playObj; + const validTime = playDate.isAfter(this.oldestScrobbleTime); + if (log && !validTime) { + this.logger.debug(`${buildTrackString(playObj)} was in an invalid time frame (played before the oldest scrobble found)`); + } + return validTime; } } diff --git a/clients/MalojaScrobbler.js b/clients/MalojaScrobbler.js index b957bf5a..b9f3cea1 100644 --- a/clients/MalojaScrobbler.js +++ b/clients/MalojaScrobbler.js @@ -109,16 +109,22 @@ export default class MalojaScrobbler extends AbstractScrobbleClient { } = {}, } = resp; this.recentScrobbles = list.map(x => MalojaScrobbler.formatPlayObj(x)).sort(sortByPlayDate); - const [{data: {playDate: newestScrobbleTime = dayjs()} = {}} = {}] = this.recentScrobbles.slice(-1); - const [{data: {playDate: oldestScrobbleTime = dayjs()} = {}} = {}] = this.recentScrobbles.slice(0, 1); - this.newestScrobbleTime = newestScrobbleTime; - this.oldestScrobbleTime = oldestScrobbleTime; + if (this.recentScrobbles.length > 0) { + const [{data: {playDate: newestScrobbleTime = dayjs()} = {}} = {}] = this.recentScrobbles.slice(-1); + const [{data: {playDate: oldestScrobbleTime = dayjs()} = {}} = {}] = this.recentScrobbles.slice(0, 1); + this.newestScrobbleTime = newestScrobbleTime; + this.oldestScrobbleTime = oldestScrobbleTime; + } } this.lastScrobbleCheck = dayjs(); } - alreadyScrobbled = (playObj) => { - return this.existingScrobble(playObj) !== undefined; + alreadyScrobbled = (playObj, log = false) => { + const result = this.existingScrobble(playObj) !== undefined; + if (log && result === true) { + this.logger.debug(`${buildTrackString(playObj, [])} was already scrobbled`); + } + return result; } existingScrobble = (playObj) => { diff --git a/clients/ScrobbleClients.js b/clients/ScrobbleClients.js index 24fbe17f..e3e2ad07 100644 --- a/clients/ScrobbleClients.js +++ b/clients/ScrobbleClients.js @@ -92,7 +92,12 @@ export default class ScrobbleClients { await client.refreshScrobbles(); } for (const playObj of playObjs) { - if (client.timeFrameIsValid(playObj.data.playDate) && !client.alreadyScrobbled(playObj)) { + const { + meta: { + newFromSource = false, + } = {} + } = playObj; + if (client.timeFrameIsValid(playObj, newFromSource) && !client.alreadyScrobbled(playObj, newFromSource)) { tracksScrobbled.push(playObj); await client.scrobble(playObj); }