aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-12-20 02:20:37 -0800
committerFuwn <[email protected]>2023-12-20 02:20:37 -0800
commita87fee111ccb501121b5aa3c1e48601f4869198a (patch)
treec1c55fc24a03bc7be848ee753ff76a76f397b4e2 /src/lib
parentfeat(attributions): add more anilist details (diff)
downloaddue.moe-a87fee111ccb501121b5aa3c1e48601f4869198a.tar.xz
due.moe-a87fee111ccb501121b5aa3c1e48601f4869198a.zip
feat(schedule): match media to anilist
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/AniList/schedule.ts75
-rw-r--r--src/lib/Media/Anime/airing.ts24
-rw-r--r--src/lib/Media/Anime/season.ts11
3 files changed, 109 insertions, 1 deletions
diff --git a/src/lib/AniList/schedule.ts b/src/lib/AniList/schedule.ts
new file mode 100644
index 00000000..33351a09
--- /dev/null
+++ b/src/lib/AniList/schedule.ts
@@ -0,0 +1,75 @@
+import type { Media, MediaTitle } from './media';
+
+interface SchedulePage {
+ data: {
+ Page: {
+ media: {
+ title: MediaTitle;
+ synonyms: string[];
+ id: number;
+ idMal: number;
+ episodes: number;
+ nextAiringEpisode?: {
+ episode: number;
+ airingAt?: number;
+ };
+ coverImage: {
+ extraLarge: string;
+ };
+ }[];
+ pageInfo: {
+ hasNextPage: boolean;
+ };
+ };
+ };
+}
+
+const schedulePage = async (
+ page: number,
+ year: number,
+ season: 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'
+): Promise<SchedulePage> =>
+ await (
+ await fetch('https://graphql.anilist.co', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
+ },
+ body: JSON.stringify({
+ query: `{
+ Page(page: ${page}) {
+ pageInfo {
+ hasNextPage
+ }
+ media(season: ${season}, seasonYear: ${year}) {
+ id idMal episodes synonyms
+ title { english romaji native }
+ nextAiringEpisode { episode airingAt }
+ coverImage { extraLarge }
+ }
+ }
+}`
+ })
+ })
+ ).json();
+
+export const scheduleMediaListCollection = async (
+ year: number,
+ season: 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL'
+) => {
+ const scheduledMedia = [];
+ let page = 1;
+ let currentPage = await schedulePage(page, year, season);
+
+ for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate);
+
+ while (currentPage['data']['Page']['pageInfo']['hasNextPage']) {
+ for (const candidate of currentPage.data.Page.media) scheduledMedia.push(candidate);
+
+ page += 1;
+ currentPage = await schedulePage(page, year, season);
+ }
+
+ return scheduledMedia as Partial<Media[]>;
+};
diff --git a/src/lib/Media/Anime/airing.ts b/src/lib/Media/Anime/airing.ts
index 15abbf2a..9c13fb5d 100644
--- a/src/lib/Media/Anime/airing.ts
+++ b/src/lib/Media/Anime/airing.ts
@@ -6,7 +6,7 @@ import type { SubsPlease } from '$lib/subsPlease';
import levenshtein from 'fast-levenshtein';
import { totalEpisodes } from './episodes';
-interface Time {
+export interface Time {
title: string;
time: string;
day: string;
@@ -66,6 +66,28 @@ const findClosestMatch = (times: Time[], titles: string[]) => {
return closestMatch as Time | null;
};
+export const findClosestMedia = (media: Media[], matchFor: string) => {
+ if (!matchFor) return null;
+
+ let bestFitMedia: Media | null = null;
+ let smallestDistance = Infinity;
+
+ const normalizedSingleTitle = normalizeTitle(matchFor);
+
+ media.forEach((m) => {
+ [m.title.romaji, m.title.english, ...m.synonyms].filter(Boolean).forEach((title) => {
+ const distance = levenshtein.get(normalizedSingleTitle, normalizeTitle(title));
+
+ if (distance < smallestDistance && distance < normalizedSingleTitle.length / 2) {
+ smallestDistance = distance;
+ bestFitMedia = m;
+ }
+ });
+ });
+
+ return bestFitMedia as Media | null;
+};
+
export const injectAiringTime = (anime: Media, subsPlease: SubsPlease | null) => {
const airingAt = anime.nextAiringEpisode?.airingAt;
// const nativeUntilAiring = airingAt
diff --git a/src/lib/Media/Anime/season.ts b/src/lib/Media/Anime/season.ts
new file mode 100644
index 00000000..d7922e2b
--- /dev/null
+++ b/src/lib/Media/Anime/season.ts
@@ -0,0 +1,11 @@
+export const season = () => {
+ if (new Date().getMonth() >= 0 && new Date().getMonth() <= 2)
+ return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+ else if (new Date().getMonth() >= 3 && new Date().getMonth() <= 5)
+ return 'SPRING' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+ else if (new Date().getMonth() >= 6 && new Date().getMonth() <= 8)
+ return 'SUMMER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+ else if (new Date().getMonth() >= 9 && new Date().getMonth() <= 11)
+ return 'FALL' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+ else return 'WINTER' as 'WINTER' | 'SPRING' | 'SUMMER' | 'FALL';
+};