Add audiobookshelf widget
This commit is contained in:
parent
c2cdb773dc
commit
a55bc357fd
6 changed files with 133 additions and 0 deletions
|
@ -540,5 +540,11 @@
|
||||||
"gross_percent_today": "Today",
|
"gross_percent_today": "Today",
|
||||||
"gross_percent_1y": "One year",
|
"gross_percent_1y": "One year",
|
||||||
"gross_percent_max": "All time"
|
"gross_percent_max": "All time"
|
||||||
|
},
|
||||||
|
"audiobookshelf": {
|
||||||
|
"podcasts": "Podcasts",
|
||||||
|
"books": "Books",
|
||||||
|
"podcastsDuration": "Duration",
|
||||||
|
"booksDuration": "Duration"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
46
src/widgets/audiobookshelf/component.jsx
Executable file
46
src/widgets/audiobookshelf/component.jsx
Executable file
|
@ -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 <Container error={librariesError} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!librariesData) {
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="audiobookshelf.podcasts" />
|
||||||
|
<Block label="audiobookshelf.podcastsDuration" />
|
||||||
|
<Block label="audiobookshelf.books" />
|
||||||
|
<Block label="audiobookshelf.booksDuration" />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="audiobookshelf.podcasts" value={t("common.number", { value: totalPodcasts })} />
|
||||||
|
<Block label="audiobookshelf.podcastsDuration" value={t("common.number", { value: totalPodcastsDuration / 60, maximumFractionDigits: 0, style: "unit", unit: "minute" })} />
|
||||||
|
<Block label="audiobookshelf.books" value={t("common.number", { value: totalBooks })} />
|
||||||
|
<Block label="audiobookshelf.booksDuration" value={t("common.number", { value: totalBooksDuration / 60, maximumFractionDigits: 0, style: "unit", unit: "minute" })} />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
64
src/widgets/audiobookshelf/proxy.js
Normal file
64
src/widgets/audiobookshelf/proxy.js
Normal file
|
@ -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}});
|
||||||
|
}
|
||||||
|
}
|
14
src/widgets/audiobookshelf/widget.js
Executable file
14
src/widgets/audiobookshelf/widget.js
Executable file
|
@ -0,0 +1,14 @@
|
||||||
|
import audiobookshelfProxyHandler from "./proxy";
|
||||||
|
|
||||||
|
const widget = {
|
||||||
|
api: "{url}/api/{endpoint}",
|
||||||
|
proxyHandler: audiobookshelfProxyHandler,
|
||||||
|
|
||||||
|
mappings: {
|
||||||
|
libraries: {
|
||||||
|
endpoint: "libraries",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default widget;
|
|
@ -2,6 +2,7 @@ import dynamic from "next/dynamic";
|
||||||
|
|
||||||
const components = {
|
const components = {
|
||||||
adguard: dynamic(() => import("./adguard/component")),
|
adguard: dynamic(() => import("./adguard/component")),
|
||||||
|
audiobookshelf: dynamic(() => import("./audiobookshelf/component")),
|
||||||
authentik: dynamic(() => import("./authentik/component")),
|
authentik: dynamic(() => import("./authentik/component")),
|
||||||
autobrr: dynamic(() => import("./autobrr/component")),
|
autobrr: dynamic(() => import("./autobrr/component")),
|
||||||
bazarr: dynamic(() => import("./bazarr/component")),
|
bazarr: dynamic(() => import("./bazarr/component")),
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import adguard from "./adguard/widget";
|
import adguard from "./adguard/widget";
|
||||||
|
import audiobookshelf from "./audiobookshelf/widget";
|
||||||
import authentik from "./authentik/widget";
|
import authentik from "./authentik/widget";
|
||||||
import autobrr from "./autobrr/widget";
|
import autobrr from "./autobrr/widget";
|
||||||
import bazarr from "./bazarr/widget";
|
import bazarr from "./bazarr/widget";
|
||||||
|
@ -75,6 +76,7 @@ import xteve from "./xteve/widget";
|
||||||
|
|
||||||
const widgets = {
|
const widgets = {
|
||||||
adguard,
|
adguard,
|
||||||
|
audiobookshelf,
|
||||||
authentik,
|
authentik,
|
||||||
autobrr,
|
autobrr,
|
||||||
bazarr,
|
bazarr,
|
||||||
|
|
Loading…
Add table
Reference in a new issue