diff options
| author | Fuwn <[email protected]> | 2024-02-08 00:01:24 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-02-08 00:01:24 -0800 |
| commit | f78f5f4857f24ee5338fb1643c666a6b18d75769 (patch) | |
| tree | 57b1b09f20b6b261a3b1ae15bfa441965f71ecd9 /src/lib/Data/AniList/following.ts | |
| parent | refactor(data): move static data to module (diff) | |
| download | due.moe-f78f5f4857f24ee5338fb1643c666a6b18d75769.tar.xz due.moe-f78f5f4857f24ee5338fb1643c666a6b18d75769.zip | |
refactor(anilist): move to data module
Diffstat (limited to 'src/lib/Data/AniList/following.ts')
| -rw-r--r-- | src/lib/Data/AniList/following.ts | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib/Data/AniList/following.ts b/src/lib/Data/AniList/following.ts new file mode 100644 index 00000000..1bba66b9 --- /dev/null +++ b/src/lib/Data/AniList/following.ts @@ -0,0 +1,56 @@ +import { user, type User } from './user'; + +export interface FollowingPage { + data: { + Page: { + pageInfo: { + hasNextPage: boolean; + }; + following: Partial<User>[]; + }; + }; +} + +const followingPage = async (page: number, id: number): Promise<FollowingPage> => + await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ + Page(page: ${page}) { + pageInfo { hasNextPage } + following(userId: ${id}) { name id } + } + }` + }) + }) + ).json(); + +export const followers = async (name: string): Promise<Partial<User>[]> => { + const activities = []; + let page = 1; + const id = (await user(name)).id; + let currentPage = await followingPage(page, id); + + for (const activity of currentPage.data.Page.following) activities.push(activity); + + while (currentPage['data']['Page']['pageInfo']['hasNextPage']) { + for (const activity of currentPage.data.Page.following) activities.push(activity); + + page += 1; + currentPage = await followingPage(page, id); + } + + for (const activity of currentPage.data.Page.following) activities.push(activity); + + for (let i = activities.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [activities[i], activities[j]] = [activities[j], activities[i]]; + } + + return activities; +}; |