diff options
| author | Fuwn <[email protected]> | 2023-09-27 23:33:01 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-27 23:33:01 -0700 |
| commit | 8562ba4280c575b3f04df598b7954a2d28b19e50 (patch) | |
| tree | eaa43530441522b5104a1fd1e404ce6b663fc9b0 /src/lib/AniList/wrapped.ts | |
| parent | fix(anime): template increment render (diff) | |
| download | due.moe-8562ba4280c575b3f04df598b7954a2d28b19e50.tar.xz due.moe-8562ba4280c575b3f04df598b7954a2d28b19e50.zip | |
feat(wrapped): initial wrapped prototype
Diffstat (limited to 'src/lib/AniList/wrapped.ts')
| -rw-r--r-- | src/lib/AniList/wrapped.ts | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/src/lib/AniList/wrapped.ts b/src/lib/AniList/wrapped.ts new file mode 100644 index 00000000..bd700101 --- /dev/null +++ b/src/lib/AniList/wrapped.ts @@ -0,0 +1,113 @@ +import type { AniListAuthorisation, UserIdentity } from './identity'; + +export interface Wrapped { + statistics: { + anime: { + startYears: { + startYear: number; + minutesWatched: number; + count: number; + }[]; + }; + manga: { + startYears: { + startYear: number; + chaptersRead: number; + count: number; + }[]; + }; + }; + activities: { + statusCount: number; + messageCount: number; + }; + avatar: { + large: string; + }; +} + +const profileActivities = async (user: AniListAuthorisation, identity: UserIdentity) => { + const get = async (page: number) => + await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + Authorization: `${user.tokenType} ${user.accessToken}`, + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ + Page(page: ${page}) { + activities(userId: ${identity.id}, type_in: [ TEXT, MESSAGE ]) { + ... on TextActivity { + type + } + ... on MessageActivity { + type + } + } + pageInfo { + hasNextPage + } + } +}` + }) + }) + ).json(); + + const pages = []; + let page = 1; + let response = await get(page); + + pages.push(response['data']['Page']['activities']); + + while (response['data']['Page']['pageInfo']['hasNextPage']) { + page += 1; + response = await get(page); + + pages.push(response['data']['Page']['activities']); + } + + return { + statusCount: pages.flat().filter((activity) => activity.type == 'TEXT').length, + messageCount: pages.flat().filter((activity) => activity.type == 'MESSAGE').length + }; +}; + +export const wrapped = async ( + anilistAuthorisation: AniListAuthorisation, + identity: UserIdentity +): Promise<Wrapped> => { + const wrappedResponse = await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + Authorization: `${anilistAuthorisation.tokenType} ${anilistAuthorisation.accessToken}`, + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ + User(name: "${identity.name}") { + avatar { large } + statistics { + anime { startYears { startYear minutesWatched count } } + manga { startYears { startYear chaptersRead count } } + } + } +}` + }) + }) + ).json(); + const { statusCount, messageCount } = await profileActivities(anilistAuthorisation, identity); + + return { + statistics: wrappedResponse['data']['User']['statistics'], + activities: { + statusCount, + messageCount + }, + avatar: wrappedResponse['data']['User']['avatar'] + }; +}; |