Files
Homarr/src/pages/api/modules/calendar.ts

94 lines
2.9 KiB
TypeScript
Raw Normal View History

import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
2022-12-11 14:11:25 +01:00
import { getConfig } from '../../../tools/config/getConfig';
import { AppIntegrationType } from '../../../types/app';
export default async (req: NextApiRequest, res: NextApiResponse) => {
// Filter out if the reuqest is a POST or a GET
2022-12-11 14:11:25 +01:00
if (req.method === 'GET') {
return Get(req, res);
}
return res.status(405).json({
statusCode: 405,
message: 'Method not allowed',
});
};
2022-12-11 14:11:25 +01:00
async function Get(req: NextApiRequest, res: NextApiResponse) {
// Parse req.body as a AppItem
2022-12-11 14:11:25 +01:00
const {
month: monthString,
year: yearString,
configName,
} = req.query as { month: string; year: string; configName: string };
2022-12-17 00:28:46 +09:00
const month = parseInt(monthString, 10);
const year = parseInt(yearString, 10);
2022-12-11 14:11:25 +01:00
2022-12-17 00:28:46 +09:00
if (Number.isNaN(month) || Number.isNaN(year) || !configName) {
2022-12-11 14:11:25 +01:00
return res.status(400).json({
statusCode: 400,
message: 'Missing required parameter in url: year, month or configName',
});
}
const config = getConfig(configName);
const mediaAppIntegrationTypes: AppIntegrationType['type'][] = [
2022-12-11 14:11:25 +01:00
'sonarr',
'radarr',
'readarr',
'lidarr',
];
const mediaApps = config.apps.filter(
(app) =>
app.integration && mediaAppIntegrationTypes.includes(app.integration.type)
2022-12-11 14:11:25 +01:00
);
const medias = await Promise.all(
await mediaApps.map(async (app) => {
const integration = app.integration!;
2022-12-11 14:11:25 +01:00
const endpoint = IntegrationTypeEndpointMap.get(integration.type);
2022-12-17 00:28:46 +09:00
if (!endpoint) {
2022-12-11 14:11:25 +01:00
return {
type: integration.type,
items: [],
};
2022-12-17 00:28:46 +09:00
}
2022-12-11 14:11:25 +01:00
// Get the origin URL
let { href: origin } = new URL(app.url);
2022-12-11 14:11:25 +01:00
if (origin.endsWith('/')) {
origin = origin.slice(0, -1);
}
const start = new Date(year, month - 1, 1); // First day of month
const end = new Date(year, month, 0); // Last day of month
const apiKey = integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) return { type: integration.type, items: [] };
2022-12-17 00:28:46 +09:00
return axios
2022-12-11 14:11:25 +01:00
.get(
`${origin}${endpoint}?apiKey=${apiKey}&end=${end.toISOString()}&start=${start.toISOString()}`
2022-12-11 14:11:25 +01:00
)
.then((x) => ({ type: integration.type, items: x.data as any[] }));
})
);
// FIXME: I need an integration for each of them
return res.status(200).json({
tvShows: medias.filter((m) => m.type === 'sonarr').flatMap((m) => m.items),
movies: medias.filter((m) => m.type === 'radarr').flatMap((m) => m.items),
books: medias.filter((m) => m.type === 'readarr').flatMap((m) => m.items),
musics: medias.filter((m) => m.type === 'lidarr').flatMap((m) => m.items),
totalCount: medias.reduce((p, c) => p + c.items.length, 0),
});
}
const IntegrationTypeEndpointMap = new Map<AppIntegrationType['type'], string>([
2022-12-11 14:11:25 +01:00
['sonarr', '/api/calendar'],
['radarr', '/api/v3/calendar'],
['lidarr', '/api/v1/calendar'],
['readarr', '/api/v1/calendar'],
]);