mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-13 17:05:47 +01:00
Cleanup
This commit is contained in:
52
src/pages/api/modules/usenet/history.ts
Normal file
52
src/pages/api/modules/usenet/history.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
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';
|
||||
import { UsenetHistoryItem } from '../../../../modules';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
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');
|
||||
|
||||
const history: UsenetHistoryItem[] = [];
|
||||
|
||||
await Promise.all(
|
||||
nzbServices.map(async (service) => {
|
||||
if (!service.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
const queue = await new Client(service.url, service.apiKey).history();
|
||||
|
||||
queue.slots.forEach((slot) => {
|
||||
history.push({
|
||||
id: slot.nzo_id,
|
||||
name: slot.name,
|
||||
size: slot.bytes * 1000,
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
return res.status(200).json(history);
|
||||
} 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',
|
||||
});
|
||||
};
|
||||
@@ -3,8 +3,9 @@ import dayjs from 'dayjs';
|
||||
import duration from 'dayjs/plugin/duration';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { Client } from 'sabnzbd-api';
|
||||
import { getConfig } from '../../../tools/getConfig';
|
||||
import { Config, DownloadItem } from '../../../tools/types';
|
||||
import { UsenetQueueItem } from '../../../../modules';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { Config } from '../../../../tools/types';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
@@ -14,7 +15,7 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const nzbServices = config.services.filter((service) => service.type === 'Sabnzbd');
|
||||
|
||||
const downloads: DownloadItem[] = [];
|
||||
const downloads: UsenetQueueItem[] = [];
|
||||
|
||||
await Promise.all(
|
||||
nzbServices.map(async (service) => {
|
||||
Reference in New Issue
Block a user