aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/birthdays/primary/+server.ts
blob: 0b92a11e2b4122926f12e0ec0b8cc8550e32b6d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { JSDOM } from 'jsdom';

export const GET = async ({ url }: { url: URL }) => {
	const document = new JSDOM(
		await (
			await fetch(
				`https://www.anisearch.com/character/birthdays?month=${url.searchParams.get('month')}`
			)
		).text()
	).window.document;
	const section = document.querySelector(`#day-${url.searchParams.get('day')}`);

	if (!section) return Response.json([]);

	const ul = section.querySelector('ul.covers.simple');

	if (!ul) return Response.json([]);

	return Response.json(
		Array.from(ul.querySelectorAll('li')).map((li) => {
			const anchor = li.querySelector('a');
			const title = li.querySelector('.title');

			if (!anchor || !title) return { image: '', title: '' };

			return {
				image: anchor.getAttribute('data-bg')
					? `https://cdn.anisearch.com/images/${anchor.getAttribute('data-bg')}`
					: null,
				name: title.textContent?.trim()
			};
		}),
		{
			headers: {
				'Cache-Control': 'max-age=10800, s-maxage=10800',
				'Access-Control-Allow-Origin': 'https://due.moe'
			}
		}
	);
};