aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/birthdays/anisearch/+server.ts
blob: 7f071927b2b75581fba3244111182e4677ecd5a1 (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
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()
			};
		})
	);
};