mirror of
https://github.com/ajnart/homarr.git
synced 2025-11-10 15:35:55 +01:00
🔀 Merge branch 'dev' into overseerr-integration
This commit is contained in:
83
src/pages/api/modules/calendar.ts
Normal file
83
src/pages/api/modules/calendar.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import axios from 'axios';
|
||||
import { getCookie } from 'cookies-next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getConfig } from '../../../tools/getConfig';
|
||||
import { Config } from '../../../tools/types';
|
||||
|
||||
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
// Parse req.body as a ServiceItem
|
||||
const { id } = req.body;
|
||||
const { type } = req.query;
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
// Find service with serviceId in config
|
||||
const service = config.services.find((service) => service.id === id);
|
||||
if (!service) {
|
||||
return res.status(500).json({
|
||||
statusCode: 500,
|
||||
message: 'Missing service',
|
||||
});
|
||||
}
|
||||
|
||||
const nextMonth = new Date(new Date().setMonth(new Date().getMonth() + 2)).toISOString();
|
||||
const lastMonth = new Date(new Date().setMonth(new Date().getMonth() - 2)).toISOString();
|
||||
const TypeToUrl: { service: string; url: string }[] = [
|
||||
{
|
||||
service: 'sonarr',
|
||||
url: '/api/calendar',
|
||||
},
|
||||
{
|
||||
service: 'radarr',
|
||||
url: '/api/v3/calendar',
|
||||
},
|
||||
{
|
||||
service: 'lidarr',
|
||||
url: '/api/v1/calendar',
|
||||
},
|
||||
{
|
||||
service: 'readarr',
|
||||
url: '/api/v1/calendar',
|
||||
},
|
||||
];
|
||||
if (!type) {
|
||||
return res.status(400).json({
|
||||
message: 'Missing required parameter in url: type',
|
||||
});
|
||||
}
|
||||
if (!service) {
|
||||
return res.status(400).json({
|
||||
message: 'Missing required parameter in body: service',
|
||||
});
|
||||
}
|
||||
// Match the type to the correct url
|
||||
const url = TypeToUrl.find((x) => x.service === type);
|
||||
if (!url) {
|
||||
return res.status(400).json({
|
||||
message: 'Invalid type',
|
||||
});
|
||||
}
|
||||
// Get the origin URL
|
||||
let { href: origin } = new URL(service.url);
|
||||
if (origin.endsWith('/')) {
|
||||
origin = origin.slice(0, -1);
|
||||
}
|
||||
const pined = `${origin}${url?.url}?apiKey=${service.apiKey}&end=${nextMonth}&start=${lastMonth}`;
|
||||
return axios
|
||||
.get(`${origin}${url?.url}?apiKey=${service.apiKey}&end=${nextMonth}&start=${lastMonth}`)
|
||||
.then((response) => res.status(200).json(response.data))
|
||||
.catch((e) => res.status(500).json(e));
|
||||
// // Make a request to the URL
|
||||
// const response = await axios.get(url);
|
||||
// // Return the response
|
||||
}
|
||||
|
||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
// Filter out if the reuqest is a POST or a GET
|
||||
if (req.method === 'POST') {
|
||||
return Post(req, res);
|
||||
}
|
||||
return res.status(405).json({
|
||||
statusCode: 405,
|
||||
message: 'Method not allowed',
|
||||
});
|
||||
};
|
||||
@@ -1,42 +1,66 @@
|
||||
import { Deluge } from '@ctrl/deluge';
|
||||
import { QBittorrent } from '@ctrl/qbittorrent';
|
||||
import { NormalizedTorrent } from '@ctrl/shared-torrent';
|
||||
import { Transmission } from '@ctrl/transmission';
|
||||
import { getCookie } from 'cookies-next';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { getConfig } from '../../../tools/getConfig';
|
||||
import { Config } from '../../../tools/types';
|
||||
|
||||
async function Post(req: NextApiRequest, res: NextApiResponse) {
|
||||
// Get the type of service from the request url
|
||||
const { dlclient } = req.query;
|
||||
const { body } = req;
|
||||
// Get login, password and url from the body
|
||||
const { username, password, url } = body;
|
||||
if (!dlclient || (!username && !password) || !url) {
|
||||
return res.status(400).json({
|
||||
error: 'Wrong request',
|
||||
const configName = getCookie('config-name', { req });
|
||||
const { config }: { config: Config } = getConfig(configName?.toString() ?? 'default').props;
|
||||
const qBittorrentServices = config.services.filter((service) => service.type === 'qBittorrent');
|
||||
const delugeServices = config.services.filter((service) => service.type === 'Deluge');
|
||||
const transmissionServices = config.services.filter((service) => service.type === 'Transmission');
|
||||
|
||||
const torrents: NormalizedTorrent[] = [];
|
||||
|
||||
if (!qBittorrentServices && !delugeServices && !transmissionServices) {
|
||||
return res.status(500).json({
|
||||
statusCode: 500,
|
||||
message: 'Missing services',
|
||||
});
|
||||
}
|
||||
let client: Deluge | QBittorrent;
|
||||
switch (dlclient) {
|
||||
case 'qbit':
|
||||
client = new QBittorrent({
|
||||
baseUrl: new URL(url).origin,
|
||||
username,
|
||||
password,
|
||||
});
|
||||
break;
|
||||
case 'deluge':
|
||||
client = new Deluge({
|
||||
baseUrl: new URL(url).origin,
|
||||
password,
|
||||
});
|
||||
break;
|
||||
default:
|
||||
return res.status(400).json({
|
||||
error: 'Wrong request',
|
||||
});
|
||||
try {
|
||||
await Promise.all(
|
||||
qBittorrentServices.map((service) =>
|
||||
new QBittorrent({
|
||||
baseUrl: service.url,
|
||||
username: service.username,
|
||||
password: service.password,
|
||||
})
|
||||
.getAllData()
|
||||
.then((e) => torrents.push(...e.torrents))
|
||||
)
|
||||
);
|
||||
await Promise.all(
|
||||
delugeServices.map((service) =>
|
||||
new Deluge({
|
||||
baseUrl: service.url,
|
||||
password: 'password' in service ? service.password : '',
|
||||
})
|
||||
.getAllData()
|
||||
.then((e) => torrents.push(...e.torrents))
|
||||
)
|
||||
);
|
||||
// Map transmissionServices
|
||||
await Promise.all(
|
||||
transmissionServices.map((service) =>
|
||||
new Transmission({
|
||||
baseUrl: service.url,
|
||||
username: 'username' in service ? service.username : '',
|
||||
password: 'password' in service ? service.password : '',
|
||||
})
|
||||
.getAllData()
|
||||
.then((e) => torrents.push(...e.torrents))
|
||||
)
|
||||
);
|
||||
} catch (e: any) {
|
||||
return res.status(401).json(e);
|
||||
}
|
||||
const data = await client.getAllData();
|
||||
res.status(200).json({
|
||||
torrents: data.torrents,
|
||||
});
|
||||
return res.status(200).json(torrents);
|
||||
}
|
||||
|
||||
export default async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
|
||||
@@ -7,10 +7,14 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
await axios
|
||||
.get(url as string)
|
||||
.then((response) => {
|
||||
res.status(200).json(response.data);
|
||||
res.status(response.status).json(response.statusText);
|
||||
})
|
||||
.catch((error) => {
|
||||
res.status(500).json(error);
|
||||
if (error.response) {
|
||||
res.status(error.response.status).json(error.response.statusText);
|
||||
} else {
|
||||
res.status(500).json('Server Error');
|
||||
}
|
||||
});
|
||||
// // Make a request to the URL
|
||||
// const response = await axios.get(url);
|
||||
|
||||
19
src/pages/api/modules/search.ts
Normal file
19
src/pages/api/modules/search.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import axios from 'axios';
|
||||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
|
||||
async function Get(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { q } = req.query;
|
||||
const response = await axios.get(`https://duckduckgo.com/ac/?q=${q}`);
|
||||
res.status(200).json(response.data);
|
||||
}
|
||||
|
||||
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',
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user