aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-10-22 14:03:08 -0700
committerFuwn <[email protected]>2023-10-22 14:03:08 -0700
commit0c6fbd9b1afe8e5967a127bffa68570ee88bfe92 (patch)
treedf5afd62f88a26e61bc92e3848d6d2c5a659dd7b /src/lib/AniList
parentfeat(tools): default selection query (diff)
downloaddue.moe-0c6fbd9b1afe8e5967a127bffa68570ee88bfe92.tar.xz
due.moe-0c6fbd9b1afe8e5967a127bffa68570ee88bfe92.zip
feat(tools): episode discussion collector
Diffstat (limited to 'src/lib/AniList')
-rw-r--r--src/lib/AniList/forum.ts58
1 files changed, 58 insertions, 0 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;
+};