diff options
| author | Fuwn <[email protected]> | 2023-12-03 15:43:05 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-12-03 15:43:05 -0800 |
| commit | 841b28a408df43b5380fa00388075a07129f7146 (patch) | |
| tree | 901c731e66c8d1f3a335148732f655be9a79be9f /src/routes/api/birthdays | |
| parent | feat(wrapped): remove old colour preview (diff) | |
| download | due.moe-841b28a408df43b5380fa00388075a07129f7146.tar.xz due.moe-841b28a408df43b5380fa00388075a07129f7146.zip | |
feat(birthdays): add second birthday source
Diffstat (limited to 'src/routes/api/birthdays')
| -rw-r--r-- | src/routes/api/birthdays/anisearch/+server.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/routes/api/birthdays/anisearch/+server.ts b/src/routes/api/birthdays/anisearch/+server.ts new file mode 100644 index 00000000..7cafb5ea --- /dev/null +++ b/src/routes/api/birthdays/anisearch/+server.ts @@ -0,0 +1,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() + }; + }) + ); +}; |