aboutsummaryrefslogtreecommitdiff
path: root/src/routes/api/birthdays/primary/+server.ts
blob: 774d47e1726c288f13f035c9c4f26b52448164d5 (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: '' };

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

      return {
        image: image ? image.getAttribute('src') : '',
        name: title.textContent?.trim()
      };
    }),
    {
      headers: {
        'Cache-Control': 'max-age=10800, s-maxage=10800',
        'Access-Control-Allow-Origin': 'https://due.moe'
      }
    }
  );
};