Skip to content

Commit

Permalink
Merge pull request #7 from FoxxMD/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
FoxxMD authored Nov 28, 2020
2 parents f749408 + 5a1fb1c commit 29cdf34
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
16 changes: 12 additions & 4 deletions clients/AbstractScrobbleClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from "dayjs";
import {createLabelledLogger} from "../utils.js";
import {buildTrackString, createLabelledLogger} from "../utils.js";

export default class AbstractScrobbleClient {

Expand Down Expand Up @@ -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;
}
}
18 changes: 12 additions & 6 deletions clients/MalojaScrobbler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
7 changes: 6 additions & 1 deletion clients/ScrobbleClients.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 6 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 29cdf34

Please sign in to comment.