Add Shoko widget

This commit is contained in:
Seow Alex 2023-05-10 17:04:23 +08:00
parent 866bb1c330
commit 42e8d1bd21
6 changed files with 137 additions and 0 deletions

View File

@ -614,5 +614,11 @@
"whatsupdocker": { "whatsupdocker": {
"monitoring": "Monitoring", "monitoring": "Monitoring",
"updates": "Updates" "updates": "Updates"
},
"shoko": {
"series": "TV Series",
"movies": "Movies",
"ovas": "OVAs",
"others": "Others"
} }
} }

View File

@ -69,6 +69,7 @@ const components = {
rutorrent: dynamic(() => import("./rutorrent/component")), rutorrent: dynamic(() => import("./rutorrent/component")),
sabnzbd: dynamic(() => import("./sabnzbd/component")), sabnzbd: dynamic(() => import("./sabnzbd/component")),
scrutiny: dynamic(() => import("./scrutiny/component")), scrutiny: dynamic(() => import("./scrutiny/component")),
shoko: dynamic(() => import("./shoko/component")),
sonarr: dynamic(() => import("./sonarr/component")), sonarr: dynamic(() => import("./sonarr/component")),
speedtest: dynamic(() => import("./speedtest/component")), speedtest: dynamic(() => import("./speedtest/component")),
strelaysrv: dynamic(() => import("./strelaysrv/component")), strelaysrv: dynamic(() => import("./strelaysrv/component")),

View File

@ -0,0 +1,42 @@
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: shokoData, error: shokoError } = useWidgetAPI(widget);
if (shokoError) {
return <Container service={service} error={shokoError} />;
}
if (!shokoData) {
return (
<Container service={service}>
<Block label="shoko.series" />
<Block label="shoko.movies" />
<Block label="shoko.ovas" />
<Block label="shoko.others" />
</Container>
);
}
const { Series: series, Movie: movies, OVA: ovas, ...others } = shokoData;
return (
<Container service={service}>
<Block label="shoko.series" value={t("common.number", { value: series })} />
<Block label="shoko.movies" value={t("common.number", { value: movies })} />
<Block label="shoko.ovas" value={t("common.number", { value: ovas })} />
<Block
label="shoko.others"
value={t("common.number", { value: Object.values(others).reduce((total, item) => total + item) })}
/>
</Container>
);
}

View File

@ -0,0 +1,78 @@
import cache from "memory-cache";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import getServiceWidget from "utils/config/service-helpers";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";
const proxyName = "shokoProxyHandler";
const sessionTokenCacheKey = `${proxyName}__sessionToken`;
const logger = createLogger(proxyName);
async function login(widget, service) {
const endpoint = "auth";
const api = widgets?.[widget.type]?.api;
const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget }));
const loginParams = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user: widget.username,
pass: widget.password,
device: "web-ui",
}),
};
// eslint-disable-next-line no-unused-vars
const [status, contentType, data] = await httpProxy(loginUrl, loginParams);
try {
const { apikey } = JSON.parse(data.toString());
cache.put(`${sessionTokenCacheKey}.${service}`, apikey);
return { apikey };
} catch (e) {
logger.error("Unable to login to Shoko API: %s", e);
}
return { apikey: false };
}
export default async function shokoProxyHandler(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 endpoint = "v3/Dashboard/SeriesSummary";
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
const params = { method: "GET", headers: { apikey: cache.get(`${sessionTokenCacheKey}.${service}`) } };
let [status, contentType, data] = await httpProxy(url, params);
if (status === 401) {
logger.debug("Shoko API rejected the request, attempting to obtain new session token");
const { apikey } = await login(widget, service);
params.headers.apikey = apikey;
[status, contentType, data] = await httpProxy(url, params);
}
if (contentType) res.setHeader("Content-Type", contentType);
return res.status(status).send(data);
}

View File

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

View File

@ -63,6 +63,7 @@ import readarr from "./readarr/widget";
import rutorrent from "./rutorrent/widget"; import rutorrent from "./rutorrent/widget";
import sabnzbd from "./sabnzbd/widget"; import sabnzbd from "./sabnzbd/widget";
import scrutiny from "./scrutiny/widget"; import scrutiny from "./scrutiny/widget";
import shoko from "./shoko/widget";
import sonarr from "./sonarr/widget"; import sonarr from "./sonarr/widget";
import speedtest from "./speedtest/widget"; import speedtest from "./speedtest/widget";
import strelaysrv from "./strelaysrv/widget"; import strelaysrv from "./strelaysrv/widget";
@ -147,6 +148,7 @@ const widgets = {
rutorrent, rutorrent,
sabnzbd, sabnzbd,
scrutiny, scrutiny,
shoko,
sonarr, sonarr,
speedtest, speedtest,
strelaysrv, strelaysrv,