From de11f8123415d03de4aa76eb87fc63c8228722aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Benn=C3=A0ssar=20Carretero?= Date: Thu, 27 Apr 2023 10:12:12 +0200 Subject: [PATCH 1/2] Feature: add albums to Plex service widget --- public/locales/en/common.json | 1 + src/widgets/plex/component.jsx | 2 ++ src/widgets/plex/proxy.js | 18 ++++++++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 2da02110c..d3eb43cc8 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -132,6 +132,7 @@ }, "plex": { "streams": "Active Streams", + "albums": "Albums", "movies": "Movies", "tv": "TV Shows" }, diff --git a/src/widgets/plex/component.jsx b/src/widgets/plex/component.jsx index 6fe15bd57..bd01230f0 100644 --- a/src/widgets/plex/component.jsx +++ b/src/widgets/plex/component.jsx @@ -21,6 +21,7 @@ export default function Component({ service }) { return ( + @@ -30,6 +31,7 @@ export default function Component({ service }) { return ( + diff --git a/src/widgets/plex/proxy.js b/src/widgets/plex/proxy.js index 135b2b081..0297a30ea 100644 --- a/src/widgets/plex/proxy.js +++ b/src/widgets/plex/proxy.js @@ -10,6 +10,7 @@ import widgets from "widgets/widgets"; const proxyName = "plexProxyHandler"; const librariesCacheKey = `${proxyName}__libraries`; +const albumsCacheKey = `${proxyName}__albums`; const moviesCacheKey = `${proxyName}__movies`; const tvCacheKey = `${proxyName}__tv`; const logger = createLogger(proxyName); @@ -87,9 +88,20 @@ export default async function plexProxyHandler(req, res) { } } + let albums = cache.get(`${albumsCacheKey}.${service}`); let movies = cache.get(`${moviesCacheKey}.${service}`); let tv = cache.get(`${tvCacheKey}.${service}`); - if (movies === null || tv === null) { + if (albums === null || movies === null || tv === null) { + 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; tv = 0; logger.debug("Getting movie + tv counts from Plex API"); @@ -105,14 +117,16 @@ export default async function plexProxyHandler(req, res) { } } })); + cache.put(`${albumsCacheKey}.${service}`, albums, 1000 * 60 * 10); cache.put(`${tvCacheKey}.${service}`, tv, 1000 * 60 * 10); cache.put(`${moviesCacheKey}.${service}`, movies, 1000 * 60 * 10); } const data = { streams, + albums, + movies, tv, - movies }; return res.status(status).send(data); From 9de376d51c0fb060dcc4a26317e4959ea5f16687 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 27 Apr 2023 02:12:21 -0700 Subject: [PATCH 2/2] simplify plex music logic --- src/widgets/plex/proxy.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/widgets/plex/proxy.js b/src/widgets/plex/proxy.js index 0297a30ea..855a60f9e 100644 --- a/src/widgets/plex/proxy.js +++ b/src/widgets/plex/proxy.js @@ -93,27 +93,23 @@ export default async function plexProxyHandler(req, res) { let tv = cache.get(`${tvCacheKey}.${service}`); if (albums === null || movies === null || tv === null) { 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; tv = 0; - logger.debug("Getting movie + tv counts from Plex API"); - const movieTVLibraries = libraries.filter(l => ["movie", "show"].includes(l._attributes.type)); + logger.debug("Getting counts from Plex API"); + const movieTVLibraries = libraries.filter(l => ["movie", "show", "artist"].includes(l._attributes.type)); 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) { const size = parseInt(apiData.MediaContainer._attributes.size, 10); if (library._attributes.type === "movie") { movies += size; } else if (library._attributes.type === "show") { tv += size; + } else if (library._attributes.type === "artist") { + albums += size; } } }));