Add Shoko widget
This commit is contained in:
parent
866bb1c330
commit
42e8d1bd21
@ -614,5 +614,11 @@
|
||||
"whatsupdocker": {
|
||||
"monitoring": "Monitoring",
|
||||
"updates": "Updates"
|
||||
},
|
||||
"shoko": {
|
||||
"series": "TV Series",
|
||||
"movies": "Movies",
|
||||
"ovas": "OVAs",
|
||||
"others": "Others"
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,6 +69,7 @@ const components = {
|
||||
rutorrent: dynamic(() => import("./rutorrent/component")),
|
||||
sabnzbd: dynamic(() => import("./sabnzbd/component")),
|
||||
scrutiny: dynamic(() => import("./scrutiny/component")),
|
||||
shoko: dynamic(() => import("./shoko/component")),
|
||||
sonarr: dynamic(() => import("./sonarr/component")),
|
||||
speedtest: dynamic(() => import("./speedtest/component")),
|
||||
strelaysrv: dynamic(() => import("./strelaysrv/component")),
|
||||
|
||||
42
src/widgets/shoko/component.jsx
Normal file
42
src/widgets/shoko/component.jsx
Normal 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>
|
||||
);
|
||||
}
|
||||
78
src/widgets/shoko/proxy.js
Normal file
78
src/widgets/shoko/proxy.js
Normal 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);
|
||||
}
|
||||
8
src/widgets/shoko/widget.js
Normal file
8
src/widgets/shoko/widget.js
Normal file
@ -0,0 +1,8 @@
|
||||
import shokoProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: shokoProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
||||
@ -63,6 +63,7 @@ import readarr from "./readarr/widget";
|
||||
import rutorrent from "./rutorrent/widget";
|
||||
import sabnzbd from "./sabnzbd/widget";
|
||||
import scrutiny from "./scrutiny/widget";
|
||||
import shoko from "./shoko/widget";
|
||||
import sonarr from "./sonarr/widget";
|
||||
import speedtest from "./speedtest/widget";
|
||||
import strelaysrv from "./strelaysrv/widget";
|
||||
@ -147,6 +148,7 @@ const widgets = {
|
||||
rutorrent,
|
||||
sabnzbd,
|
||||
scrutiny,
|
||||
shoko,
|
||||
sonarr,
|
||||
speedtest,
|
||||
strelaysrv,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user