aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/birthdays/primary/+server.ts
blob: 109961f816166a1f067a5a4d5412c66a4a057dad (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";
import { appOriginHeaders } from "$lib/Utility/appOrigin";

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: "" };

			const image = li.getElementsByClassName("item-cover")[0];

			return {
				image: image ? image.getAttribute("src") : "",
				name: title.textContent?.trim(),
			};
		}),
		{
			headers: appOriginHeaders({
				"Cache-Control": "max-age=10800, s-maxage=10800",
			}),
		},
	);
};