Fix some reporting of incorrect episodes (check more log history to account for short episodes; only accept positive deltas)

This commit is contained in:
2026-04-26 12:03:11 -04:00
parent cfcf16a031
commit b950b55017

View File

@@ -14,13 +14,13 @@ Pebble.addEventListener("appmessage", function (dict) {
// TODO report last episode as "Configure app settings!" if CLAY_API_HOST, CLAY_API_KEY, or CLAY_USER is empty // TODO report last episode as "Configure app settings!" if CLAY_API_HOST, CLAY_API_KEY, or CLAY_USER is empty
// TODO use API to dynamically determine user (prompt on watch) // TODO use API to dynamically determine user (prompt on watch)
if (cfg.CLAY_API_IS_JELLYFIN == true) { if (cfg.CLAY_API_IS_JELLYFIN == true) {
callAPI(cfg.CLAY_API_HOST + "/System/ActivityLog/Entries?hasUserId=true&limit=50", cfg.CLAY_API_KEY, true, cfg.CLAY_USER, sleepTimestamp * 1000); callAPI(cfg.CLAY_API_HOST + "/System/ActivityLog/Entries?hasUserId=true&limit=100", cfg.CLAY_API_KEY, true, cfg.CLAY_USER, sleepTimestamp * 1000);
} else { } else {
// The official history endpoint doesn't seem to return history w/o Plex Pass. // The official history endpoint doesn't seem to return history w/o Plex Pass.
// If someone with Plex Pass wants to give it a shot, I'd be happy to accept a PR adding this as an option. // If someone with Plex Pass wants to give it a shot, I'd be happy to accept a PR adding this as an option.
// Until then, I'm using Tautulli because it's free. // Until then, I'm using Tautulli because it's free.
//callAPI(cfg.CLAY_API_HOST + "/status/sessions/history/all", cfg.CLAY_API_KEY, false, cfg.CLAY_USER, sleepTimestamp); //callAPI(cfg.CLAY_API_HOST + "/status/sessions/history/all", cfg.CLAY_API_KEY, false, cfg.CLAY_USER, sleepTimestamp);
callAPI(cfg.CLAY_API_HOST + "/api/v2?apikey=" + cfg.CLAY_API_KEY + "&cmd=get_history&length=50", null, false, cfg.CLAY_USER, sleepTimestamp); callAPI(cfg.CLAY_API_HOST + "/api/v2?apikey=" + cfg.CLAY_API_KEY + "&cmd=get_history&length=100", null, false, cfg.CLAY_USER, sleepTimestamp);
} }
} }
}); });
@@ -43,8 +43,8 @@ function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) {
if (arr != null && arr[1] == trackedUser) { if (arr != null && arr[1] == trackedUser) {
logTimestamp = Date.parse(item.Date); logTimestamp = Date.parse(item.Date);
if (!Number.isNaN(logTimestamp)) { if (!Number.isNaN(logTimestamp)) {
delta = Math.abs(logTimestamp - sleepTimestamp); delta = sleepTimestamp - logTimestamp;
if (delta < bestDelta) { if (delta >= 0 && delta < bestDelta) {
bestDelta = delta; bestDelta = delta;
lastWatched = arr[2]; lastWatched = arr[2];
} }
@@ -56,8 +56,8 @@ function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) {
if (item.user == trackedUser) { if (item.user == trackedUser) {
// we use "date" instead of "started" because started tracks the first // we use "date" instead of "started" because started tracks the first
// time the user watched the media, not just the start of the last session. // time the user watched the media, not just the start of the last session.
delta = Math.abs(item.date - sleepTimestamp); delta = sleepTimestamp - item.date;
if (delta < bestDelta) { if (delta >= 0 && delta < bestDelta) {
bestDelta = delta; bestDelta = delta;
lastWatched = item.full_title; lastWatched = item.full_title;
} }