mirror of
https://git.wetfence.xyz/RandyTheSilly/jellyPod.git
synced 2026-10-26 23:39:56 -04:00
Redesign Jellyfin API data operations
This commit is contained in:
1 parent
79045d8b4b
commit
f7b4805ba1
2 files changed
+50
-72
No files matched your search
+39
-46
@@ -7,7 +7,7 @@ var cfg = JSON.parse(localStorage.getItem('clay-settings'));
|
||||
Pebble.addEventListener("ready", function () {
|
||||
// store cache of library info from Jellyfin
|
||||
if (cfg.PRIV_BASE_URL && cfg.PRIV_API_KEY && cfg.PRIV_USERNAME) {
|
||||
// get userId
|
||||
// get userId from Jellyfin
|
||||
callAPI("GET", cfg.PRIV_BASE_URL + "/Users", function (userIdResp) {
|
||||
if (userIdResp === null) { return; }
|
||||
const userIdObj = JSON.parse(userIdResp);
|
||||
@@ -19,62 +19,55 @@ Pebble.addEventListener("ready", function () {
|
||||
}
|
||||
}
|
||||
|
||||
// get library
|
||||
callAPI("GET", cfg.PRIV_BASE_URL + "/Items?userId=" + userId + "&recursive=true&includeItemTypes=Audio&sortBy=AlbumArtist,Artist,ProductionYear,Album,IndexNumber,Name'", function (libResp) {
|
||||
// get library response from Jellyfin
|
||||
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;
|
||||
// format response for jellyPod
|
||||
//// build tracks array and lookupMap (currently just binds alpha to artists)
|
||||
let jpTracksArr = [];
|
||||
const lookupMap = new Map();
|
||||
let lastAlbumInfo = {AlbumArtist: "", AlbumName: "", AlbumYear: 0};
|
||||
for (const track of libObj.Items) {
|
||||
if (!track.Name) {
|
||||
continue;
|
||||
}
|
||||
let albumArtist = "unknown artist";
|
||||
if (track.AlbumArtist) {
|
||||
if (track.AlbumArtist != lastArtist) {
|
||||
artists.push(track.AlbumArtist);
|
||||
lastArtist = track.AlbumArtist;
|
||||
}
|
||||
albumArtist = track.AlbumArtist;
|
||||
} else if (track.Artists.length > 0) {
|
||||
if (track.Artists[0] != lastArtist) {
|
||||
artists.push(track.Artists[0]);
|
||||
lastArtist = track.Artists[0];
|
||||
albumArtist = track.Artists[0]
|
||||
}
|
||||
const albumName = track.Album ? track.Album : "unknown album";
|
||||
if (lastAlbumInfo.AlbumArtist != albumArtist) {
|
||||
firstChar = Array.from(albumArtist)[0].toUpperCase();
|
||||
if (!lookupMap.has(firstChar)) {
|
||||
lookupMap.set(firstChar, [albumArtist]);
|
||||
} else {
|
||||
lookupMap.get(firstChar).push(albumArtist);
|
||||
};
|
||||
if (lastAlbumInfo.AlbumName != albumName) {
|
||||
lastAlbumInfo = {
|
||||
AlbumArtist: albumArtist,
|
||||
AlbumName: track.Album ? track.Album : "unknown album",
|
||||
AlbumYear: track.ProductionYear ? track.ProductionYear : 1970
|
||||
};
|
||||
}
|
||||
} else if (!unknownFound) {
|
||||
unknownFound = true;
|
||||
}
|
||||
jpTracksArr.push({
|
||||
TrackName: track.Name,
|
||||
JfId: track.Id,
|
||||
AlbumInfo: lastAlbumInfo
|
||||
});
|
||||
}
|
||||
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);
|
||||
//console.log(jpTracksArr);
|
||||
//console.log("Items:", libObj.Items.length, "Tracks:", jpTracksArr.length);
|
||||
//console.log(...lookupMap);
|
||||
|
||||
Pebble.sendAppMessage({ PKJS_READY: 1 });
|
||||
});
|
||||
|
||||
Pebble.sendAppMessage({ PKJS_READY: 1 });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user