From df0625550b71d8527f28eb02c1dd7523699fec59 Mon Sep 17 00:00:00 2001 From: Karl Hudgell Date: Tue, 6 Jun 2023 18:12:07 +0100 Subject: [PATCH 1/4] JDRssDownloader --- public/locales/en/common.json | 5 ++ src/widgets/components.js | 1 + src/widgets/jdrssdownloader/component.jsx | 37 +++++++++ src/widgets/jdrssdownloader/proxy.js | 97 +++++++++++++++++++++++ src/widgets/jdrssdownloader/widget.js | 14 ++++ src/widgets/widgets.js | 2 + 6 files changed, 156 insertions(+) create mode 100644 src/widgets/jdrssdownloader/component.jsx create mode 100644 src/widgets/jdrssdownloader/proxy.js create mode 100644 src/widgets/jdrssdownloader/widget.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 37f7e233..873586e5 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -653,5 +653,10 @@ "nextpvr": { "upcoming": "Upcoming", "ready": "Recent" + }, + "jdrssdownloader": { + "totalShows": "Tracked Shows", + "retryCache": "Retry Cache", + "feedCache": "Feed Cache" } } \ No newline at end of file diff --git a/src/widgets/components.js b/src/widgets/components.js index 9916d601..b6a7ca82 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -31,6 +31,7 @@ const components = { healthchecks: dynamic(() => import("./healthchecks/component")), immich: dynamic(() => import("./immich/component")), jackett: dynamic(() => import("./jackett/component")), + jdrssdownloader: dynamic(() => import("./jdrssdownloader/component")), jellyfin: dynamic(() => import("./emby/component")), jellyseerr: dynamic(() => import("./jellyseerr/component")), komga: dynamic(() => import("./komga/component")), diff --git a/src/widgets/jdrssdownloader/component.jsx b/src/widgets/jdrssdownloader/component.jsx new file mode 100644 index 00000000..1790e9a5 --- /dev/null +++ b/src/widgets/jdrssdownloader/component.jsx @@ -0,0 +1,37 @@ +import { useTranslation } from "next-i18next"; + +import Block from "components/services/widget/block"; +import Container from "components/services/widget/container"; +import useWidgetAPI from "utils/proxy/use-widget-api"; + +export default function Component({ service }) { + const { t } = useTranslation(); + + const { widget } = service; + + const { data: wgeasyData, error: wgeasyAPIError } = useWidgetAPI(widget, "unified", { + refreshInterval: 60000, + }); + + if (wgeasyAPIError) { + return ; + } + + if (!wgeasyData) { + return ( + + + + + + ); + } + + return ( + + + + + + ); +} \ No newline at end of file diff --git a/src/widgets/jdrssdownloader/proxy.js b/src/widgets/jdrssdownloader/proxy.js new file mode 100644 index 00000000..99452692 --- /dev/null +++ b/src/widgets/jdrssdownloader/proxy.js @@ -0,0 +1,97 @@ +/* eslint-disable no-underscore-dangle */ +import { formatApiCall } from "utils/proxy/api-helpers"; +import { httpProxy } from "utils/proxy/http"; +import getServiceWidget from "utils/config/service-helpers"; +import createLogger from "utils/logger"; +import widgets from "widgets/widgets"; + + +const proxyName = "JDRssProxyHandler"; + +const logger = createLogger(proxyName); + +async function getWidget(req) { + const { group, service } = req.query; + if (!group || !service) { + logger.debug("Invalid or missing service '%s' or group '%s'", service, group); + return null; + } + const widget = await getServiceWidget(group, service); + if (!widget) { + logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); + return null; + } + + return widget; +} + +async function fetchDataFromJDRss(endpoint, widget) { + const api = widgets?.[widget.type]?.api; + if (!api) { + return [403, null]; + } + const url = `${new URL(formatApiCall(api, { endpoint, ...widget }))}` + const [status, contentType, data] = await httpProxy(url, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + }, + }); + + if (status !== 200) { + logger.error("HTTP %d communicating with JDRss. Data: %s", status, data.toString()); + return [status, data]; + } + + try { + return [status, JSON.parse(data), contentType]; + } catch (e) { + logger.error("Error decoding JDRss API data. Data: %s", data.toString()); + return [status, null]; + } +} + +export default async function JDRssProxyHandler(req, res) { + const widget = await getWidget(req); + + if (!widget) { + return res.status(400).json({ error: "Invalid proxy service type" }); + } + + logger.debug("Getting data from JDRss API"); + // Calculate the number of Tracked Shows + let [status, apiData] = await fetchDataFromJDRss('shows', widget); + + if (status !== 200) { + return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); + } + let showCount; + showCount = apiData.length; + + // Calculate the number of items in the retry cache + [status, apiData] = await fetchDataFromJDRss('retryCache', widget); + if (status !== 200) { + return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); + } + let retryCache; + retryCache = apiData.length; + + // Calculate the number of items in the feed cache + [status, apiData] = await fetchDataFromJDRss('feedCache', widget); + if (status !== 200) { + return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); + } + let feedCache; + feedCache = apiData.length; + + const data = { + showCount:showCount, + retryCache:retryCache, + feedCache:feedCache + + }; + + return res.status(status).send(data); + +} + diff --git a/src/widgets/jdrssdownloader/widget.js b/src/widgets/jdrssdownloader/widget.js new file mode 100644 index 00000000..d372d0cf --- /dev/null +++ b/src/widgets/jdrssdownloader/widget.js @@ -0,0 +1,14 @@ +import JDRssProxyHandler from "./proxy"; + +const widget = { + api: "{url}/api/{endpoint}", + proxyHandler: JDRssProxyHandler, + + mappings: { + unified: { + endpoint: "/", + }, + }, +}; + +export default widget; \ No newline at end of file diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index ff770939..a2a00ff7 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -84,6 +84,7 @@ import uptimekuma from "./uptimekuma/widget"; import watchtower from "./watchtower/widget"; import whatsupdocker from "./whatsupdocker/widget"; import xteve from "./xteve/widget"; +import jdrssdownloader from "./jdrssdownloader/widget"; const widgets = { adguard, @@ -115,6 +116,7 @@ const widgets = { immich, jackett, jellyfin: emby, + jdrssdownloader, jellyseerr, komga, kopia, From 84acccba83ba224fe2c129a21aa1d8928151514a Mon Sep 17 00:00:00 2001 From: Karl Hudgell Date: Tue, 6 Jun 2023 18:30:00 +0100 Subject: [PATCH 2/4] linting fixes --- src/widgets/jdrssdownloader/proxy.js | 12 ++++++------ src/widgets/wgeasy/proxy.js | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/widgets/jdrssdownloader/proxy.js b/src/widgets/jdrssdownloader/proxy.js index 99452692..71c2a945 100644 --- a/src/widgets/jdrssdownloader/proxy.js +++ b/src/widgets/jdrssdownloader/proxy.js @@ -65,7 +65,7 @@ export default async function JDRssProxyHandler(req, res) { if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let showCount; + let showCount = 0; showCount = apiData.length; // Calculate the number of items in the retry cache @@ -73,7 +73,7 @@ export default async function JDRssProxyHandler(req, res) { if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let retryCache; + let retryCache = 0; retryCache = apiData.length; // Calculate the number of items in the feed cache @@ -81,13 +81,13 @@ export default async function JDRssProxyHandler(req, res) { if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let feedCache; + let feedCache = 0; feedCache = apiData.length; const data = { - showCount:showCount, - retryCache:retryCache, - feedCache:feedCache + showCount, + retryCache, + feedCache }; diff --git a/src/widgets/wgeasy/proxy.js b/src/widgets/wgeasy/proxy.js index 233e80da..40ddd792 100644 --- a/src/widgets/wgeasy/proxy.js +++ b/src/widgets/wgeasy/proxy.js @@ -33,9 +33,9 @@ async function loginToWGEasy(endpoint, widget) { return [403, null]; } // Create new session on WgEasy - let url = new URL(formatApiCall(api, { endpoint, ...widget })); + const url = new URL(formatApiCall(api, { endpoint, ...widget })); - let [status, contentType, data, responseHeaders] = await httpProxy(url, { + const [status, data, , responseHeaders] = await httpProxy(url, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -50,7 +50,7 @@ async function loginToWGEasy(endpoint, widget) { return [status, data, responseHeaders]; } try { - globalSid = responseHeaders["set-cookie"][0] + [ globalSid ] = responseHeaders["set-cookie"] } catch (e) { logger.error("Error decoding NextPVR API data. Data: %s", data.toString()); return [status, null]; @@ -99,14 +99,14 @@ export default async function WGeasyProxyHandler(req, res) { logger.debug("Getting data from WGeasy API"); // Calculate the number of clients - let [status, apiData] = await fetchDataFromWGeasy('wireguard/client', widget, globalSid); + const [status, apiData] = await fetchDataFromWGeasy('wireguard/client', widget, globalSid); if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let clientCount; + let clientCount = 0; clientCount = apiData.length; - + const data = { clientCount }; From a01598c0b4ad3deaf55249d21b147ffcc43d6d74 Mon Sep 17 00:00:00 2001 From: Karl Hudgell Date: Wed, 7 Jun 2023 08:48:01 +0100 Subject: [PATCH 3/4] linting fixes --- src/widgets/jdrssdownloader/proxy.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/widgets/jdrssdownloader/proxy.js b/src/widgets/jdrssdownloader/proxy.js index 99452692..9b98c0a1 100644 --- a/src/widgets/jdrssdownloader/proxy.js +++ b/src/widgets/jdrssdownloader/proxy.js @@ -65,7 +65,7 @@ export default async function JDRssProxyHandler(req, res) { if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let showCount; + let showCount = 0; showCount = apiData.length; // Calculate the number of items in the retry cache @@ -73,7 +73,7 @@ export default async function JDRssProxyHandler(req, res) { if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let retryCache; + let retryCache = 0; retryCache = apiData.length; // Calculate the number of items in the feed cache @@ -81,17 +81,16 @@ export default async function JDRssProxyHandler(req, res) { if (status !== 200) { return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); } - let feedCache; + let feedCache = 0; feedCache = apiData.length; const data = { - showCount:showCount, - retryCache:retryCache, - feedCache:feedCache + showCount, + retryCache, + feedCache }; return res.status(status).send(data); } - From 9ae891dad7d95c6cdeb261e4d61b8d525a5be3f1 Mon Sep 17 00:00:00 2001 From: Karl Hudgell Date: Wed, 7 Jun 2023 08:52:22 +0100 Subject: [PATCH 4/4] new endpoint --- src/widgets/jdrssdownloader/proxy.js | 31 ++++++---------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/src/widgets/jdrssdownloader/proxy.js b/src/widgets/jdrssdownloader/proxy.js index 9b98c0a1..c301b447 100644 --- a/src/widgets/jdrssdownloader/proxy.js +++ b/src/widgets/jdrssdownloader/proxy.js @@ -59,35 +59,16 @@ export default async function JDRssProxyHandler(req, res) { } logger.debug("Getting data from JDRss API"); - // Calculate the number of Tracked Shows - let [status, apiData] = await fetchDataFromJDRss('shows', widget); + // Get data from JDRss stats API + const [status, apiData] = await fetchDataFromJDRss('stats', widget); if (status !== 200) { - return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); + return res.status(status).json({ error: { message: "HTTP error communicating with JDRss API", data: Buffer.from(apiData).toString() } }); } - let showCount = 0; - showCount = apiData.length; - - // Calculate the number of items in the retry cache - [status, apiData] = await fetchDataFromJDRss('retryCache', widget); - if (status !== 200) { - return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); - } - let retryCache = 0; - retryCache = apiData.length; - - // Calculate the number of items in the feed cache - [status, apiData] = await fetchDataFromJDRss('feedCache', widget); - if (status !== 200) { - return res.status(status).json({ error: { message: "HTTP error communicating with WGeasy API", data: Buffer.from(apiData).toString() } }); - } - let feedCache = 0; - feedCache = apiData.length; - const data = { - showCount, - retryCache, - feedCache + showCount:apiData.ShowList , + retryCache:apiData.RetryCache, + feedCache: apiData.FeedCache };