2024-12-05 10:15:56 -05:00
|
|
|
const AggregateTracker = require("./trackers/aggregate-tracker");
|
2024-12-05 03:36:22 -05:00
|
|
|
const Session = require("../models/session");
|
|
|
|
|
|
|
|
class Recorder {
|
|
|
|
#sessions = null;
|
|
|
|
#trackers = null;
|
2024-12-05 12:32:37 -05:00
|
|
|
#originalTrackers = [];
|
|
|
|
#scrobblers = [];
|
2024-12-05 03:36:22 -05:00
|
|
|
#config = null;
|
|
|
|
#logger = null;
|
|
|
|
#lastTick = null;
|
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
constructor(sessions, trackers, scrobblers, config, logger) {
|
2024-12-05 03:36:22 -05:00
|
|
|
this.#sessions = sessions;
|
2024-12-05 12:32:37 -05:00
|
|
|
this.#trackers = new AggregateTracker('aggregate', trackers);
|
|
|
|
this.#originalTrackers = trackers;
|
|
|
|
this.#scrobblers = scrobblers;
|
2024-12-05 03:36:22 -05:00
|
|
|
this.#config = config;
|
|
|
|
this.#logger = logger;
|
|
|
|
this.#lastTick = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
async record() {
|
|
|
|
const now = Date.now();
|
|
|
|
const timeDiff = now - this.#lastTick;
|
|
|
|
const media = await this.#trackers.poll();
|
2024-12-05 12:32:37 -05:00
|
|
|
const contexts = media.map(m => this.#fetchContext(m));
|
2024-12-05 03:36:22 -05:00
|
|
|
|
|
|
|
// Find sessions that ended and that are deemable of a scrobble.
|
|
|
|
const sessionIds = this.#sessions.getSessionIds();
|
2024-12-05 12:32:37 -05:00
|
|
|
const stopped = sessionIds.filter(sessionId => !contexts.some(context => sessionId == context.session.id))
|
|
|
|
.map(sessionId => this.#sessions.get(sessionId))
|
|
|
|
.map(session => this.#fetchContext(session.playing));
|
|
|
|
const contextEnded = stopped.filter(context => this.#canScrobble(context.session, null, context.session.playing, now));
|
|
|
|
|
|
|
|
for (let context of contextEnded)
|
|
|
|
context.session.playDuration = now - (context.session.lastScrobbleTimestamp || context.session.started) - context.session.pauseDuration;
|
2024-12-05 03:36:22 -05:00
|
|
|
|
|
|
|
// Find ongoing sessions that have moved on to the next song.
|
2024-12-05 12:32:37 -05:00
|
|
|
const finishedPlaying = contexts.filter(context => this.#listen(context, now, timeDiff));
|
2024-12-05 03:36:22 -05:00
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
// Scrobble
|
|
|
|
const scrobbling = finishedPlaying.concat(contextEnded);
|
|
|
|
for (let context of scrobbling)
|
|
|
|
await this.#scrobble(context);
|
2024-12-05 03:36:22 -05:00
|
|
|
|
|
|
|
// Remove dead sessions.
|
2024-12-05 12:32:37 -05:00
|
|
|
for (let context of stopped) {
|
|
|
|
this.#sessions.remove(context.session.id);
|
|
|
|
}
|
2024-12-05 03:36:22 -05:00
|
|
|
|
|
|
|
this.#lastTick = now;
|
|
|
|
}
|
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
#fetchContext(media) {
|
|
|
|
const tracker = this.#originalTrackers.find(t => t.provider == media.provider && t.name == media.source);
|
2024-12-05 03:36:22 -05:00
|
|
|
let session = this.#sessions.get(media.session);
|
|
|
|
if (session == null) {
|
|
|
|
session = new Session(media.session);
|
|
|
|
this.#sessions.add(session);
|
|
|
|
}
|
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
return { session, media, tracker }
|
2024-12-05 03:36:22 -05:00
|
|
|
}
|
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
#listen(context, timestamp, timeDiff) {
|
|
|
|
const session = context.session;
|
|
|
|
const current = context.media;
|
|
|
|
const previous = context.session.playing;
|
2024-12-05 03:36:22 -05:00
|
|
|
session.playing = current;
|
2024-12-05 12:32:37 -05:00
|
|
|
session.playDuration = timestamp - (session.lastScrobbleTimestamp || session.started) - session.pauseDuration;
|
2024-12-05 03:36:22 -05:00
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
if (!previous) {
|
2024-12-05 03:36:22 -05:00
|
|
|
this.#logger.info(current, "A new session has started.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
if (session.playing.state == "paused" || previous.state == "paused")
|
2024-12-05 03:36:22 -05:00
|
|
|
session.pauseDuration += timeDiff - (session.playing.progress - previous.progress);
|
|
|
|
|
|
|
|
if (this.#canScrobble(session, current, previous, timestamp)) {
|
|
|
|
session.pauseDuration = 0;
|
|
|
|
session.lastScrobbleTimestamp = timestamp - (timeDiff - (previous.duration - previous.progress));
|
|
|
|
return true;
|
|
|
|
} else if (current.progress < previous.progress && session.playing.id != previous.id) {
|
|
|
|
session.pauseDuration = 0;
|
|
|
|
if (current.progress < timeDiff)
|
|
|
|
session.lastScrobbleTimestamp = timestamp - session.playing.progress;
|
|
|
|
else
|
|
|
|
session.lastScrobbleTimestamp = timestamp;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#canScrobble(session, current, previous, timestamp) {
|
|
|
|
if (previous == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const scrobbleDuration = this.#config.minimum.duration || 240;
|
|
|
|
const scrobblePercent = this.#config.minimum.percent || 50;
|
|
|
|
|
|
|
|
const newPlayback = current == null || current.progress < previous.progress;
|
2024-12-05 12:32:37 -05:00
|
|
|
const canBeScrobbled = session.playDuration > scrobbleDuration * 1000 || session.playDuration / previous.duration > scrobblePercent / 100.0;
|
2024-12-05 03:36:22 -05:00
|
|
|
|
|
|
|
return newPlayback && canBeScrobbled;
|
|
|
|
}
|
|
|
|
|
2024-12-05 12:32:37 -05:00
|
|
|
async #scrobble(context) {
|
|
|
|
this.#logger.info(context, "Scrobble");
|
|
|
|
|
|
|
|
for (var scrobblerName of context.tracker.scrobblerNames) {
|
|
|
|
const scrobbler = this.#scrobblers.find(s => s.name == scrobblerName);
|
|
|
|
if (scrobbler == null) {
|
|
|
|
this.#logger.error(`Cannot find scrobbler by name of '${scrobblerName}'.`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await scrobbler.scrobble(context.media, Date.now() - Math.min(context.media.duration, context.session.playDuration));
|
|
|
|
} catch (ex) {
|
|
|
|
this.#logger.error(ex, "Could not send to maloja.");
|
|
|
|
}
|
|
|
|
}
|
2024-12-05 03:36:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Recorder;
|