From 940ce359bf288ecf546aa07b92cb6aac0263ac53 Mon Sep 17 00:00:00 2001 From: dimitricappelle Date: Tue, 20 Jun 2023 17:05:07 +0200 Subject: [PATCH] Adding Kavita --- public/locales/en/common.json | 4 ++ src/widgets/components.js | 3 +- src/widgets/kavita/component.jsx | 33 +++++++++++ src/widgets/kavita/proxy.js | 96 ++++++++++++++++++++++++++++++++ src/widgets/kavita/widget.js | 13 +++++ src/widgets/widgets.js | 4 +- 6 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 src/widgets/kavita/component.jsx create mode 100644 src/widgets/kavita/proxy.js create mode 100644 src/widgets/kavita/widget.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 36b5a391d..718b913ff 100755 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -659,5 +659,9 @@ "downloadBytesRemaining": "Remaining", "downloadTotalBytes": "Size", "downloadSpeed": "Speed" + }, + "kavita": { + "seriesCount": "Series", + "totalFiles": "Files" } } diff --git a/src/widgets/components.js b/src/widgets/components.js index 97760d368..3359dd021 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -34,6 +34,7 @@ const components = { jdownloader: dynamic(() => import("./jdownloader/component")), jellyfin: dynamic(() => import("./emby/component")), jellyseerr: dynamic(() => import("./jellyseerr/component")), + kavita: dynamic(() => import("./kavita/component")), komga: dynamic(() => import("./komga/component")), kopia: dynamic(() => import("./kopia/component")), lidarr: dynamic(() => import("./lidarr/component")), @@ -92,4 +93,4 @@ const components = { xteve: dynamic(() => import("./xteve/component")), }; -export default components; \ No newline at end of file +export default components; diff --git a/src/widgets/kavita/component.jsx b/src/widgets/kavita/component.jsx new file mode 100644 index 000000000..f7d91a925 --- /dev/null +++ b/src/widgets/kavita/component.jsx @@ -0,0 +1,33 @@ +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: kavitaData, error: kavitaError } = useWidgetAPI(widget, "info"); + + if (kavitaError) { + return ; + } + + if (!kavitaData) { + return ( + + + + + ); + } + + return ( + + + + + ); +} diff --git a/src/widgets/kavita/proxy.js b/src/widgets/kavita/proxy.js new file mode 100644 index 000000000..786bdf1da --- /dev/null +++ b/src/widgets/kavita/proxy.js @@ -0,0 +1,96 @@ +import cache from "memory-cache"; + +import { httpProxy } from "utils/proxy/http"; +import { formatApiCall } from "utils/proxy/api-helpers"; +import getServiceWidget from "utils/config/service-helpers"; +import createLogger from "utils/logger"; +import widgets from "widgets/widgets"; + +const proxyName = "kavitaProxyHandler"; +const sessionTokenCacheKey = `${proxyName}__sessionToken`; +const logger = createLogger(proxyName); + +async function login(widget, service) { + const endpoint = "Account/login"; + const api = widgets?.[widget.type]?.api + const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget })); + const loginBody = { username: widget.username, password: widget.password }; + const headers = { "Content-Type": "application/json", "accept": "text/plain" }; + + const [, , data,] = await httpProxy(loginUrl, { + method: "POST", + body: JSON.stringify(loginBody), + headers, + }); + + try { + const { token: accessToken } = JSON.parse(data.toString()); + cache.put(`${sessionTokenCacheKey}.${service}`, accessToken); + return { accessToken }; + } catch (e) { + logger.error("Unable to login to Kavita API: %s", e); + } + + return { token: false }; +} + +async function apiCall(widget, endpoint, service) { + const key = `${sessionTokenCacheKey}.${service}`; + const headers = { + "content-type": "application/json", + "Authorization": `Bearer ${cache.get(key)}`, + } + + const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget })); + const method = "GET"; + + let [status, contentType, data, responseHeaders] = await httpProxy(url, { + method, + headers, + }); + + if (status === 401 || status === 403) { + logger.debug("Kavita API rejected the request, attempting to obtain new session token"); + const { accessToken } = await login(widget, service); + headers.Authorization = `Bearer ${accessToken}`; + + // retry the request, now with the new session token + [status, contentType, data, responseHeaders] = await httpProxy(url, { + method, + headers, + }); + } + + if (status !== 200) { + logger.error("Error getting data from Kavita: %s status %d. Data: %s", url, status, data); + return { status, contentType, data: null, responseHeaders }; + } + + return { status, contentType, data: JSON.parse(data.toString()), responseHeaders }; +} + +export default async function KavitaProxyHandler(req, res) { + const { group, service } = req.query; + + if (!group || !service) { + logger.debug("Invalid or missing service '%s' or group '%s'", service, group); + return res.status(400).json({ error: "Invalid proxy service type" }); + } + + const widget = await getServiceWidget(group, service); + if (!widget) { + logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); + return res.status(400).json({ error: "Invalid proxy service type" }); + } + + if (!cache.get(`${sessionTokenCacheKey}.${service}`)) { + await login(widget, service); + } + + const { data: statsData } = await apiCall(widget, "Stats/server/stats", service); + + return res.status(200).send({ + seriesCount: statsData?.seriesCount, + totalFiles: statsData?.totalFiles + }); +} diff --git a/src/widgets/kavita/widget.js b/src/widgets/kavita/widget.js new file mode 100644 index 000000000..cb35f03c4 --- /dev/null +++ b/src/widgets/kavita/widget.js @@ -0,0 +1,13 @@ +import kavitaProxyHandler from "./proxy"; + +const widget = { +api: "{url}/api/{endpoint}", + proxyHandler: kavitaProxyHandler, + mappings: { + info: { + endpoint: "/" + } + } +}; + +export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index 86c4266fc..fc2094141 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -28,6 +28,7 @@ import immich from "./immich/widget"; import jackett from "./jackett/widget"; import jellyseerr from "./jellyseerr/widget"; import jdownloader from "./jdownloader/widget"; +import kavita from "./kavita/widget"; import komga from "./komga/widget"; import kopia from "./kopia/widget"; import lidarr from "./lidarr/widget"; @@ -100,7 +101,7 @@ const widgets = { diskstation, downloadstation, emby, - evcc, + evcc, fileflows, flood, freshrss, @@ -117,6 +118,7 @@ const widgets = { jdownloader, jellyfin: emby, jellyseerr, + kavita, komga, kopia, lidarr,