From 33cf69964a2ea6108acbce83992d510e510c69ec Mon Sep 17 00:00:00 2001 From: Ben Phelps Date: Thu, 25 Aug 2022 02:48:52 +0300 Subject: [PATCH] add nzbget service widget --- package.json | 1 + pnpm-lock.yaml | 6 ++ src/components/services/widget.jsx | 2 + src/components/services/widgets/nzbget.jsx | 85 ++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/components/services/widgets/nzbget.jsx diff --git a/package.json b/package.json index c8646bde2..f9d6b946d 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@tailwindcss/forms": "^0.5.2", "dockerode": "^3.3.3", "js-yaml": "^4.1.0", + "json-rpc-2.0": "^1.3.0", "memory-cache": "^0.2.0", "next": "12.2.5", "node-os-utils": "^1.3.7", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a026931aa..e53cc8e8c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,7 @@ specifiers: eslint: 8.22.0 eslint-config-next: 12.2.5 js-yaml: ^4.1.0 + json-rpc-2.0: ^1.3.0 memory-cache: ^0.2.0 next: 12.2.5 node-os-utils: ^1.3.7 @@ -25,6 +26,7 @@ dependencies: '@tailwindcss/forms': 0.5.2_tailwindcss@3.1.8 dockerode: 3.3.3 js-yaml: 4.1.0 + json-rpc-2.0: 1.3.0 memory-cache: 0.2.0 next: 12.2.5_biqbaboplfbrettd7655fr4n2y node-os-utils: 1.3.7 @@ -1489,6 +1491,10 @@ packages: dependencies: argparse: 2.0.1 + /json-rpc-2.0/1.3.0: + resolution: {integrity: sha512-pA85D9LG4B+b8njdb8DzktltNfGPcxGJJydEg6jRqePfwhK008lRdbX7bco6aokfw9oDFw7fEe+LRxxXwP11HA==} + dev: false + /json-schema-traverse/0.4.1: resolution: {integrity: sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==} dev: true diff --git a/src/components/services/widget.jsx b/src/components/services/widget.jsx index 57aad5c2a..ccbb91762 100644 --- a/src/components/services/widget.jsx +++ b/src/components/services/widget.jsx @@ -3,6 +3,7 @@ import Radarr from "./widgets/radarr"; import Ombi from "./widgets/ombi"; import Portainer from "./widgets/portainer"; import Emby from "./widgets/emby"; +import Nzbget from "./widgets/nzbget"; const widgetMappings = { sonarr: Sonarr, @@ -10,6 +11,7 @@ const widgetMappings = { ombi: Ombi, portainer: Portainer, emby: Emby, + nzbget: Nzbget, }; export default function Widget({ service }) { diff --git a/src/components/services/widgets/nzbget.jsx b/src/components/services/widgets/nzbget.jsx new file mode 100644 index 000000000..ae34677ae --- /dev/null +++ b/src/components/services/widgets/nzbget.jsx @@ -0,0 +1,85 @@ +import useSWR from "swr"; +import { JSONRPCClient } from "json-rpc-2.0"; + +import { formatBytes } from "utils/stats-helpers"; + +export default function Nzbget({ service }) { + const config = service.widget; + + const constructedUrl = new URL(config.url); + constructedUrl.pathname = "jsonrpc"; + + const client = new JSONRPCClient((jsonRPCRequest) => + fetch(constructedUrl.toString(), { + method: "POST", + headers: { + "content-type": "application/json", + authorization: `Basic ${btoa(`${config.username}:${config.password}`)}`, + }, + body: JSON.stringify(jsonRPCRequest), + }).then(async (response) => { + if (response.status === 200) { + const jsonRPCResponse = await response.json(); + return client.receive(jsonRPCResponse); + } else if (jsonRPCRequest.id !== undefined) { + return Promise.reject(new Error(response.statusText)); + } + }) + ); + + const { data: statusData, error: statusError } = useSWR( + "status", + (resource) => { + return client.request(resource).then((response) => response); + }, + { + refreshInterval: 1000, + } + ); + + if (statusError) { + return ( +
+
Nzbget API Error
+
+ ); + } + + if (!statusData) { + return ( +
+
+
-
+
RATE
+
+
+
-
+
REMAINING
+
+
+
-
+
DOWNLOADED
+
+
+ ); + } + + console.log(statusData); + + return ( +
+
+
{formatBytes(statusData.DownloadRate)}/s
+
RATE
+
+
+
{Math.round((statusData.RemainingSizeMB / 1024) * 100) / 100} GB
+
REMAINING
+
+
+
{Math.round((statusData.DownloadedSizeMB / 1024) * 100) / 100} GB
+
DOWNLOADED
+
+
+ ); +}