Significant Jellyfin API work on the PKJS side
This commit is contained in:
1 parent
317241d294
commit
79045d8b4b
5 files changed
+192
-47
No files matched your search
+92
-7
@@ -2,32 +2,117 @@ var Clay = require('@rebble/clay');
|
||||
var clayConfig = require('./config');
|
||||
new Clay(clayConfig);
|
||||
|
||||
var cfg = JSON.parse(localStorage.getItem('clay-settings'));
|
||||
|
||||
Pebble.addEventListener("ready", function () {
|
||||
Pebble.sendAppMessage({ PKJS_READY: 1 });
|
||||
// store cache of library info from Jellyfin
|
||||
if (cfg.PRIV_BASE_URL && cfg.PRIV_API_KEY && cfg.PRIV_USERNAME) {
|
||||
// get userId
|
||||
callAPI("GET", cfg.PRIV_BASE_URL + "/Users", function (userIdResp) {
|
||||
if (userIdResp === null) { return; }
|
||||
const userIdObj = JSON.parse(userIdResp);
|
||||
let userId;
|
||||
for (const user of userIdObj) {
|
||||
if (user.Name === cfg.PRIV_USERNAME) {
|
||||
userId = user.Id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// get library
|
||||
callAPI("GET", cfg.PRIV_BASE_URL + "/Items?userId=" + userId + "&recursive=true&includeItemTypes=Audio&sortBy=AlbumArtist,Artist,ProductionYear,Album,IndexNumber,Name'", function (libResp) {
|
||||
if (libResp === null) { return; }
|
||||
const libObj = JSON.parse(libResp);
|
||||
// extraction output:
|
||||
// a map of artists to arrays of albums
|
||||
// a map of albums to arrays of contained tracks
|
||||
|
||||
// extract alphanumerical list of artists
|
||||
let artists = [];
|
||||
let lastArtist = "";
|
||||
let unknownFound = false;
|
||||
for (const track of libObj.Items) {
|
||||
if (track.AlbumArtist) {
|
||||
if (track.AlbumArtist != lastArtist) {
|
||||
artists.push(track.AlbumArtist);
|
||||
lastArtist = track.AlbumArtist;
|
||||
}
|
||||
} else if (track.Artists.length > 0) {
|
||||
if (track.Artists[0] != lastArtist) {
|
||||
artists.push(track.Artists[0]);
|
||||
lastArtist = track.Artists[0];
|
||||
}
|
||||
} else if (!unknownFound) {
|
||||
unknownFound = true;
|
||||
}
|
||||
}
|
||||
if (unknownFound) {
|
||||
artists.push("unknown artist");
|
||||
}
|
||||
console.log(artists);
|
||||
|
||||
// sort artists into arrays mapped to first characters
|
||||
const charMap = new Map();
|
||||
let currentChar = "";
|
||||
let currentArr = [];
|
||||
for (const artistName of artists) {
|
||||
char = Array.from(artistName)[0].toUpperCase();
|
||||
if (currentChar == "") {
|
||||
currentChar = char;
|
||||
}
|
||||
if (char == currentChar) {
|
||||
currentArr.push(artistName);
|
||||
} else {
|
||||
charMap.set(currentChar, currentArr);
|
||||
currentChar = char;
|
||||
currentArr = [artistName];
|
||||
}
|
||||
}
|
||||
if (currentChar !== "") {
|
||||
charMap.set(currentChar, currentArr);
|
||||
}
|
||||
console.log(...charMap);
|
||||
});
|
||||
|
||||
Pebble.sendAppMessage({ PKJS_READY: 1 });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function callAPI(method, endpoint) {
|
||||
Pebble.addEventListener("appmessage", function (dict) {
|
||||
if (dict.payload["OUT_MENU_NAV_SCOPE"]) {
|
||||
//const cfg = JSON.parse(localStorage.getItem('clay-settings'));
|
||||
//callAPI(cfg.PKJS_PAVLOK_API_KEY, parseInt(dict.payload["PKJS_PAVLOK_ZAP"]));
|
||||
}
|
||||
});
|
||||
|
||||
function reportAPIFailure() {
|
||||
console.log("API FAILURE");
|
||||
}
|
||||
|
||||
function callAPI(method, endpoint, callback) {
|
||||
const xhr = new XMLHttpRequest();
|
||||
|
||||
xhr.onload = function () {
|
||||
switch (xhr.status) {
|
||||
case 200:
|
||||
return this.responseText;
|
||||
callback(this.responseText);
|
||||
return;
|
||||
default:
|
||||
reportAPIFailure(); // TODO
|
||||
reportAPIFailure();
|
||||
}
|
||||
};
|
||||
xhr.onerror = function () {
|
||||
reportAPIFailure(); // TODO
|
||||
reportAPIFailure();
|
||||
};
|
||||
xhr.ontimeout = function () {
|
||||
reportAPIFailure(); // TODO
|
||||
reportAPIFailure();
|
||||
};
|
||||
|
||||
xhr.open(method, endpoint, true);
|
||||
xhr.timeout = 8000;
|
||||
xhr.setRequestHeader("Accept", "application/json");
|
||||
xhr.setRequestHeader("Authorization", 'MediaBrowser Token="' + apiKey + '"'); // TODO
|
||||
xhr.setRequestHeader("Authorization", 'MediaBrowser Token="' + cfg.PRIV_API_KEY + '"');
|
||||
xhr.send();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user