From a7fe390be2b812e77395738eba395835dc45f007 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Mon, 22 Jul 2024 16:00:33 -0700 Subject: refactor(Birthday): rename modules --- src/routes/api/birthdays/primary/+server.ts | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/routes/api/birthdays/primary/+server.ts (limited to 'src/routes/api/birthdays/primary') diff --git a/src/routes/api/birthdays/primary/+server.ts b/src/routes/api/birthdays/primary/+server.ts new file mode 100644 index 00000000..0b92a11e --- /dev/null +++ b/src/routes/api/birthdays/primary/+server.ts @@ -0,0 +1,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' + } + } + ); +}; -- cgit v1.2.3