aboutsummaryrefslogtreecommitdiff
path: root/lib/anilist/getUpcomingAnime.js
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-07-16 22:35:39 +0700
committerFactiven <[email protected]>2023-07-16 22:35:39 +0700
commit1eee181e219dfd993d396ac3169e7aad3dd285eb (patch)
tree23fe54e9c3f8810f3ac9ab6b29070b4f0d4b9d20 /lib/anilist/getUpcomingAnime.js
parentremoved console.log (diff)
downloadmoopa-1eee181e219dfd993d396ac3169e7aad3dd285eb.tar.xz
moopa-1eee181e219dfd993d396ac3169e7aad3dd285eb.zip
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)
Diffstat (limited to 'lib/anilist/getUpcomingAnime.js')
-rw-r--r--lib/anilist/getUpcomingAnime.js80
1 files changed, 80 insertions, 0 deletions
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;