From 1eee181e219dfd993d396ac3169e7aad3dd285eb Mon Sep 17 00:00:00 2001 From: Factiven Date: Sun, 16 Jul 2023 22:35:39 +0700 Subject: Update v3.6.4 - Added Manga page with a working tracker for AniList user - Added schedule component to home page - Added disqus comment section so you can fight on each other (not recommended) - Added /id and /en route for english and indonesian subs (id route still work in progress) --- lib/anilist/getUpcomingAnime.js | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 lib/anilist/getUpcomingAnime.js (limited to 'lib/anilist/getUpcomingAnime.js') diff --git a/lib/anilist/getUpcomingAnime.js b/lib/anilist/getUpcomingAnime.js new file mode 100644 index 0000000..fc848fd --- /dev/null +++ b/lib/anilist/getUpcomingAnime.js @@ -0,0 +1,80 @@ +const getUpcomingAnime = async () => { + // Determine the current season and year + const currentDate = new Date(); + const currentMonth = currentDate.getMonth(); + let currentSeason, currentYear; + + if (currentMonth < 3) { + currentSeason = "WINTER"; + currentYear = currentDate.getFullYear(); + } else if (currentMonth < 6) { + currentSeason = "SPRING"; + currentYear = currentDate.getFullYear(); + } else if (currentMonth < 9) { + currentSeason = "SUMMER"; + currentYear = currentDate.getFullYear(); + } else { + currentSeason = "FALL"; + currentYear = currentDate.getFullYear(); + } + + const query = ` + query ($season: MediaSeason, $seasonYear: Int) { + Page(page: 1, perPage: 20) { + media(season: $season, seasonYear: $seasonYear, sort: POPULARITY_DESC, type: ANIME) { + id + coverImage{ + large + } + bannerImage + title { + english + romaji + native + } + nextAiringEpisode { + episode + airingAt + timeUntilAiring + } + } + } + } + `; + + const variables = { + season: currentSeason, + seasonYear: currentYear, + }; + + let response = await fetch("https://graphql.anilist.co", { + method: "POST", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + body: JSON.stringify({ + query, + variables: variables ? variables : undefined, + }), + }); + + let json = await response.json(); + + let currentSeasonAnime = json.data.Page.media; + let nextAiringAnime = currentSeasonAnime.filter( + (anime) => + anime.nextAiringEpisode !== null && anime.nextAiringEpisode.episode === 1 + ); + + if (nextAiringAnime.length >= 1) { + nextAiringAnime.sort( + (a, b) => a.nextAiringEpisode.airingAt - b.nextAiringEpisode.airingAt + ); + return nextAiringAnime; // return all upcoming anime, not just the first two + } + + return null; +}; + +export default getUpcomingAnime; -- cgit v1.2.3