Complete Jellyfin log parsing logic

This commit is contained in:
2026-04-16 23:28:17 -04:00
parent 41d1a4e707
commit aaa45d5743

View File

@@ -9,8 +9,8 @@ Pebble.addEventListener("ready", function () {
Pebble.addEventListener("appmessage", function (dict) { Pebble.addEventListener("appmessage", function (dict) {
if (dict.payload["PKJS_SLEEP_TIMESTAMP"]) { if (dict.payload["PKJS_SLEEP_TIMESTAMP"]) {
var sleepTimestamp = dict.payload["PKJS_SLEEP_TIMESTAMP"]; const sleepTimestamp = dict.payload["PKJS_SLEEP_TIMESTAMP"] * 1000; // convert to milliseconds to later comparison
var cfg = JSON.parse(localStorage.getItem('clay-settings')); const cfg = JSON.parse(localStorage.getItem('clay-settings'));
// 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) {
@@ -26,19 +26,32 @@ Pebble.addEventListener("appmessage", function (dict) {
}); });
function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) { function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) {
var xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
var resp = JSON.parse(xhr.responseText); const resp = JSON.parse(xhr.responseText);
console.log(resp);
if (resp && Array.isArray(resp.Items)) { if (resp && Array.isArray(resp.Items)) {
resp.Items.forEach(function (item, index) { const re = /^(.+) is playing (.+) on .*$/;
console.log( let arr;
"[" + index + "] Name: " + item.Name + " | Date: " + item.Date let logTimestamp;
); let delta;
let bestDelta = Infinity;
let lastWatched;
resp.Items.forEach(function (item) {
arr = re.exec(item.Name);
if (arr != null && arr[1] == trackedUser) {
logTimestamp = Date.parse(item.Date);
if (!Number.isNaN(logTimestamp)) {
delta = Math.abs(logTimestamp - sleepTimestamp);
if (delta < bestDelta) {
bestDelta = delta;
lastWatched = arr[2];
}
}
}
}); });
console.log("Last watched: " + lastWatched)
} }
} }
}; };