aboutsummaryrefslogtreecommitdiff
path: root/src/lib/AniList/prequels.ts
blob: 726ad66e6f8950cd06ee658de2f3b286b169e75d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import type { AniListAuthorisation } from './identity';
import type { MediaTitle } from './media';

export interface MediaPrequel {
	id: number;
	title: MediaTitle;
	episodes: number;
	seen: number;
}

interface PrequelRelations {
	edges: {
		relationType: string;
		node: {
			title: MediaTitle;
			episodes: number;
			mediaListEntry: {
				status: string;
				progress: number;
			};
		};
	}[];
}

interface PrequelsPage {
	data: {
		Page: {
			media: {
				title: MediaTitle;
				id: number;
				relations: PrequelRelations;
			}[];
			pageInfo: {
				hasNextPage: boolean;
			};
		};
	};
}

const prequelsPage = async (
	page: number,
	anilistAuthorisation: AniListAuthorisation,
	year: number,
	season: 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'
): Promise<PrequelsPage> =>
	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: `{
  Page(page: ${page}) {
    pageInfo {
      hasNextPage
    }
    media(season: ${season}, seasonYear: ${year}) {
      title {
        english
        romaji
      }
      id
      relations {
        edges {
          relationType
          node {
            title {
              english
              romaji
            }
            episodes
            mediaListEntry {
              status
              progress
            }
          }
        }
      }
    }
  }
}`
			})
		})
	).json();

export const prequels = async (
	anilistAuthorisation: AniListAuthorisation,
	year: number,
	season: 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'
): Promise<MediaPrequel[]> => {
	const candidates = [];
	let page = 1;
	let currentPage = await prequelsPage(page, anilistAuthorisation, year, season);

	for (const candidate of currentPage.data.Page.media) candidates.push(candidate);

	while (currentPage['data']['Page']['pageInfo']['hasNextPage']) {
		for (const candidate of currentPage.data.Page.media) candidates.push(candidate);

		page += 1;
		currentPage = await prequelsPage(page, anilistAuthorisation, year, season);
	}

	const media: MediaPrequel[] = [];

	for (const candidate of candidates) {
		let episodes = 0;
		let seen = 0;

		for (const relation of candidate.relations.edges) {
			if (relation.relationType === 'PREQUEL') {
				if (
					relation.node.mediaListEntry === null ||
					relation.node.mediaListEntry.status !== 'COMPLETED'
				) {
					episodes += relation.node.episodes;

					if (relation.node.mediaListEntry !== null)
						seen += relation.node.mediaListEntry.progress || 0;
				}
			}
		}

		if (media.some((m) => m.id === candidate.id)) continue;

		if (episodes !== 0)
			media.push({
				id: candidate.id,
				title: candidate.title,
				episodes,
				seen
			});
	}

	return media;
};