From a55bc357fdc71dec9bbd5bb03187536df83202d0 Mon Sep 17 00:00:00 2001
From: shamoon <4887959+shamoon@users.noreply.github.com>
Date: Sun, 5 Mar 2023 23:34:46 -0800
Subject: [PATCH] Add audiobookshelf widget
---
public/locales/en/common.json | 6 +++
src/widgets/audiobookshelf/component.jsx | 46 +++++++++++++++++
src/widgets/audiobookshelf/proxy.js | 64 ++++++++++++++++++++++++
src/widgets/audiobookshelf/widget.js | 14 ++++++
src/widgets/components.js | 1 +
src/widgets/widgets.js | 2 +
6 files changed, 133 insertions(+)
create mode 100755 src/widgets/audiobookshelf/component.jsx
create mode 100644 src/widgets/audiobookshelf/proxy.js
create mode 100755 src/widgets/audiobookshelf/widget.js
diff --git a/public/locales/en/common.json b/public/locales/en/common.json
index a9d6ed34c..6e89bf9fc 100755
--- a/public/locales/en/common.json
+++ b/public/locales/en/common.json
@@ -540,5 +540,11 @@
"gross_percent_today": "Today",
"gross_percent_1y": "One year",
"gross_percent_max": "All time"
+ },
+ "audiobookshelf": {
+ "podcasts": "Podcasts",
+ "books": "Books",
+ "podcastsDuration": "Duration",
+ "booksDuration": "Duration"
}
}
diff --git a/src/widgets/audiobookshelf/component.jsx b/src/widgets/audiobookshelf/component.jsx
new file mode 100755
index 000000000..2ac38be0d
--- /dev/null
+++ b/src/widgets/audiobookshelf/component.jsx
@@ -0,0 +1,46 @@
+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: librariesData, error: librariesError } = useWidgetAPI(widget, "libraries");
+
+
+ if (librariesError) {
+ return ;
+ }
+
+ if (!librariesData) {
+ return (
+
+
+
+
+
+
+ );
+ }
+
+ const podcastLibraries = librariesData.filter(l => l.mediaType === "podcast");
+ const bookLibraries = librariesData.filter(l => l.mediaType === "book");
+
+ const totalPodcasts = podcastLibraries.reduce((total, pL) => parseInt(pL.stats?.totalItems, 10) + total, 0);
+ const totalBooks = bookLibraries.reduce((total, bL) => parseInt(bL.stats?.totalItems, 10) + total, 0);
+
+ const totalPodcastsDuration = podcastLibraries.reduce((total, pL) => parseFloat(pL.stats?.totalDuration) + total, 0);
+ const totalBooksDuration = bookLibraries.reduce((total, bL) => parseFloat(bL.stats?.totalDuration) + total, 0);
+
+ return (
+
+
+
+
+
+
+ );
+}
diff --git a/src/widgets/audiobookshelf/proxy.js b/src/widgets/audiobookshelf/proxy.js
new file mode 100644
index 000000000..cdcb51830
--- /dev/null
+++ b/src/widgets/audiobookshelf/proxy.js
@@ -0,0 +1,64 @@
+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 = "audiobookshelfProxyHandler";
+const logger = createLogger(proxyName);
+
+async function retrieveFromAPI(url, key) {
+ const headers = {
+ "content-type": "application/json",
+ "Authorization": `Bearer ${key}`
+ };
+
+ const [status, , data] = await httpProxy(url, { headers });
+
+ if (status !== 200) {
+ throw new Error(`Error getting data from Audiobookshelf: ${status}. Data: ${data.toString()}`);
+ }
+
+ return JSON.parse(Buffer.from(data).toString());
+}
+
+export default async function audiobookshelfProxyHandler(req, res) {
+ const { group, service, endpoint } = 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 (!widget.key) {
+ logger.debug("Invalid or missing key for service '%s' in group '%s'", service, group);
+ return res.status(400).json({ error: "Missing widget key" });
+ }
+
+ const apiURL = widgets[widget.type].api;
+
+ try {
+ const url = new URL(formatApiCall(apiURL, { endpoint, ...widget }));
+ const libraryData = await retrieveFromAPI(url, widget.key);
+
+ const libraryStats = await Promise.all(libraryData.libraries.map(async l => {
+ const stats = await retrieveFromAPI(new URL(formatApiCall(apiURL, { endpoint: `libraries/${l.id}/stats`, ...widget })), widget.key);
+ return {
+ ...l,
+ stats
+ };
+ }));
+
+ return res.status(200).send(libraryStats);
+ } catch (e) {
+ logger.error(e.message);
+ return res.status(500).send({error: {message: e.message}});
+ }
+}
diff --git a/src/widgets/audiobookshelf/widget.js b/src/widgets/audiobookshelf/widget.js
new file mode 100755
index 000000000..011f726a0
--- /dev/null
+++ b/src/widgets/audiobookshelf/widget.js
@@ -0,0 +1,14 @@
+import audiobookshelfProxyHandler from "./proxy";
+
+const widget = {
+ api: "{url}/api/{endpoint}",
+ proxyHandler: audiobookshelfProxyHandler,
+
+ mappings: {
+ libraries: {
+ endpoint: "libraries",
+ },
+ },
+};
+
+export default widget;
\ No newline at end of file
diff --git a/src/widgets/components.js b/src/widgets/components.js
index cfd3d6654..aa54e2462 100644
--- a/src/widgets/components.js
+++ b/src/widgets/components.js
@@ -2,6 +2,7 @@ import dynamic from "next/dynamic";
const components = {
adguard: dynamic(() => import("./adguard/component")),
+ audiobookshelf: dynamic(() => import("./audiobookshelf/component")),
authentik: dynamic(() => import("./authentik/component")),
autobrr: dynamic(() => import("./autobrr/component")),
bazarr: dynamic(() => import("./bazarr/component")),
diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js
index 0716c1d63..ab2d44b2f 100644
--- a/src/widgets/widgets.js
+++ b/src/widgets/widgets.js
@@ -1,4 +1,5 @@
import adguard from "./adguard/widget";
+import audiobookshelf from "./audiobookshelf/widget";
import authentik from "./authentik/widget";
import autobrr from "./autobrr/widget";
import bazarr from "./bazarr/widget";
@@ -75,6 +76,7 @@ import xteve from "./xteve/widget";
const widgets = {
adguard,
+ audiobookshelf,
authentik,
autobrr,
bazarr,