Complete Jellyfin log parsing logic
This commit is contained in:
@@ -9,8 +9,8 @@ Pebble.addEventListener("ready", function () {
|
||||
|
||||
Pebble.addEventListener("appmessage", function (dict) {
|
||||
if (dict.payload["PKJS_SLEEP_TIMESTAMP"]) {
|
||||
var sleepTimestamp = dict.payload["PKJS_SLEEP_TIMESTAMP"];
|
||||
var cfg = JSON.parse(localStorage.getItem('clay-settings'));
|
||||
const sleepTimestamp = dict.payload["PKJS_SLEEP_TIMESTAMP"] * 1000; // convert to milliseconds to later comparison
|
||||
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 use API to dynamically determine user (prompt on watch)
|
||||
if (cfg.CLAY_API_IS_JELLYFIN == true) {
|
||||
@@ -26,19 +26,32 @@ Pebble.addEventListener("appmessage", function (dict) {
|
||||
});
|
||||
|
||||
function callAPI(fullURL, apiKey, isJellyfin, trackedUser, sleepTimestamp) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = function () {
|
||||
if (xhr.readyState === 4) {
|
||||
var resp = JSON.parse(xhr.responseText);
|
||||
console.log(resp);
|
||||
const resp = JSON.parse(xhr.responseText);
|
||||
if (resp && Array.isArray(resp.Items)) {
|
||||
resp.Items.forEach(function (item, index) {
|
||||
console.log(
|
||||
"[" + index + "] Name: " + item.Name + " | Date: " + item.Date
|
||||
);
|
||||
});
|
||||
const re = /^(.+) is playing (.+) on .*$/;
|
||||
let arr;
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user