Added basic filtering to Plex scrobbling detection.
This commit is contained in:
@ -43,6 +43,7 @@ async function getCurrentlyPlaying(cached = false) {
|
||||
"platformVersion": media.Player.platformVersion,
|
||||
"product": media.Player.product,
|
||||
"version": media.Player.version,
|
||||
"source": "plex"
|
||||
}));
|
||||
return cache[key];
|
||||
}
|
||||
|
@ -17,22 +17,30 @@ async function poll() {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let media of playing) {
|
||||
if (!lastScrobbleTimes[media.mediaKey]) {
|
||||
lastScrobbleTimes[media.mediaKey] = 1;
|
||||
for (let current of playing) {
|
||||
const previous = lastPlaying[current.sessionKey];
|
||||
lastPlaying[current.sessionKey] = current;
|
||||
if (previous == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const lastTrack = lastPlaying[media.sessionKey];
|
||||
if (!lastTrack) {
|
||||
logger.info(media, "A new session has started.");
|
||||
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(media, lastTrack, now)) {
|
||||
logger.info(lastTrack, "Scrobble");
|
||||
lastScrobbleTimes[lastTrack.mediaKey] = now;
|
||||
if (checkIfCanScrobble(current, previous, now)) {
|
||||
logger.info(previous, "Scrobble");
|
||||
lastScrobbleTimes[previous.mediaKey] = now;
|
||||
}
|
||||
}
|
||||
|
||||
lastPlaying[media.sessionKey] = media;
|
||||
}
|
||||
|
||||
// Scrobble then remove lingering sessions
|
||||
@ -49,9 +57,32 @@ async function poll() {
|
||||
}
|
||||
}
|
||||
|
||||
function applyFilter(track, filters) {
|
||||
if (!filters)
|
||||
return;
|
||||
|
||||
for (let filter of filters) {
|
||||
if (filter.library && !filter.library.some(l => l == track.library))
|
||||
continue;
|
||||
if (filter.ip && !filter.ip.some(l => l == track.ip))
|
||||
continue;
|
||||
if (filter.deviceId && !filter.deviceId.some(l => l == track.deviceId))
|
||||
continue;
|
||||
if (filter.platform && !filter.platform.some(l => l == track.platform))
|
||||
continue;
|
||||
if (filter.product && !filter.product.some(l => l == track.product))
|
||||
continue;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkIfCanScrobble(current, last, now) {
|
||||
const scrobbleDuration = isInt(config.scrobble.duration) ? Number(config.scrobble.duration) : 30;
|
||||
const scrobblePercent = isInt(config.scrobble.percent) ? Number(config.scrobble.percent) : 30;
|
||||
if (!last)
|
||||
return;
|
||||
|
||||
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;
|
||||
|
Reference in New Issue
Block a user