🏗️ Migrate usenet history to tRPC

This commit is contained in:
Meier Lukas
2023-06-10 17:25:36 +02:00
parent 8c676c9e16
commit 9cefe5d3a3
2 changed files with 94 additions and 19 deletions

View File

@@ -2,10 +2,11 @@ import dayjs from 'dayjs';
import { Client } from 'sabnzbd-api';
import { z } from 'zod';
import { TRPCError } from '@trpc/server';
import { NzbgetStatus } from '~/server/api/routers/usenet/nzbget/types';
import { NzbgetHistoryItem, NzbgetStatus } from '~/server/api/routers/usenet/nzbget/types';
import { getConfig } from '~/tools/config/getConfig';
import { createTRPCRouter, publicProcedure } from '../../trpc';
import { NzbgetClient } from './nzbget/nzbget-client';
import { UsenetHistoryItem } from '~/widgets/useNet/types';
export const usenetRouter = createTRPCRouter({
info: publicProcedure
@@ -91,6 +92,84 @@ export const usenetRouter = createTRPCRouter({
eta: eta.asSeconds(),
};
}),
history: publicProcedure
.input(
z.object({
configName: z.string(),
appId: z.string(),
limit: z.number(),
offset: z.number(),
})
)
.query(async ({ input }) => {
const config = getConfig(input.configName);
const app = config.apps.find((x) => x.id === input.appId);
if (!app || (app.integration?.type !== 'nzbGet' && app.integration?.type !== 'sabnzbd')) {
throw new Error(`App with ID "${input.appId}" could not be found.`);
}
if (app.integration?.type === 'nzbGet') {
const url = new URL(app.url);
const options = {
host: url.hostname,
port: url.port || (url.protocol === 'https:' ? '443' : '80'),
login: app.integration.properties.find((x) => x.field === 'username')?.value ?? undefined,
hash: app.integration.properties.find((x) => x.field === 'password')?.value ?? undefined,
};
const nzbGet = NzbgetClient(options);
const nzbgetHistory: NzbgetHistoryItem[] = await new Promise((resolve, reject) => {
nzbGet.history(false, (err: any, result: NzbgetHistoryItem[]) => {
if (!err) {
resolve(result);
} else {
reject(err);
}
});
});
if (!nzbgetHistory) {
throw new Error('Error while getting NZBGet history');
}
const nzbgetItems: UsenetHistoryItem[] = nzbgetHistory.map((item: NzbgetHistoryItem) => ({
id: item.NZBID.toString(),
name: item.Name,
// Convert from MB to bytes
size: item.DownloadedSizeMB * 1000000,
time: item.DownloadTimeSec,
}));
return {
items: nzbgetItems,
total: nzbgetItems.length,
};
}
const { origin } = new URL(app.url);
const apiKey = app.integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) {
throw new Error(`API Key for app "${app.name}" is missing`);
}
const history = await new Client(origin, apiKey).history(input.offset, input.limit);
const items: UsenetHistoryItem[] = history.slots.map((slot) => ({
id: slot.nzo_id,
name: slot.name,
size: slot.bytes,
time: slot.download_time,
}));
return {
items,
total: history.noofslots,
};
}),
});
export interface UsenetInfoResponse {