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 { UsenetHistoryItem } from '../../../../modules';
|
|
|
|
|
import { getConfig } from '../../../../tools/getConfig';
|
|
|
|
|
import { Config } from '../../../../tools/types';
|
2022-08-25 18:10:23 +02:00
|
|
|
|
|
|
|
|
dayjs.extend(duration);
|
|
|
|
|
|
|
|
|
|
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
|
try {
|
|
|
|
|
const configName = getCookie('config-name', { req });
|
|
|
|
|
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
|
|
|
|
const nzbServices = config.services.filter((service) => service.type === 'Sabnzbd');
|
|
|
|
|
|
2022-08-25 18:47:06 +02:00
|
|
|
const history: UsenetHistoryItem[] = [];
|
2022-08-25 18:10:23 +02:00
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
|
nzbServices.map(async (service) => {
|
|
|
|
|
if (!service.apiKey) {
|
|
|
|
|
throw new Error(`API Key for service "${service.name}" is missing`);
|
|
|
|
|
}
|
2022-08-25 18:47:06 +02:00
|
|
|
const queue = await new Client(service.url, service.apiKey).history();
|
2022-08-25 18:10:23 +02:00
|
|
|
|
|
|
|
|
queue.slots.forEach((slot) => {
|
2022-08-25 18:47:06 +02:00
|
|
|
history.push({
|
2022-08-25 18:10:23 +02:00
|
|
|
id: slot.nzo_id,
|
2022-08-25 18:47:06 +02:00
|
|
|
name: slot.name,
|
|
|
|
|
size: slot.bytes * 1000,
|
2022-08-25 18:10:23 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2022-08-25 18:47:06 +02:00
|
|
|
return res.status(200).json(history);
|
2022-08-25 18:10:23 +02:00
|
|
|
} catch (err) {
|
|
|
|
|
return res.status(401).json(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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',
|
|
|
|
|
});
|
|
|
|
|
};
|