mirror of
https://github.com/karl0ss/homepage.git
synced 2025-05-02 05:23:39 +01:00
Merge pull request #141 from JazzFisch/add-transmission-widget
Add transmission widget
This commit is contained in:
commit
52816426fc
@ -64,6 +64,12 @@
|
|||||||
"upload": "Upload",
|
"upload": "Upload",
|
||||||
"download": "Download"
|
"download": "Download"
|
||||||
},
|
},
|
||||||
|
"transmission": {
|
||||||
|
"download": "Download",
|
||||||
|
"upload": "Upload",
|
||||||
|
"leech": "Leech",
|
||||||
|
"seed": "Seed"
|
||||||
|
},
|
||||||
"sonarr": {
|
"sonarr": {
|
||||||
"wanted": "Wanted",
|
"wanted": "Wanted",
|
||||||
"queued": "Queued",
|
"queued": "Queued",
|
||||||
|
@ -8,6 +8,7 @@ import Portainer from "./widgets/service/portainer";
|
|||||||
import Emby from "./widgets/service/emby";
|
import Emby from "./widgets/service/emby";
|
||||||
import Nzbget from "./widgets/service/nzbget";
|
import Nzbget from "./widgets/service/nzbget";
|
||||||
import SABnzbd from "./widgets/service/sabnzbd";
|
import SABnzbd from "./widgets/service/sabnzbd";
|
||||||
|
import Transmission from "./widgets/service/transmission";
|
||||||
import Docker from "./widgets/service/docker";
|
import Docker from "./widgets/service/docker";
|
||||||
import Pihole from "./widgets/service/pihole";
|
import Pihole from "./widgets/service/pihole";
|
||||||
import Rutorrent from "./widgets/service/rutorrent";
|
import Rutorrent from "./widgets/service/rutorrent";
|
||||||
@ -32,6 +33,8 @@ const widgetMappings = {
|
|||||||
emby: Emby,
|
emby: Emby,
|
||||||
jellyfin: Jellyfin,
|
jellyfin: Jellyfin,
|
||||||
nzbget: Nzbget,
|
nzbget: Nzbget,
|
||||||
|
sabnzbd: SABnzbd,
|
||||||
|
transmission: Transmission,
|
||||||
pihole: Pihole,
|
pihole: Pihole,
|
||||||
rutorrent: Rutorrent,
|
rutorrent: Rutorrent,
|
||||||
speedtest: Speedtest,
|
speedtest: Speedtest,
|
||||||
@ -42,7 +45,6 @@ const widgetMappings = {
|
|||||||
npm: Npm,
|
npm: Npm,
|
||||||
tautulli: Tautulli,
|
tautulli: Tautulli,
|
||||||
gotify: Gotify,
|
gotify: Gotify,
|
||||||
sabnzbd: SABnzbd,
|
|
||||||
prowlarr: Prowlarr
|
prowlarr: Prowlarr
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ export default function SABnzbd({ service }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Widget>
|
<Widget>
|
||||||
<Block label={t("sabnzbd.rate")} value={`${queueData.queue.speed}bps`} />
|
<Block label={t("sabnzbd.rate")} value={`${queueData.queue.speed}B/s`} />
|
||||||
<Block label={t("sabnzbd.queue")} value={queueData.queue.noofslots} />
|
<Block label={t("sabnzbd.queue")} value={queueData.queue.noofslots} />
|
||||||
<Block label={t("sabnzbd.timeleft")} value={queueData.queue.timeleft} />
|
<Block label={t("sabnzbd.timeleft")} value={queueData.queue.timeleft} />
|
||||||
</Widget>
|
</Widget>
|
||||||
|
70
src/components/services/widgets/service/transmission.jsx
Normal file
70
src/components/services/widgets/service/transmission.jsx
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import useSWR from "swr";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
|
import Widget from "../widget";
|
||||||
|
import Block from "../block";
|
||||||
|
|
||||||
|
import { formatApiUrl } from "utils/api-helpers";
|
||||||
|
|
||||||
|
export default function Transmission({ service }) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const config = service.widget;
|
||||||
|
|
||||||
|
const { data: torrentData, error: torrentError } = useSWR(formatApiUrl(config));
|
||||||
|
|
||||||
|
if (torrentError) {
|
||||||
|
return <Widget error={t("widget.api_error")} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!torrentData) {
|
||||||
|
return (
|
||||||
|
<Widget>
|
||||||
|
<Block label={t("transmission.leech")} />
|
||||||
|
<Block label={t("transmission.download")} />
|
||||||
|
<Block label={t("transmission.seed")} />
|
||||||
|
<Block label={t("transmission.upload")} />
|
||||||
|
</Widget>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { torrents } = torrentData.arguments;
|
||||||
|
let rateDl = 0;
|
||||||
|
let rateUl = 0;
|
||||||
|
let completed = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < torrents.length; i += 1) {
|
||||||
|
const torrent = torrents[i];
|
||||||
|
rateDl += torrent.rateDownload;
|
||||||
|
rateUl += torrent.rateUpload;
|
||||||
|
if (torrent.percentDone === 1) {
|
||||||
|
completed += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const leech = torrents.length - completed;
|
||||||
|
|
||||||
|
let unitsDl = "KB/s";
|
||||||
|
let unitsUl = "KB/s";
|
||||||
|
rateDl /= 1024;
|
||||||
|
rateUl /= 1024;
|
||||||
|
|
||||||
|
if (rateDl > 1024) {
|
||||||
|
rateDl /= 1024;
|
||||||
|
unitsDl = "MB/s";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rateUl > 1024) {
|
||||||
|
rateUl /= 1024;
|
||||||
|
unitsUl = "MB/s";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Widget>
|
||||||
|
<Block label={t("transmission.leech")} value={leech} />
|
||||||
|
<Block label={t("transmission.download")} value={`${rateDl.toFixed(2)} ${unitsDl}`} />
|
||||||
|
<Block label={t("transmission.seed")} value={completed} />
|
||||||
|
<Block label={t("transmission.upload")} value={`${rateUl.toFixed(2)} ${unitsUl}`} />
|
||||||
|
</Widget>
|
||||||
|
);
|
||||||
|
}
|
@ -3,6 +3,7 @@ import credentialedProxyHandler from "utils/proxies/credentialed";
|
|||||||
import rutorrentProxyHandler from "utils/proxies/rutorrent";
|
import rutorrentProxyHandler from "utils/proxies/rutorrent";
|
||||||
import nzbgetProxyHandler from "utils/proxies/nzbget";
|
import nzbgetProxyHandler from "utils/proxies/nzbget";
|
||||||
import npmProxyHandler from "utils/proxies/npm";
|
import npmProxyHandler from "utils/proxies/npm";
|
||||||
|
import transmissionProxyHandler from "utils/proxies/transmission";
|
||||||
|
|
||||||
const serviceProxyHandlers = {
|
const serviceProxyHandlers = {
|
||||||
// uses query param auth
|
// uses query param auth
|
||||||
@ -28,6 +29,7 @@ const serviceProxyHandlers = {
|
|||||||
rutorrent: rutorrentProxyHandler,
|
rutorrent: rutorrentProxyHandler,
|
||||||
nzbget: nzbgetProxyHandler,
|
nzbget: nzbgetProxyHandler,
|
||||||
npm: npmProxyHandler,
|
npm: npmProxyHandler,
|
||||||
|
transmission: transmissionProxyHandler,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default async function handler(req, res) {
|
export default async function handler(req, res) {
|
||||||
|
@ -9,6 +9,7 @@ const formats = {
|
|||||||
traefik: `{url}/api/{endpoint}`,
|
traefik: `{url}/api/{endpoint}`,
|
||||||
portainer: `{url}/api/endpoints/{env}/{endpoint}`,
|
portainer: `{url}/api/endpoints/{env}/{endpoint}`,
|
||||||
rutorrent: `{url}/plugins/httprpc/action.php`,
|
rutorrent: `{url}/plugins/httprpc/action.php`,
|
||||||
|
transmission: `{url}/transmission/rpc`,
|
||||||
jellyseerr: `{url}/api/v1/{endpoint}`,
|
jellyseerr: `{url}/api/v1/{endpoint}`,
|
||||||
overseerr: `{url}/api/v1/{endpoint}`,
|
overseerr: `{url}/api/v1/{endpoint}`,
|
||||||
ombi: `{url}/api/v1/{endpoint}`,
|
ombi: `{url}/api/v1/{endpoint}`,
|
||||||
|
@ -12,7 +12,7 @@ export function httpsRequest(url, params) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
response.on("end", () => {
|
response.on("end", () => {
|
||||||
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data)]);
|
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -20,6 +20,10 @@ export function httpsRequest(url, params) {
|
|||||||
reject([500, error]);
|
reject([500, error]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (params.body) {
|
||||||
|
request.write(params.body);
|
||||||
|
}
|
||||||
|
|
||||||
request.end();
|
request.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -34,7 +38,7 @@ export function httpRequest(url, params) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
response.on("end", () => {
|
response.on("end", () => {
|
||||||
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data)]);
|
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -42,6 +46,10 @@ export function httpRequest(url, params) {
|
|||||||
reject([500, error]);
|
reject([500, error]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (params.body) {
|
||||||
|
request.write(params.body);
|
||||||
|
}
|
||||||
|
|
||||||
request.end();
|
request.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
56
src/utils/proxies/transmission.js
Normal file
56
src/utils/proxies/transmission.js
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
import { httpProxy } from "utils/http";
|
||||||
|
import { formatApiCall } from "utils/api-helpers";
|
||||||
|
import getServiceWidget from "utils/service-helpers";
|
||||||
|
|
||||||
|
export default async function transmissionProxyHandler(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);
|
||||||
|
|
||||||
|
if (!widget) {
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = new URL(formatApiCall(widget.type, { endpoint, ...widget }));
|
||||||
|
const csrfHeaderName = "x-transmission-session-id";
|
||||||
|
|
||||||
|
const method = "POST";
|
||||||
|
const auth = `${widget.username}:${widget.password}`;
|
||||||
|
const body = JSON.stringify({
|
||||||
|
method: "torrent-get",
|
||||||
|
arguments: {
|
||||||
|
fields: ["percentDone", "status", "rateDownload", "rateUpload"]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
"content-type": "application/json",
|
||||||
|
};
|
||||||
|
|
||||||
|
let [status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||||
|
method,
|
||||||
|
auth,
|
||||||
|
body,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (status === 409) {
|
||||||
|
// Transmission is rejecting the request, but returning a CSRF token
|
||||||
|
headers[csrfHeaderName] = responseHeaders[csrfHeaderName];
|
||||||
|
|
||||||
|
// retry the request, now with the CSRF token
|
||||||
|
[status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||||
|
method,
|
||||||
|
auth,
|
||||||
|
body,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contentType) res.setHeader("Content-Type", contentType);
|
||||||
|
return res.status(status).send(data);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user