Merge pull request #1085 from benphelps/audiobookshelf-widget
Add audiobookshelf widgetpull/1124/head
commit
70a15838d5
@ -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>
|
||||
);
|
||||
}
|
@ -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}});
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import audiobookshelfProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: audiobookshelfProxyHandler,
|
||||
|
||||
mappings: {
|
||||
libraries: {
|
||||
endpoint: "libraries",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue