Enhancement: booklore service widget (#6202)

This commit is contained in:
shamoon
2026-01-18 20:47:30 -08:00
committed by GitHub
parent 9076cfd7e7
commit 4349f30169
9 changed files with 234 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
---
title: Booklore
description: Booklore Widget Configuration
---
Learn more about [Booklore](https://github.com/booklore-app/booklore).
The widget authenticates with your Booklore credentials to surface total libraries, books, and reading progress counts for your account.
```yaml
widget:
type: booklore
url: https://booklore.host.or.ip
username: username
password: password
```

View File

@@ -17,6 +17,7 @@ You can also find a list of all available service widgets in the sidebar navigat
- [Azure DevOps](azuredevops.md) - [Azure DevOps](azuredevops.md)
- [Backrest](backrest.md) - [Backrest](backrest.md)
- [Bazarr](bazarr.md) - [Bazarr](bazarr.md)
- [Booklore](booklore.md)
- [Beszel](beszel.md) - [Beszel](beszel.md)
- [Caddy](caddy.md) - [Caddy](caddy.md)
- [Calendar](calendar.md) - [Calendar](calendar.md)

View File

@@ -41,6 +41,7 @@ nav:
- widgets/services/azuredevops.md - widgets/services/azuredevops.md
- widgets/services/backrest.md - widgets/services/backrest.md
- widgets/services/bazarr.md - widgets/services/bazarr.md
- widgets/services/booklore.md
- widgets/services/beszel.md - widgets/services/beszel.md
- widgets/services/caddy.md - widgets/services/caddy.md
- widgets/services/calendar.md - widgets/services/calendar.md

View File

@@ -793,6 +793,12 @@
"categories": "Categories", "categories": "Categories",
"series": "Series" "series": "Series"
}, },
"booklore": {
"libraries": "Libraries",
"books": "Books",
"reading": "Reading",
"finished": "Finished"
},
"jdownloader": { "jdownloader": {
"downloadCount": "Queue", "downloadCount": "Queue",
"downloadBytesRemaining": "Remaining", "downloadBytesRemaining": "Remaining",

View File

@@ -0,0 +1,43 @@
import Block from "components/services/widget/block";
import Container from "components/services/widget/container";
import { useTranslation } from "next-i18next";
import useWidgetAPI from "utils/proxy/use-widget-api";
export default function Component({ service }) {
const { t } = useTranslation();
const { widget } = service;
const { data: bookloreData, error: bookloreError } = useWidgetAPI(widget);
if (bookloreError) {
return <Container service={service} error={bookloreError} />;
}
if (!bookloreData) {
return (
<Container service={service}>
<Block label="booklore.libraries" />
<Block label="booklore.books" />
<Block label="booklore.reading" />
<Block label="booklore.finished" />
</Container>
);
}
const stats = {
libraries: bookloreData.libraries ?? 0,
books: bookloreData.books ?? 0,
reading: bookloreData.reading ?? 0,
finished: bookloreData.finished ?? 0,
};
return (
<Container service={service}>
<Block label="booklore.libraries" value={t("common.number", { value: stats.libraries })} />
<Block label="booklore.books" value={t("common.number", { value: stats.books })} />
<Block label="booklore.reading" value={t("common.number", { value: stats.reading })} />
<Block label="booklore.finished" value={t("common.number", { value: stats.finished })} />
</Container>
);
}

View File

@@ -0,0 +1,156 @@
import cache from "memory-cache";
import getServiceWidget from "utils/config/service-helpers";
import createLogger from "utils/logger";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import widgets from "widgets/widgets";
const proxyName = "bookloreProxyHandler";
const sessionTokenCacheKey = `${proxyName}__sessionToken`;
const logger = createLogger(proxyName);
async function login(widget, service) {
if (!widget.username || !widget.password) {
logger.debug("Missing credentials for Booklore service '%s'", service);
return { accessToken: false };
}
const api = widgets?.[widget.type]?.api;
const loginUrl = new URL(formatApiCall(api, { ...widget, endpoint: "auth/login" }));
const [status, , data] = await httpProxy(loginUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
accept: "application/json",
},
body: JSON.stringify({
username: widget.username,
password: widget.password,
}),
});
if (status !== 200) {
logger.debug("Booklore login failed for service '%s' with status %d", service, status);
return { accessToken: false };
}
try {
const { accessToken } = JSON.parse(data.toString());
if (accessToken) {
// access tokens are valid for ~10 hours; refresh 1 minute early.
cache.put(`${sessionTokenCacheKey}.${service}`, accessToken, 10 * 60 * 60 * 1000 - 60 * 1000);
return { accessToken };
}
} catch (e) {
logger.error("Unable to login to Booklore API: %s", e);
}
return { accessToken: false };
}
async function apiCall(widget, endpoint, service) {
const cacheKey = `${sessionTokenCacheKey}.${service}`;
let accessToken = cache.get(cacheKey);
if (!accessToken) {
({ accessToken } = await login(widget, service));
}
if (!accessToken) {
return { status: 401, data: null };
}
const headers = {
accept: "application/json",
Authorization: `Bearer ${accessToken}`,
};
const url = new URL(formatApiCall(widgets[widget.type].api, { ...widget, endpoint }));
let [status, , data] = await httpProxy(url, {
method: "GET",
headers,
});
if (status === 401 || status === 403) {
logger.debug("Booklore API rejected the request, attempting to obtain new session token");
const refreshedToken = (await login(widget, service)).accessToken;
if (!refreshedToken) {
return { status, data: null };
}
headers.Authorization = `Bearer ${refreshedToken}`;
[status, , data] = await httpProxy(url, {
method: "GET",
headers,
});
}
if (status !== 200) {
logger.error("Error getting data from Booklore: %s status %d. Data: %s", url, status, data);
return { status, data: null };
}
try {
return { status, data: JSON.parse(data.toString()) };
} catch (e) {
logger.error("Error parsing Booklore response: %s", e);
}
return { status, data: null };
}
function summarizeStatuses(books = []) {
return books.reduce(
(accumulator, book) => {
const status = (book?.readStatus || "").toString().toUpperCase();
if (status === "READING") accumulator.reading += 1;
else if (status === "READ") accumulator.finished += 1;
return accumulator;
},
{ reading: 0, finished: 0 },
);
}
export default async function bookloreProxyHandler(req, res) {
const { group, service, index } = 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, index);
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.username || !widget.password) {
logger.debug("Missing credentials for Booklore widget in service '%s'", service);
return res.status(400).json({ error: "Missing Booklore credentials" });
}
const { data: librariesData, status: librariesStatus } = await apiCall(widget, "libraries", service);
if (librariesStatus !== 200 || !Array.isArray(librariesData)) {
return res.status(librariesStatus || 500).send(librariesData || { error: "Error fetching libraries" });
}
const { data: booksData, status: booksStatus } = await apiCall(widget, "books", service);
if (booksStatus !== 200 || !Array.isArray(booksData)) {
return res.status(booksStatus || 500).send(booksData || { error: "Error fetching books" });
}
const { reading, finished } = summarizeStatuses(booksData);
return res.status(200).send({
libraries: librariesData.length,
books: booksData.length,
reading,
finished,
});
}

View File

@@ -0,0 +1,8 @@
import bookloreProxyHandler from "./proxy";
const widget = {
api: "{url}/api/v1/{endpoint}",
proxyHandler: bookloreProxyHandler,
};
export default widget;

View File

@@ -12,6 +12,7 @@ const components = {
backrest: dynamic(() => import("./backrest/component")), backrest: dynamic(() => import("./backrest/component")),
bazarr: dynamic(() => import("./bazarr/component")), bazarr: dynamic(() => import("./bazarr/component")),
beszel: dynamic(() => import("./beszel/component")), beszel: dynamic(() => import("./beszel/component")),
booklore: dynamic(() => import("./booklore/component")),
caddy: dynamic(() => import("./caddy/component")), caddy: dynamic(() => import("./caddy/component")),
calendar: dynamic(() => import("./calendar/component")), calendar: dynamic(() => import("./calendar/component")),
calibreweb: dynamic(() => import("./calibreweb/component")), calibreweb: dynamic(() => import("./calibreweb/component")),

View File

@@ -9,6 +9,7 @@ import azuredevops from "./azuredevops/widget";
import backrest from "./backrest/widget"; import backrest from "./backrest/widget";
import bazarr from "./bazarr/widget"; import bazarr from "./bazarr/widget";
import beszel from "./beszel/widget"; import beszel from "./beszel/widget";
import booklore from "./booklore/widget";
import caddy from "./caddy/widget"; import caddy from "./caddy/widget";
import calendar from "./calendar/widget"; import calendar from "./calendar/widget";
import calibreweb from "./calibreweb/widget"; import calibreweb from "./calibreweb/widget";
@@ -156,6 +157,7 @@ const widgets = {
azuredevops, azuredevops,
backrest, backrest,
bazarr, bazarr,
booklore,
beszel, beszel,
caddy, caddy,
calibreweb, calibreweb,