mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-12 08:25:47 +01:00
✨Add useNet tile
This commit is contained in:
@@ -4,11 +4,11 @@ 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 { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
import { NzbgetHistoryItem } from './nzbget/types';
|
||||
import { NzbgetClient } from './nzbget/nzbget-client';
|
||||
import { getConfig } from '../../../../tools/config/getConfig';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
@@ -26,24 +26,24 @@ export interface UsenetHistoryResponse {
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const config = getConfig(configName?.toString() ?? 'default');
|
||||
const { limit, offset, serviceId } = req.query as any as UsenetHistoryRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
const service = config.services.find((x) => x.id === serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
let response: UsenetHistoryResponse;
|
||||
switch (service.type) {
|
||||
case 'NZBGet': {
|
||||
switch (service.integration?.type) {
|
||||
case 'nzbGet': {
|
||||
const url = new URL(service.url);
|
||||
const options = {
|
||||
host: url.hostname,
|
||||
port: url.port,
|
||||
login: service.username,
|
||||
hash: service.password,
|
||||
login: service.integration.properties.username,
|
||||
hash: service.integration.properties.password,
|
||||
};
|
||||
|
||||
const nzbGet = NzbgetClient(options);
|
||||
@@ -76,14 +76,17 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
};
|
||||
break;
|
||||
}
|
||||
case 'Sabnzbd': {
|
||||
case 'sabnzbd': {
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
if (!service.apiKey) {
|
||||
if (!service.integration.properties.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const history = await new Client(origin, service.apiKey).history(offset, limit);
|
||||
const history = await new Client(origin, service.integration.properties.apiKey).history(
|
||||
offset,
|
||||
limit
|
||||
);
|
||||
|
||||
const items: UsenetHistoryItem[] = history.slots.map((slot) => ({
|
||||
id: slot.nzo_id,
|
||||
@@ -99,7 +102,7 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Service type "${service.type}" unrecognized.`);
|
||||
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
|
||||
}
|
||||
|
||||
return res.status(200).json(response);
|
||||
|
||||
@@ -3,11 +3,11 @@ 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 { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
import { NzbgetStatus } from './nzbget/types';
|
||||
import { NzbgetClient } from './nzbget/nzbget-client';
|
||||
import { getConfig } from '../../../../tools/config/getConfig';
|
||||
|
||||
dayjs.extend(duration);
|
||||
|
||||
@@ -25,24 +25,24 @@ export interface UsenetInfoResponse {
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const config = getConfig(configName?.toString() ?? 'default');
|
||||
const { serviceId } = req.query as any as UsenetInfoRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
const service = config.services.find((x) => x.id === serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
let response: UsenetInfoResponse;
|
||||
switch (service.type) {
|
||||
case 'NZBGet': {
|
||||
switch (service.integration?.type) {
|
||||
case 'nzbGet': {
|
||||
const url = new URL(service.url);
|
||||
const options = {
|
||||
host: url.hostname,
|
||||
port: url.port,
|
||||
login: service.username,
|
||||
hash: service.password,
|
||||
login: service.integration.properties.username,
|
||||
hash: service.integration.properties.password,
|
||||
};
|
||||
|
||||
const nzbGet = NzbgetClient(options);
|
||||
@@ -71,14 +71,14 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
};
|
||||
break;
|
||||
}
|
||||
case 'Sabnzbd': {
|
||||
if (!service.apiKey) {
|
||||
case 'sabnzbd': {
|
||||
if (!service.integration.properties.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
const queue = await new Client(origin, service.apiKey).queue(0, -1);
|
||||
const queue = await new Client(origin, service.integration.properties.apiKey).queue(0, -1);
|
||||
|
||||
const [hours, minutes, seconds] = queue.timeleft.split(':');
|
||||
const eta = dayjs.duration({
|
||||
@@ -96,7 +96,7 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Service type "${service.type}" unrecognized.`);
|
||||
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
|
||||
}
|
||||
|
||||
return res.status(200).json(response);
|
||||
|
||||
@@ -3,9 +3,7 @@ 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 { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
import { getConfig } from '../../../../tools/config/getConfig';
|
||||
import { NzbgetClient } from './nzbget/nzbget-client';
|
||||
|
||||
dayjs.extend(duration);
|
||||
@@ -17,24 +15,24 @@ export interface UsenetPauseRequestParams {
|
||||
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const config = getConfig(configName?.toString() ?? 'default');
|
||||
const { serviceId } = req.query as any as UsenetPauseRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
const service = config.services.find((x) => x.id === serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
let result;
|
||||
switch (service.type) {
|
||||
case 'NZBGet': {
|
||||
switch (service.integration?.type) {
|
||||
case 'nzbGet': {
|
||||
const url = new URL(service.url);
|
||||
const options = {
|
||||
host: url.hostname,
|
||||
port: url.port,
|
||||
login: service.username,
|
||||
hash: service.password,
|
||||
login: service.integration.properties.username,
|
||||
hash: service.integration.properties.password,
|
||||
};
|
||||
|
||||
const nzbGet = NzbgetClient(options);
|
||||
@@ -50,18 +48,18 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'Sabnzbd': {
|
||||
if (!service.apiKey) {
|
||||
case 'sabnzbd': {
|
||||
if (!service.integration.properties.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
result = await new Client(origin, service.apiKey).queuePause();
|
||||
result = await new Client(origin, service.integration.properties.apiKey).queuePause();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Service type "${service.type}" unrecognized.`);
|
||||
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
|
||||
}
|
||||
|
||||
return res.status(200).json(result);
|
||||
|
||||
@@ -4,9 +4,7 @@ import duration from 'dayjs/plugin/duration';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { Client } from 'sabnzbd-api';
|
||||
import { UsenetQueueItem } from '../../../../modules';
|
||||
import { getConfig } from '../../../../tools/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
import { getConfig } from '../../../../tools/config/getConfig';
|
||||
import { NzbgetClient } from './nzbget/nzbget-client';
|
||||
import { NzbgetQueueItem, NzbgetStatus } from './nzbget/types';
|
||||
|
||||
@@ -26,24 +24,24 @@ export interface UsenetQueueResponse {
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const config = getConfig(configName?.toString() ?? 'default');
|
||||
const { limit, offset, serviceId } = req.query as any as UsenetQueueRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
const service = config.services.find((x) => x.id === serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
let response: UsenetQueueResponse;
|
||||
switch (service.type) {
|
||||
case 'NZBGet': {
|
||||
switch (service.integration?.type) {
|
||||
case 'nzbGet': {
|
||||
const url = new URL(service.url);
|
||||
const options = {
|
||||
host: url.hostname,
|
||||
port: url.port,
|
||||
login: service.username,
|
||||
hash: service.password,
|
||||
login: service.integration.properties.username,
|
||||
hash: service.integration.properties.password,
|
||||
};
|
||||
|
||||
const nzbGet = NzbgetClient(options);
|
||||
@@ -92,13 +90,16 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
};
|
||||
break;
|
||||
}
|
||||
case 'Sabnzbd': {
|
||||
if (!service.apiKey) {
|
||||
case 'sabnzbd': {
|
||||
if (!service.integration.properties.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
const queue = await new Client(origin, service.apiKey).queue(offset, limit);
|
||||
const queue = await new Client(origin, service.integration.properties.apiKey).queue(
|
||||
offset,
|
||||
limit
|
||||
);
|
||||
|
||||
const items: UsenetQueueItem[] = queue.slots.map((slot) => {
|
||||
const [hours, minutes, seconds] = slot.timeleft.split(':');
|
||||
@@ -125,7 +126,7 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Service type "${service.type}" unrecognized.`);
|
||||
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
|
||||
}
|
||||
|
||||
return res.status(200).json(response);
|
||||
|
||||
@@ -3,7 +3,7 @@ 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 { getConfig } from '../../../../tools/config/getConfig';
|
||||
import { getServiceById } from '../../../../tools/hooks/useGetServiceByType';
|
||||
import { Config } from '../../../../tools/types';
|
||||
import { NzbgetClient } from './nzbget/nzbget-client';
|
||||
@@ -18,24 +18,24 @@ export interface UsenetResumeRequestParams {
|
||||
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
try {
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const config = getConfig(configName?.toString() ?? 'default');
|
||||
const { serviceId } = req.query as any as UsenetResumeRequestParams;
|
||||
|
||||
const service = getServiceById(config, serviceId);
|
||||
const service = config.services.find((x) => x.id === serviceId);
|
||||
|
||||
if (!service) {
|
||||
throw new Error(`Service with ID "${req.query.serviceId}" could not be found.`);
|
||||
}
|
||||
|
||||
let result;
|
||||
switch (service.type) {
|
||||
case 'NZBGet': {
|
||||
switch (service.integration?.type) {
|
||||
case 'nzbGet': {
|
||||
const url = new URL(service.url);
|
||||
const options = {
|
||||
host: url.hostname,
|
||||
port: url.port,
|
||||
login: service.username,
|
||||
hash: service.password,
|
||||
login: service.integration.properties.username,
|
||||
hash: service.integration.properties.password,
|
||||
};
|
||||
|
||||
const nzbGet = NzbgetClient(options);
|
||||
@@ -51,18 +51,18 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'Sabnzbd': {
|
||||
if (!service.apiKey) {
|
||||
case 'sabnzbd': {
|
||||
if (!service.integration.properties.apiKey) {
|
||||
throw new Error(`API Key for service "${service.name}" is missing`);
|
||||
}
|
||||
|
||||
const { origin } = new URL(service.url);
|
||||
|
||||
result = await new Client(origin, service.apiKey).queueResume();
|
||||
result = await new Client(origin, service.integration.properties.apiKey).queueResume();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Service type "${service.type}" unrecognized.`);
|
||||
throw new Error(`Service type "${service.integration?.type}" unrecognized.`);
|
||||
}
|
||||
|
||||
return res.status(200).json(result);
|
||||
|
||||
Reference in New Issue
Block a user