diff options
| author | Fuwn <[email protected]> | 2023-08-26 22:29:03 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-08-26 22:29:03 -0700 |
| commit | b89d0e7dada186e31be37e62a7a75efc2dbe9c99 (patch) | |
| tree | 8c9f6b5d7aa0f709c06d5eb45fbf763883b21c89 /src/lib/AniList | |
| download | due.moe-b89d0e7dada186e31be37e62a7a75efc2dbe9c99.tar.xz due.moe-b89d0e7dada186e31be37e62a7a75efc2dbe9c99.zip | |
feat: initial build
Diffstat (limited to 'src/lib/AniList')
| -rw-r--r-- | src/lib/AniList/activity.ts | 23 | ||||
| -rw-r--r-- | src/lib/AniList/identity.ts | 32 | ||||
| -rw-r--r-- | src/lib/AniList/media.ts | 86 |
3 files changed, 141 insertions, 0 deletions
diff --git a/src/lib/AniList/activity.ts b/src/lib/AniList/activity.ts new file mode 100644 index 00000000..995ff6eb --- /dev/null +++ b/src/lib/AniList/activity.ts @@ -0,0 +1,23 @@ +import type { UserIdentity } from './identity'; + +export const lastActivityDate = async (userIdentity: UserIdentity): Promise<Date> => { + const activityHistory = await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: `{ Activity(userId: ${userIdentity.id}, type: MEDIA_LIST, sort: ID_DESC) { + __typename ... on ListActivity { createdAt } + } }` + }) + }) + ).json(); + const date = new Date(0); + + date.setUTCSeconds(activityHistory['data']['Activity']['createdAt']); + + return date; +}; diff --git a/src/lib/AniList/identity.ts b/src/lib/AniList/identity.ts new file mode 100644 index 00000000..a77891d6 --- /dev/null +++ b/src/lib/AniList/identity.ts @@ -0,0 +1,32 @@ +export interface UserIdentity { + id: number; + name: string; +} + +export interface AniListAuthorisation { + tokenType: string; + accessToken: string; + expiresIn: number; + refreshToken: string; +} + +export const userIdentity = async ( + anilistAuthorisation: AniListAuthorisation +): Promise<UserIdentity> => { + const userIdResponse = 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: `{ Viewer { id name } }` }) + }) + ).json(); + + return { + id: userIdResponse['data']['Viewer']['id'], + name: userIdResponse['data']['Viewer']['name'] + }; +}; diff --git a/src/lib/AniList/media.ts b/src/lib/AniList/media.ts new file mode 100644 index 00000000..bd481d9a --- /dev/null +++ b/src/lib/AniList/media.ts @@ -0,0 +1,86 @@ +import type { AniListAuthorisation } from '$lib/AniList/identity'; +import type { UserIdentity } from './identity'; + +export enum Type { + Anime, + Manga +} + +export interface Media { + id: number; + status: string; + type: string; + episodes: number; + format: string; + title: { + romaji: string; + english: string; + native: string; + }; + nextAiringEpisode?: { + episode: number; + timeUntilAiring?: number; + }; + mediaListEntry?: { + progress: number; + }; + startDate: { + year: number; + }; +} + +export const flattenLists = (lists: object[][]) => { + if (lists === undefined) { + return []; + } + + let flattenedList: any[] = []; + const minimisedList: Media[] = []; + + for (const list of lists) { + flattenedList = flattenedList.concat(list['entries']); + } + + for (const [position, entry] of flattenedList.entries()) { + minimisedList[position] = entry['media']; + } + + return minimisedList; +}; + +export const mediaListCollection = async ( + anilistAuthorisation: AniListAuthorisation, + userIdentity: UserIdentity, + type: Type +) => { + const userIdResponse = 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: `{ MediaListCollection(userId: ${userIdentity.id}, type: ${ + type === Type.Anime ? 'ANIME' : 'MANGA' + }, status_not_in: [ COMPLETED ]) { + lists { entries { media { + id + status + type + episodes + format + title { romaji english native } + nextAiringEpisode { episode timeUntilAiring } + mediaListEntry { progress } + startDate { year } + } } } + } + }` + }) + }) + ).json(); + + return userIdResponse['data']['MediaListCollection']['lists']; +}; |