aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-01-02 15:21:59 -0800
committerFuwn <[email protected]>2024-01-02 15:21:59 -0800
commitdb17b25a759bde3ebca2b282d1bc45cd51a78840 (patch)
tree38125c3839269afa52e618c01c6e71b9bc67dc1b /src/lib/AniList
parentchore: remote debug logs (diff)
downloaddue.moe-db17b25a759bde3ebca2b282d1bc45cd51a78840.tar.xz
due.moe-db17b25a759bde3ebca2b282d1bc45cd51a78840.zip
feat(tools): random follower finder
Diffstat (limited to 'src/lib/AniList')
-rw-r--r--src/lib/AniList/following.ts56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lib/AniList/following.ts b/src/lib/AniList/following.ts
new file mode 100644
index 00000000..1bba66b9
--- /dev/null
+++ b/src/lib/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;
+};