Updated polling with sessions. Changed scrobble condition to play duration only. Fixed percentage scrobble condition. Changed default duration and percentage to 240 & 50, respectively.

This commit is contained in:
Tom 2024-12-05 01:10:09 +00:00
parent 478bd76aeb
commit 539b9a5055
2 changed files with 46 additions and 30 deletions

View File

@ -1,13 +1,15 @@
const plex = require("./plex"); const plex = require("./plex");
const logger = require("./logging") const logger = require("./logging")
const config = require("../config/configuration"); const config = require("../config/configuration");
const Session = require("../models/session");
const sessions = require("../services/session-manager");
const spotify = require("./spotify"); const spotify = require("./spotify");
const lastPlaying = {}; let lastTick = Date.now();
const lastScrobbleTimes = {};
async function poll() { async function poll() {
const now = Date.now(); const now = Date.now();
const timeDiff = now - lastTick;
const playing = []; const playing = [];
await spotify.loadCredentials(); await spotify.loadCredentials();
@ -25,31 +27,50 @@ async function poll() {
} }
for (let current of playing) { for (let current of playing) {
const previous = lastPlaying[current.sessionKey]; let session = sessions.get(current.sessionKey);
lastPlaying[current.sessionKey] = current; if (session == null) {
session = new Session(current.sessionKey);
sessions.add(session);
}
const previous = session.playing;
session.playing = current;
if (previous == null) { if (previous == null) {
logger.info(current, "A new session has started."); logger.info(current, "A new session has started.");
continue; continue;
} }
if (checkIfCanScrobble(current, previous, now)) { if (session.playing.state == "paused" || previous.state == "paused") {
session.pauseDuration += timeDiff - (session.playing.playtime - previous.playtime);
}
if (checkIfCanScrobble(session, previous, now, timeDiff)) {
logger.info(previous, "Scrobble"); logger.info(previous, "Scrobble");
lastScrobbleTimes[previous.mediaKey] = now; session.pauseDuration = 0;
session.lastScrobbleTimestamp = now - (timeDiff - (previous.duration - previous.playtime));
} else if (session.playing.playtime < previous.playtime && session.playing.mediaKey != previous.mediaKey) {
session.pauseDuration = 0;
if (session.playing.playtime < timeDiff)
session.lastScrobbleTimestamp = now - session.playing.playtime;
else
session.lastScrobbleTimestamp = now;
} }
} }
// Scrobble then remove lingering sessions const ids = sessions.getSessionIds();
for (let key in lastPlaying) { for (let sessionId of ids) {
if (!playing.some(p => p.sessionKey == key)) { if (playing.some(p => p.sessionKey == sessionId))
const track = lastPlaying[key]; continue;
if (checkIfCanScrobble(null, track, now)) {
logger.info(track, "Scrobble"); session.playing = null;
lastScrobbleTimes[track.mediaKey] = now; if (checkIfCanScrobble(session, session.playing, now, timeDiff)) {
} logger.info(session.playing, "Scrobble");
delete lastPlaying[key];
logger.debug("Deleted old session.", key);
} }
sessions.remove(sessionId);
logger.debug("Deleted old session (" + sessionId + ")");
} }
lastTick = now;
} }
function applyFilter(track, filters) { function applyFilter(track, filters) {
@ -72,7 +93,7 @@ function applyFilter(track, filters) {
return false; return false;
} }
function checkIfCanScrobble(current, previous, now) { function checkIfCanScrobble(session, previous, now, timeDiff) {
if (!previous) if (!previous)
return false; return false;
@ -85,20 +106,15 @@ function checkIfCanScrobble(current, previous, now) {
return false; return false;
} }
const scrobbleDuration = isInt(config.scrobble.minimum.duration) ? Number(config.scrobble.minimum.duration) : 30; const scrobbleDuration = config.scrobble.minimum.duration || 240;
const scrobblePercent = isInt(config.scrobble.minimum.percent) ? Number(config.scrobble.minimum.percent) : 30; const scrobblePercent = config.scrobble.minimum.percent || 50;
if (previous) { const current = session.playing;
const newPlayback = current == null || current.playtime < previous.playtime; const durationPlayed = now - (session.lastScrobbleTimestamp ?? session.started) - session.pauseDuration;
const canBeScrobbled = previous.playtime > scrobbleDuration * 1000 || previous.playtime / previous.duration > scrobblePercent; const newPlayback = current == null || current.playtime < previous.playtime && current.playtime < timeDiff;
const canBeScrobbled = durationPlayed > scrobbleDuration * 1000 || durationPlayed / previous.duration > scrobblePercent / 100.0;
if (newPlayback && canBeScrobbled) { return newPlayback && canBeScrobbled;
const sameSong = current != null && current.mediaKey == previous.mediaKey;
const lastTime = lastScrobbleTimes[previous.mediaKey];
return !sameSong || !lastTime || now - lastTime > scrobbleDuration;
}
}
return false;
} }
function isInt(value) { function isInt(value) {

View File

@ -34,7 +34,7 @@ async function refreshTokenIfNeeded() {
} }
await fs.writeFile("credentials.spotify.json", JSON.stringify(data)); await fs.writeFile("credentials.spotify.json", JSON.stringify(data));
token = data; token = data;
logger.info("Updated access token for Spotify."); logger.debug("Updated access token for Spotify.");
return true; return true;
} catch (ex) { } catch (ex) {
logger.error(ex, "Failed to get Spotify oauth."); logger.error(ex, "Failed to get Spotify oauth.");