diff options
| author | Fuwn <[email protected]> | 2023-10-24 19:11:21 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-10-24 19:11:21 -0700 |
| commit | 7051fd67e98be0166d4f093f111dfa5dfa28bc0a (patch) | |
| tree | 61395da21f35395a88950428990f21ca9c12d261 /src/lib/AniList | |
| parent | feat: badge wall (diff) | |
| parent | chore(git): ignore data folder (diff) | |
| download | due.moe-7051fd67e98be0166d4f093f111dfa5dfa28bc0a.tar.xz due.moe-7051fd67e98be0166d4f093f111dfa5dfa28bc0a.zip | |
merge: main into badges
Diffstat (limited to 'src/lib/AniList')
| -rw-r--r-- | src/lib/AniList/forum.ts | 58 | ||||
| -rw-r--r-- | src/lib/AniList/user.ts | 5 |
2 files changed, 62 insertions, 1 deletions
diff --git a/src/lib/AniList/forum.ts b/src/lib/AniList/forum.ts new file mode 100644 index 00000000..6b95fa07 --- /dev/null +++ b/src/lib/AniList/forum.ts @@ -0,0 +1,58 @@ +import { user } from './user'; + +export interface Thread { + id: number; + title: string; + createdAt: number; +} + +export interface ThreadPage { + data: { + Page: { + threads: Thread[]; + pageInfo: { + hasNextPage: boolean; + currentPage: number; + }; + }; + }; +} + +const threadPage = async (page: number, userId: number): Promise<ThreadPage> => + await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ Page(perPage: 50, page: ${page}) { + threads(userId: ${userId}) { id title createdAt } + pageInfo { hasNextPage } +} }` + }) + }) + ).json(); + +export const threads = async (username: string): Promise<Thread[]> => { + const allThreads = []; + const userId = (await user(username)).id; + let page = 1; + let currentPage = await threadPage(page, userId); + + for (const thread of currentPage.data.Page.threads) { + allThreads.push(thread); + } + + while (currentPage.data.Page.pageInfo.hasNextPage) { + page += 1; + currentPage = await threadPage(page, userId); + + for (const thread of currentPage.data.Page.threads) { + allThreads.push(thread); + } + } + + return allThreads; +}; diff --git a/src/lib/AniList/user.ts b/src/lib/AniList/user.ts index dd9995fd..fdc98a58 100644 --- a/src/lib/AniList/user.ts +++ b/src/lib/AniList/user.ts @@ -15,6 +15,9 @@ export interface User { volumesRead: number; }; }; + avatar: { + large: string; + }; } export const user = async (username: string): Promise<User> => { @@ -28,7 +31,7 @@ export const user = async (username: string): Promise<User> => { }, body: JSON.stringify({ query: `{ User(name: "${username}") { - name id statistics { + name id avatar { large } statistics { anime { count meanScore minutesWatched episodesWatched } |