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';
|
2022-08-25 18:47:06 +02:00
|
|
|
import { getConfig } from '../../../../tools/getConfig';
|
2022-08-26 10:46:34 +02:00
|
|
|
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
2022-08-25 18:47:06 +02:00
|
|
|
import { Config } from '../../../../tools/types';
|
2022-08-25 18:10:23 +02:00
|
|
|
|
|
|
|
|
dayjs.extend(duration);
|
|
|
|
|
|
2022-08-26 16:12:40 +02:00
|
|
|
export interface UsenetInfoRequestParams {
|
2022-08-26 10:46:34 +02:00
|
|
|
serviceId: string;
|
|
|
|
|
}
|
|
|
|
|
|
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 });
|
|
|
|
|
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
2022-08-26 16:12:40 +02:00
|
|
|
const { serviceId } = req.query as any as UsenetInfoRequestParams;
|
2022-08-26 10:46:34 +02:00
|
|
|
|
|
|
|
|
const service = getServiceById(config, serviceId);
|
|
|
|
|
|
|
|
|
|
if (!service) {
|
|
|
|
|
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!service.apiKey) {
|
|
|
|
|
throw new Error(`API Key for service "${service.name}" is missing`);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-31 19:13:34 +02:00
|
|
|
const { origin } = new URL(service.url);
|
|
|
|
|
|
|
|
|
|
const queue = await new Client(origin, service.apiKey).queue(0, -1);
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-08-26 16:12:40 +02: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);
|
2022-08-26 10:46:34 +02:00
|
|
|
|
2022-08-26 16:12:40 +02:00
|
|
|
const response: UsenetInfoResponse = {
|
|
|
|
|
paused: queue.paused,
|
|
|
|
|
sizeLeft: parseFloat(queue.mbleft) * 1024 * 1024,
|
|
|
|
|
speed: parseFloat(queue.kbpersec) * 1000,
|
|
|
|
|
eta: eta.asSeconds(),
|
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',
|
|
|
|
|
});
|
|
|
|
|
};
|