From 5b21444c2ef5ab0a63969f239a2bd27ea8758610 Mon Sep 17 00:00:00 2001 From: stuffinator Date: Sun, 6 Nov 2022 11:05:31 +0100 Subject: [PATCH] Add Pyload widget --- public/locales/de/common.json | 6 +++++ public/locales/en/common.json | 6 +++++ src/widgets/components.js | 1 + src/widgets/pyload/component.jsx | 27 +++++++++++++++++++++++ src/widgets/pyload/proxy.js | 38 ++++++++++++++++++++++++++++++++ src/widgets/pyload/widget.js | 8 +++++++ src/widgets/widgets.js | 2 ++ 7 files changed, 88 insertions(+) create mode 100644 src/widgets/pyload/component.jsx create mode 100644 src/widgets/pyload/proxy.js create mode 100644 src/widgets/pyload/widget.js diff --git a/public/locales/de/common.json b/public/locales/de/common.json index ec3b6e394..f13cd0282 100644 --- a/public/locales/de/common.json +++ b/public/locales/de/common.json @@ -292,5 +292,11 @@ "containers_scanned": "Scanned", "containers_updated": "Updated", "containers_failed": "Failed" + }, + "pyload": { + "speed": "Geschwindigkeit", + "active": "Aktiv", + "queue": "Warteschlange", + "total": "Gesamt" } } diff --git a/public/locales/en/common.json b/public/locales/en/common.json index dd00ff86e..913a3d9ba 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -303,5 +303,11 @@ "rejectedPushes": "Rejected", "filters": "Filters", "indexers": "Indexers" + }, + "pyload": { + "speed": "Speed", + "active": "Active", + "queue": "Queue", + "total": "Total" } } diff --git a/src/widgets/components.js b/src/widgets/components.js index c2b501890..ce5aa41a8 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -25,6 +25,7 @@ const components = { portainer: dynamic(() => import("./portainer/component")), prowlarr: dynamic(() => import("./prowlarr/component")), proxmox: dynamic(() => import("./proxmox/component")), + pyload: dynamic(() => import("./pyload/component")), qbittorrent: dynamic(() => import("./qbittorrent/component")), radarr: dynamic(() => import("./radarr/component")), readarr: dynamic(() => import("./readarr/component")), diff --git a/src/widgets/pyload/component.jsx b/src/widgets/pyload/component.jsx new file mode 100644 index 000000000..a15aab4cd --- /dev/null +++ b/src/widgets/pyload/component.jsx @@ -0,0 +1,27 @@ +import { useTranslation } from 'next-i18next' + +import Container from 'components/services/widget/container' +import Block from 'components/services/widget/block' +import useWidgetAPI from 'utils/proxy/use-widget-api' + +export default function Component({ service }) { + const { t } = useTranslation() + const { widget } = service + const { data: pyloadData, error: pyloadError } = useWidgetAPI( + widget, + 'statusServer', + ) + + if (pyloadError || !pyloadData) { + return + } + + return ( + + + + + + + ) +} diff --git a/src/widgets/pyload/proxy.js b/src/widgets/pyload/proxy.js new file mode 100644 index 000000000..35fb7becc --- /dev/null +++ b/src/widgets/pyload/proxy.js @@ -0,0 +1,38 @@ +import getServiceWidget from "utils/config/service-helpers"; +import { formatApiCall } from "utils/proxy/api-helpers"; +import widgets from "widgets/widgets"; + +export default async function pyloadProxyHandler(req, res) { + const { group, service, endpoint } = req.query; + + if (group && service) { + const widget = await getServiceWidget(group, service); + + if (widget) { + const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget })); + const loginUrl = `${widget.url}/api/login`; + + // Pyload api does not support argument passing as JSON. + const sessionId = await fetch(loginUrl, { + method: "POST", + // Empty passwords are supported. + body: `username=${widget.username}&password=${widget.password ?? ''}`, + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }).then((response) => response.json()); + + const apiResponse = await fetch(url, { + method: "POST", + body: `session=${sessionId}`, + headers: { + "Content-Type": "application/x-www-form-urlencoded", + }, + }).then((response) => response.json()); + + return res.send(apiResponse); + } + } + + return res.status(400).json({ error: "Invalid proxy service type" }); +} diff --git a/src/widgets/pyload/widget.js b/src/widgets/pyload/widget.js new file mode 100644 index 000000000..3d2f2958f --- /dev/null +++ b/src/widgets/pyload/widget.js @@ -0,0 +1,8 @@ +import pyloadProxyHandler from "./proxy"; + +const widget = { + api: "{url}/api/{endpoint}", + proxyHandler: pyloadProxyHandler, +}; + +export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index 74f426b36..eb5bec488 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -20,6 +20,7 @@ import plex from "./plex/widget"; import portainer from "./portainer/widget"; import prowlarr from "./prowlarr/widget"; import proxmox from "./proxmox/widget"; +import pyload from "./pyload/widget"; import qbittorrent from "./qbittorrent/widget"; import radarr from "./radarr/widget"; import readarr from "./readarr/widget"; @@ -58,6 +59,7 @@ const widgets = { portainer, prowlarr, proxmox, + pyload, qbittorrent, radarr, readarr,