From 1682e048fae37c482d20922b3c261dc83bcc0ba7 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Fri, 22 Sep 2023 23:39:34 -0700 Subject: feat(routes): tools --- src/lib/AniList/character.ts | 66 +++++++++++++++++++++++++++++++++++++++++++ src/routes/tools/+page.svelte | 43 ++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 src/lib/AniList/character.ts create mode 100644 src/routes/tools/+page.svelte diff --git a/src/lib/AniList/character.ts b/src/lib/AniList/character.ts new file mode 100644 index 00000000..27caef99 --- /dev/null +++ b/src/lib/AniList/character.ts @@ -0,0 +1,66 @@ +export interface Character { + name: { + full: string; + }; + id: number; +} + +export interface CharactersPage { + data: { + Page: { + characters: Character[]; + pageInfo: { + hasNextPage: boolean; + currentPage: number; + }; + }; + }; +} + +const charactersPage = async (page: number): Promise => + await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ Page(page: ${page}, perPage: 50) { + characters(isBirthday: true) { name { full } id } + pageInfo { hasNextPage currentPage } + } }` + }) + }) + ).json(); + +export const todaysCharacterBirthdays = async (): Promise => { + const characters = []; + let page = 1; + let currentPage = await charactersPage(page); + + for (const character of currentPage['data']['Page']['characters']) { + characters.push({ + id: character['id'], + name: { + full: character['name']['full'] + } + }); + } + + while (currentPage['data']['Page']['pageInfo']['hasNextPage']) { + for (const character of currentPage['data']['Page']['characters']) { + characters.push({ + id: character['id'], + name: { + full: character['name']['full'] + } + }); + } + + page += 1; + currentPage = await charactersPage(page); + } + + return characters; +}; diff --git a/src/routes/tools/+page.svelte b/src/routes/tools/+page.svelte new file mode 100644 index 00000000..53ace72b --- /dev/null +++ b/src/routes/tools/+page.svelte @@ -0,0 +1,43 @@ + + +

+ +

+ +{#if tool === 0} + Select a tool to continue. +{:else if tool === 1} +
    + {#await todaysCharacterBirthdays()} +
  • Loading ...
  • + {:then birthdays} + {#each birthdays as birthday} +
  • + {birthday.name.full} +
  • + {/each} + {:catch} +
  • +

    + Characters could not be loaded. You might have been rate limited. +

    +

    + Try again in a few minutes. If the problem persists, please contact + @fuwn on AniList. +

    +
  • + {/await} +
+{/if} -- cgit v1.2.3