From 1f66d64f2430fade4c835d901f74251b7eb0ecbf Mon Sep 17 00:00:00 2001 From: ajnart Date: Thu, 26 May 2022 21:08:16 +0200 Subject: [PATCH] :sparkles: Add deluge integration Fixes #122 --- .../modules/downloads/DownloadsModule.tsx | 32 +++++++++++------ src/pages/api/modules/downloads.ts | 34 +++++++++++++++---- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/components/modules/downloads/DownloadsModule.tsx b/src/components/modules/downloads/DownloadsModule.tsx index 10e300ee1..4b3b6b24d 100644 --- a/src/components/modules/downloads/DownloadsModule.tsx +++ b/src/components/modules/downloads/DownloadsModule.tsx @@ -25,37 +25,45 @@ export default function DownloadComponent() { const qBittorrentService = config.services .filter((service) => service.type === 'qBittorrent') .at(0); + const delugeService = config.services.filter((service) => service.type === 'Deluge').at(0); const hideComplete: boolean = (config?.modules?.[DownloadsModule.title]?.options?.hidecomplete?.value as boolean) ?? false; - const [torrents, setTorrents] = useState([]); + const [delugeTorrents, setDelugeTorrents] = useState([]); + const [qBittorrentTorrents, setqBittorrentTorrents] = useState([]); useEffect(() => { if (qBittorrentService) { - axios.post('/api/modules/downloads', { ...qBittorrentService }).then((res) => { - setTorrents(res.data.torrents); - }); setInterval(() => { - axios.post('/api/modules/downloads', { ...qBittorrentService }).then((res) => { - setTorrents(res.data.torrents); + axios + .post('/api/modules/downloads?dlclient=qbit', { ...qBittorrentService }) + .then((res) => { + setqBittorrentTorrents(res.data.torrents); + }); + }, 3000); + } + if (delugeService) { + setInterval(() => { + axios.post('/api/modules/downloads?dlclient=deluge', { ...delugeService }).then((res) => { + setDelugeTorrents(res.data.torrents); }); }, 3000); } }, [config.modules]); - if (!qBittorrentService) { + if (!qBittorrentService && !delugeService) { return ( - Critical: No qBittorrent instance found in services. + Critical: No qBittorrent/Deluge instance found in services. - Add a qBittorrent service to view current downloads + Add a qBittorrent/Deluge service to view current downloads ); } - if (torrents.length === 0) { + if (qBittorrentTorrents.length === 0 && delugeTorrents.length === 0) { return (
@@ -71,6 +79,10 @@ export default function DownloadComponent() { Progress ); + // Loop over qBittorrent torrents merging with deluge torrents + const torrents: NormalizedTorrent[] = []; + delugeTorrents.forEach((torrent) => torrents.push(torrent)); + qBittorrentTorrents.forEach((torrent) => torrents.push(torrent)); const rows = torrents.map((torrent) => { if (torrent.progress === 1 && hideComplete) { diff --git a/src/pages/api/modules/downloads.ts b/src/pages/api/modules/downloads.ts index 46edc5a80..2322a481b 100644 --- a/src/pages/api/modules/downloads.ts +++ b/src/pages/api/modules/downloads.ts @@ -1,16 +1,38 @@ +import { Deluge } from '@ctrl/deluge'; import { QBittorrent } from '@ctrl/qbittorrent'; import { NextApiRequest, NextApiResponse } from 'next'; async function Post(req: NextApiRequest, res: NextApiResponse) { - // Get the body + // Get the type of service from the request url + const { dlclient } = req.query; const { body } = req; // Get login, password and url from the body const { username, password, url } = body; - const client = new QBittorrent({ - baseUrl: new URL(url).origin, - username, - password, - }); + if (!dlclient || (!username && !password) || !url) { + return res.status(400).json({ + error: 'Wrong request', + }); + } + let client: Deluge | QBittorrent; + switch (dlclient) { + case 'qbit': + client = new QBittorrent({ + baseUrl: new URL(url).origin, + username, + password, + }); + break; + case 'deluge': + client = new Deluge({ + baseUrl: new URL(url).origin, + password, + }); + break; + default: + return res.status(400).json({ + error: 'Wrong request', + }); + } const data = await client.getAllData(); res.status(200).json({ torrents: data.torrents,