simplify plex music logic

pull/1418/head
shamoon 2 years ago
parent de11f81234
commit 9de376d51c

@ -93,27 +93,23 @@ export default async function plexProxyHandler(req, res) {
let tv = cache.get(`${tvCacheKey}.${service}`); let tv = cache.get(`${tvCacheKey}.${service}`);
if (albums === null || movies === null || tv === null) { if (albums === null || movies === null || tv === null) {
albums = 0; albums = 0;
logger.debug("Getting album counts from Plex API");
const albumLibraries = libraries.filter(l => ["artist"].includes(l._attributes.type));
await Promise.all(albumLibraries.map(async (library) => {
[status, apiData] = await fetchFromPlexAPI(`/library/sections/${library._attributes.key}/albums`, widget);
if (apiData && apiData.MediaContainer) {
const size = parseInt(apiData.MediaContainer._attributes.size, 10);
albums += size;
}
}));
movies = 0; movies = 0;
tv = 0; tv = 0;
logger.debug("Getting movie + tv counts from Plex API"); logger.debug("Getting counts from Plex API");
const movieTVLibraries = libraries.filter(l => ["movie", "show"].includes(l._attributes.type)); const movieTVLibraries = libraries.filter(l => ["movie", "show", "artist"].includes(l._attributes.type));
await Promise.all(movieTVLibraries.map(async (library) => { await Promise.all(movieTVLibraries.map(async (library) => {
[status, apiData] = await fetchFromPlexAPI(`/library/sections/${library._attributes.key}/all`, widget); const libraryURL = ["movie", "show"].includes(library._attributes.type) ?
`/library/sections/${library._attributes.key}/all` : // tv + movies
`/library/sections/${library._attributes.key}/albums`; // music
[status, apiData] = await fetchFromPlexAPI(libraryURL, widget);
if (apiData && apiData.MediaContainer) { if (apiData && apiData.MediaContainer) {
const size = parseInt(apiData.MediaContainer._attributes.size, 10); const size = parseInt(apiData.MediaContainer._attributes.size, 10);
if (library._attributes.type === "movie") { if (library._attributes.type === "movie") {
movies += size; movies += size;
} else if (library._attributes.type === "show") { } else if (library._attributes.type === "show") {
tv += size; tv += size;
} else if (library._attributes.type === "artist") {
albums += size;
} }
} }
})); }));

Loading…
Cancel
Save