aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/AniList/following.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-02-08 00:01:24 -0800
committerFuwn <[email protected]>2024-02-08 00:01:24 -0800
commitf78f5f4857f24ee5338fb1643c666a6b18d75769 (patch)
tree57b1b09f20b6b261a3b1ae15bfa441965f71ecd9 /src/lib/Data/AniList/following.ts
parentrefactor(data): move static data to module (diff)
downloaddue.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.ts56
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;
+};