aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/media.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/AniList/media.ts')
-rw-r--r--src/lib/AniList/media.ts86
1 files changed, 86 insertions, 0 deletions
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'];
+};