diff options
| author | Fuwn <[email protected]> | 2023-09-22 23:39:34 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-22 23:39:34 -0700 |
| commit | 1682e048fae37c482d20922b3c261dc83bcc0ba7 (patch) | |
| tree | f5ec98c3870e0c45368d9d89871f50f627aac48c /src | |
| parent | fix(settings): feedback line breaks (diff) | |
| download | due.moe-1682e048fae37c482d20922b3c261dc83bcc0ba7.tar.xz due.moe-1682e048fae37c482d20922b3c261dc83bcc0ba7.zip | |
feat(routes): tools
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/AniList/character.ts | 66 | ||||
| -rw-r--r-- | src/routes/tools/+page.svelte | 43 |
2 files changed, 109 insertions, 0 deletions
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<CharactersPage> => + 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<Character[]> => { + 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 @@ +<script lang="ts"> + import { todaysCharacterBirthdays } from '$lib/AniList/character'; + + let tool: number; +</script> + +<p> + <select bind:value={tool}> + <option value={0}>Tools</option> + <option value={1}>Today's Character Birthdays</option> + </select> +</p> + +{#if tool === 0} + Select a tool to continue. +{:else if tool === 1} + <ul> + {#await todaysCharacterBirthdays()} + <li>Loading ...</li> + {:then birthdays} + {#each birthdays as birthday} + <li> + <a href={`https://anilist.co/character/${birthday.id}`} target="_blank" + >{birthday.name.full}</a + > + </li> + {/each} + {:catch} + <li> + <p> + Characters could not be loaded. You might have been <a + href="https://en.wikipedia.org/wiki/Rate_limiting" + target="_blank">rate limited</a + >. + </p> + <p> + Try again in a few minutes. If the problem persists, please contact + <a href="https://anilist.co/user/fuwn" target="_blank">@fuwn</a> on AniList. + </p> + </li> + {/await} + </ul> +{/if} |