mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
added widget for xteve (#731)
* added xteve widget * eslint fixes * xteve code cleanup Co-authored-by: Marcus Kimpenhaus <k@AirM2.kimpenhaus.net> Co-authored-by: Michael Shamoon <4887959+shamoon@users.noreply.github.com>
This commit is contained in:
parent
3a27486c34
commit
43a11eadfc
@ -401,5 +401,10 @@
|
|||||||
"memoryUsed": "Memory Used",
|
"memoryUsed": "Memory Used",
|
||||||
"uptime": "Uptime",
|
"uptime": "Uptime",
|
||||||
"numberOfLeases": "Leases"
|
"numberOfLeases": "Leases"
|
||||||
|
},
|
||||||
|
"xteve": {
|
||||||
|
"streams_all": "All Streams",
|
||||||
|
"streams_active": "Active Streams",
|
||||||
|
"streams_xepg": "XEPG Channels"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,7 @@ const components = {
|
|||||||
truenas: dynamic(() => import("./truenas/component")),
|
truenas: dynamic(() => import("./truenas/component")),
|
||||||
unifi: dynamic(() => import("./unifi/component")),
|
unifi: dynamic(() => import("./unifi/component")),
|
||||||
watchtower: dynamic(() => import("./watchtower/component")),
|
watchtower: dynamic(() => import("./watchtower/component")),
|
||||||
|
xteve: dynamic(() => import("./xteve/component")),
|
||||||
};
|
};
|
||||||
|
|
||||||
export default components;
|
export default components;
|
||||||
|
@ -49,6 +49,7 @@ import tubearchivist from "./tubearchivist/widget";
|
|||||||
import truenas from "./truenas/widget";
|
import truenas from "./truenas/widget";
|
||||||
import unifi from "./unifi/widget";
|
import unifi from "./unifi/widget";
|
||||||
import watchtower from './watchtower/widget'
|
import watchtower from './watchtower/widget'
|
||||||
|
import xteve from './xteve/widget'
|
||||||
|
|
||||||
const widgets = {
|
const widgets = {
|
||||||
adguard,
|
adguard,
|
||||||
@ -105,6 +106,7 @@ const widgets = {
|
|||||||
unifi,
|
unifi,
|
||||||
unifi_console: unifi,
|
unifi_console: unifi,
|
||||||
watchtower,
|
watchtower,
|
||||||
|
xteve,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default widgets;
|
export default widgets;
|
||||||
|
35
src/widgets/xteve/component.jsx
Normal file
35
src/widgets/xteve/component.jsx
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
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: xteveData, error: xteveError } = useWidgetAPI(widget, "api");
|
||||||
|
|
||||||
|
if (xteveError) {
|
||||||
|
return <Container error={xteveError} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!xteveData) {
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="xteve.streams_all" />
|
||||||
|
<Block label="xteve.streams_active " />
|
||||||
|
<Block label="xteve.streams_xepg" />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Container service={service}>
|
||||||
|
<Block label="xteve.streams_all" value={t("common.number", { value: xteveData["streams.all"] ?? 0 })} />
|
||||||
|
<Block label="xteve.streams_active" value={t("common.number", { value: xteveData["streams.active"] ?? 0 })} />
|
||||||
|
<Block label="xteve.streams_xepg" value={t("common.number", { value: xteveData["streams.xepg"] ?? 0 })} />
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
62
src/widgets/xteve/proxy.js
Normal file
62
src/widgets/xteve/proxy.js
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
|
import { httpProxy } from "utils/proxy/http";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
import widgets from "widgets/widgets";
|
||||||
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
|
|
||||||
|
const logger = createLogger("xteveProxyHandler");
|
||||||
|
|
||||||
|
export default async function xteveProxyHandler(req, res) {
|
||||||
|
const { group, service, endpoint } = req.query;
|
||||||
|
|
||||||
|
if (!group || !service) {
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const widget = await getServiceWidget(group, service);
|
||||||
|
const api = widgets?.[widget.type]?.api;
|
||||||
|
if (!api) {
|
||||||
|
return res.status(403).json({ error: "Service does not support API calls" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = formatApiCall(api, { endpoint, ...widget });
|
||||||
|
const method = "POST";
|
||||||
|
const payload = { cmd: "status" };
|
||||||
|
|
||||||
|
if (widget.username && widget.password) {
|
||||||
|
const [status, contentType, data] = await httpProxy(url, {
|
||||||
|
method,
|
||||||
|
body: JSON.stringify({
|
||||||
|
cmd: "login",
|
||||||
|
username: widget.username,
|
||||||
|
password: widget.password,
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
return [status, contentType, data];
|
||||||
|
}
|
||||||
|
|
||||||
|
const json = JSON.parse(data.toString());
|
||||||
|
|
||||||
|
if (json?.status !== true) {
|
||||||
|
const message = "Authentication failed.";
|
||||||
|
return res.status(401).end(JSON.stringify({error: { message } }));
|
||||||
|
}
|
||||||
|
|
||||||
|
payload.token = json.token;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [status, contentType, data] = await httpProxy(url, {
|
||||||
|
method,
|
||||||
|
body: JSON.stringify(payload)
|
||||||
|
});
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
logger.debug("Error %d calling endpoint %s", status, url);
|
||||||
|
return res.status(status, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
|
return res.status(status).send(data);
|
||||||
|
}
|
14
src/widgets/xteve/widget.js
Normal file
14
src/widgets/xteve/widget.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import xteveProxyHandler from "./proxy";
|
||||||
|
|
||||||
|
const widget = {
|
||||||
|
api: "{url}/{endpoint}",
|
||||||
|
proxyHandler: xteveProxyHandler,
|
||||||
|
|
||||||
|
mappings: {
|
||||||
|
"api": {
|
||||||
|
endpoint: "api/",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default widget;
|
Loading…
x
Reference in New Issue
Block a user