Compare commits

...

2 Commits

Author SHA1 Message Date
Tom
ad624172a6 Logging level changes based on environment 2024-12-04 01:39:35 +00:00
Tom
cfb63f1e3e Fixed scrobble filtering when a session ends. 2024-12-04 01:38:38 +00:00
2 changed files with 30 additions and 25 deletions

View File

@ -1,4 +1,12 @@
const pino = require("pino"); const pino = require("pino");
const logger = pino(pino.destination({ dest: 'logs/.log', sync: false })); const logger = pino(pino.destination({ dest: 'logs/.log', sync: false }));
const environment = process.env.NODE_ENV || 'development';
console.log(process.env.NODE_ENV);
if (environment == "production") {
logger.level = 30
} else {
logger.level = 20
}
module.exports = logger; module.exports = logger;

View File

@ -21,25 +21,13 @@ async function poll() {
const previous = lastPlaying[current.sessionKey]; const previous = lastPlaying[current.sessionKey];
lastPlaying[current.sessionKey] = current; lastPlaying[current.sessionKey] = current;
if (previous == null) { if (previous == null) {
continue;
}
let filters = [];
if (previous.source == 'plex')
filters = config.scrobble.plex.filters;
if (!applyFilter(previous, filters)) {
logger.debug(previous, 'No filters got triggered. Ignoring.');
continue;
}
if (!previous) {
logger.info(current, "A new session has started."); logger.info(current, "A new session has started.");
} else { continue;
if (checkIfCanScrobble(current, previous, now)) { }
logger.info(previous, "Scrobble");
lastScrobbleTimes[previous.mediaKey] = now; if (checkIfCanScrobble(current, previous, now)) {
} logger.info(previous, "Scrobble");
lastScrobbleTimes[previous.mediaKey] = now;
} }
} }
@ -77,20 +65,29 @@ function applyFilter(track, filters) {
return false; return false;
} }
function checkIfCanScrobble(current, last, now) { function checkIfCanScrobble(current, previous, now) {
if (!last) if (!previous)
return; return;
let filters = [];
if (previous.source == 'plex')
filters = config.scrobble.plex.filters;
if (!applyFilter(previous, filters)) {
logger.debug(previous, 'No filters got triggered. Ignoring.');
return false;
}
const scrobbleDuration = isInt(config.scrobble.minimum.duration) ? Number(config.scrobble.minimum.duration) : 30; const scrobbleDuration = isInt(config.scrobble.minimum.duration) ? Number(config.scrobble.minimum.duration) : 30;
const scrobblePercent = isInt(config.scrobble.minimum.percent) ? Number(config.scrobble.minimum.percent) : 30; const scrobblePercent = isInt(config.scrobble.minimum.percent) ? Number(config.scrobble.minimum.percent) : 30;
if (last) { if (previous) {
const newPlayback = current == null || current.playtime < last.playtime; const newPlayback = current == null || current.playtime < previous.playtime;
const canBeScrobbled = last.playtime > scrobbleDuration * 1000 || last.playtime / last.duration > scrobblePercent; const canBeScrobbled = previous.playtime > scrobbleDuration * 1000 || previous.playtime / previous.duration > scrobblePercent;
if (newPlayback && canBeScrobbled) { if (newPlayback && canBeScrobbled) {
const sameSong = current != null && current.mediaKey == last.mediaKey; const sameSong = current != null && current.mediaKey == previous.mediaKey;
const lastTime = lastScrobbleTimes[last.mediaKey]; const lastTime = lastScrobbleTimes[previous.mediaKey];
return !sameSong || !lastTime || now - lastTime > scrobbleDuration; return !sameSong || !lastTime || now - lastTime > scrobbleDuration;
} }
} }