2022-08-25 18:10:23 +02:00
|
|
|
import { getCookie } from 'cookies-next';
|
|
|
|
|
import dayjs from 'dayjs';
|
|
|
|
|
import duration from 'dayjs/plugin/duration';
|
|
|
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
|
import { Client } from 'sabnzbd-api';
|
2023-07-21 18:08:40 +09:00
|
|
|
import { findAppProperty } from '~/tools/client/app-properties';
|
|
|
|
|
|
2023-06-10 17:48:55 +02:00
|
|
|
import { NzbgetClient } from '../../../../server/api/routers/usenet/nzbget/nzbget-client';
|
|
|
|
|
import { NzbgetStatus } from '../../../../server/api/routers/usenet/nzbget/types';
|
2023-07-21 18:08:40 +09:00
|
|
|
import { getConfig } from '../../../../tools/config/getConfig';
|
2022-08-25 18:10:23 +02:00
|
|
|
|
|
|
|
|
dayjs.extend(duration);
|
|
|
|
|
|
2022-08-26 16:12:40 +02:00
|
|
|
export interface UsenetInfoRequestParams {
|
2022-12-18 22:27:01 +01:00
|
|
|
appId: string;
|
2022-08-26 10:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-26 16:12:40 +02:00
|
|
|
export interface UsenetInfoResponse {
|
|
|
|
|
paused: boolean;
|
|
|
|
|
sizeLeft: number;
|
|
|
|
|
speed: number;
|
|
|
|
|
eta: number;
|
2022-08-26 10:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-25 18:10:23 +02:00
|
|
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
try {
|
|
|
|
|
const configName = getCookie('config-name', { req });
|
2022-12-11 16:06:41 +01:00
|
|
|
const config = getConfig(configName?.toString() ?? 'default');
|
2022-12-18 22:27:01 +01:00
|
|
|
const { appId } = req.query as any as UsenetInfoRequestParams;
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-12-18 22:27:01 +01:00
|
|
|
const app = config.apps.find((x) => x.id === appId);
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-12-18 22:27:01 +01:00
|
|
|
if (!app) {
|
|
|
|
|
throw new Error(`App with ID "${req.query.appId}" could not be found.`);
|
2022-08-26 10:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-06 10:05:35 -06:00
|
|
|
let response: UsenetInfoResponse;
|
2022-12-18 22:27:01 +01:00
|
|
|
switch (app.integration?.type) {
|
2022-12-11 16:06:41 +01:00
|
|
|
case 'nzbGet': {
|
2022-12-18 22:27:01 +01:00
|
|
|
const url = new URL(app.url);
|
2022-11-06 10:05:35 -06:00
|
|
|
const options = {
|
|
|
|
|
host: url.hostname,
|
2023-01-24 18:04:09 -06:00
|
|
|
port: url.port || (url.protocol === 'https:' ? '443' : '80'),
|
2023-06-10 19:04:54 +02:00
|
|
|
login: findAppProperty(app, 'username'),
|
|
|
|
|
hash: findAppProperty(app, 'password'),
|
2022-11-06 10:05:35 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const nzbGet = NzbgetClient(options);
|
|
|
|
|
|
2022-11-12 08:12:54 +09:00
|
|
|
const nzbgetStatus: NzbgetStatus = await new Promise((resolve, reject) => {
|
2022-11-06 10:05:35 -06:00
|
|
|
nzbGet.status((err: any, result: NzbgetStatus) => {
|
|
|
|
|
if (!err) {
|
|
|
|
|
resolve(result);
|
|
|
|
|
} else {
|
|
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-11-06 10:05:35 -06:00
|
|
|
if (!nzbgetStatus) {
|
|
|
|
|
throw new Error('Error while getting NZBGet status');
|
|
|
|
|
}
|
2022-08-31 19:13:34 +02:00
|
|
|
|
2022-11-06 10:05:35 -06:00
|
|
|
const bytesRemaining = nzbgetStatus.RemainingSizeMB * 1000000;
|
|
|
|
|
const eta = bytesRemaining / nzbgetStatus.DownloadRate;
|
|
|
|
|
response = {
|
|
|
|
|
paused: nzbgetStatus.DownloadPaused,
|
|
|
|
|
sizeLeft: bytesRemaining,
|
|
|
|
|
speed: nzbgetStatus.DownloadRate,
|
|
|
|
|
eta,
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-12-11 16:06:41 +01:00
|
|
|
case 'sabnzbd': {
|
2023-06-10 19:04:54 +02:00
|
|
|
const apiKey = findAppProperty(app, 'apiKey');
|
2022-12-11 19:33:54 +01:00
|
|
|
if (!apiKey) {
|
2022-12-18 22:27:01 +01:00
|
|
|
throw new Error(`API Key for app "${app.name}" is missing`);
|
2022-11-06 10:05:35 -06:00
|
|
|
}
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-12-18 22:27:01 +01:00
|
|
|
const { origin } = new URL(app.url);
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-12-11 19:33:54 +01:00
|
|
|
const queue = await new Client(origin, apiKey).queue(0, -1);
|
2022-11-06 10:05:35 -06:00
|
|
|
|
|
|
|
|
const [hours, minutes, seconds] = queue.timeleft.split(':');
|
|
|
|
|
const eta = dayjs.duration({
|
|
|
|
|
hour: parseInt(hours, 10),
|
|
|
|
|
minutes: parseInt(minutes, 10),
|
|
|
|
|
seconds: parseInt(seconds, 10),
|
|
|
|
|
} as any);
|
|
|
|
|
|
|
|
|
|
response = {
|
|
|
|
|
paused: queue.paused,
|
|
|
|
|
sizeLeft: parseFloat(queue.mbleft) * 1024 * 1024,
|
|
|
|
|
speed: parseFloat(queue.kbpersec) * 1000,
|
|
|
|
|
eta: eta.asSeconds(),
|
|
|
|
|
};
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
2022-12-18 22:27:01 +01:00
|
|
|
throw new Error(`App type "${app.integration?.type}" unrecognized.`);
|
2022-11-06 10:05:35 -06:00
|
|
|
}
|
2022-08-26 10:46:34 +02:00
|
|
|
|
|
|
|
|
return res.status(200).json(response);
|
2022-08-25 18:10:23 +02:00
|
|
|
} catch (err) {
|
2022-08-26 11:10:40 +02:00
|
|
|
return res.status(500).send((err as any).message);
|
2022-08-25 18:10:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
|
|
|
|
// Filter out if the reuqest is a POST or a GET
|
|
|
|
|
if (req.method === 'GET') {
|
|
|
|
|
return Get(req, res);
|
|
|
|
|
}
|
|
|
|
|
return res.status(405).json({
|
|
|
|
|
statusCode: 405,
|
|
|
|
|
message: 'Method not allowed',
|
|
|
|
|
});
|
|
|
|
|
};
|