diff --git a/services/poll.js b/services/poll.js index 368536e..6f80ca6 100644 --- a/services/poll.js +++ b/services/poll.js @@ -21,25 +21,13 @@ async function poll() { const previous = lastPlaying[current.sessionKey]; lastPlaying[current.sessionKey] = current; 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."); - } else { - if (checkIfCanScrobble(current, previous, now)) { - logger.info(previous, "Scrobble"); - lastScrobbleTimes[previous.mediaKey] = now; - } + continue; + } + + if (checkIfCanScrobble(current, previous, now)) { + logger.info(previous, "Scrobble"); + lastScrobbleTimes[previous.mediaKey] = now; } } @@ -77,20 +65,29 @@ function applyFilter(track, filters) { return false; } -function checkIfCanScrobble(current, last, now) { - if (!last) +function checkIfCanScrobble(current, previous, now) { + if (!previous) 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 scrobblePercent = isInt(config.scrobble.minimum.percent) ? Number(config.scrobble.minimum.percent) : 30; - if (last) { - const newPlayback = current == null || current.playtime < last.playtime; - const canBeScrobbled = last.playtime > scrobbleDuration * 1000 || last.playtime / last.duration > scrobblePercent; + if (previous) { + const newPlayback = current == null || current.playtime < previous.playtime; + const canBeScrobbled = previous.playtime > scrobbleDuration * 1000 || previous.playtime / previous.duration > scrobblePercent; if (newPlayback && canBeScrobbled) { - const sameSong = current != null && current.mediaKey == last.mediaKey; - const lastTime = lastScrobbleTimes[last.mediaKey]; + const sameSong = current != null && current.mediaKey == previous.mediaKey; + const lastTime = lastScrobbleTimes[previous.mediaKey]; return !sameSong || !lastTime || now - lastTime > scrobbleDuration; } }