From 4bf2bdf7e15d47d766adf1c787020daf975c8dcf Mon Sep 17 00:00:00 2001 From: Erick Lima Date: Sat, 5 Oct 2024 20:13:24 -0300 Subject: [PATCH 1/2] refactor: Change Aniwatch URL to new version, update fetch functions Update Aniwatch API URLs and function names, and refactor related code in components and pages. --- app/api/aniwatch.ts | 13 ++--- app/components/NavigationButtons/index.tsx | 2 +- .../dataFetch/optimizedFetchAnimeOptions.ts | 12 ++-- .../AnimeEpisodesContainer/index.tsx | 58 +++++++++---------- .../[id]/components/VideoPlayer/index.tsx | 2 +- app/watch/[id]/page.tsx | 2 +- 6 files changed, 44 insertions(+), 45 deletions(-) diff --git a/app/api/aniwatch.ts b/app/api/aniwatch.ts index f71fbbd4..45a8d3f7 100644 --- a/app/api/aniwatch.ts +++ b/app/api/aniwatch.ts @@ -7,7 +7,7 @@ import Axios from "axios"; import axiosRetry from "axios-retry"; import { cache } from "react"; -const BASE_URL = process.env.NEXT_PUBLIC_ANIWATCH_API_URL; +const BASE_URL = `${process.env.NEXT_PUBLIC_ANIWATCH_API_URL}/api/v2/hianime`; // HANDLES SERVER ERRORS, most of time when server was not running due to be using the Free Tier axiosRetry(Axios, { @@ -28,7 +28,7 @@ export default { async ({ query, page }: { query: string; page?: number }) => { try { const { data } = await Axios({ - url: `${BASE_URL}/anime/search?q=${query}${page ? `&page=${page}` : ""}`, + url: `${BASE_URL}/search?q=${query}${page ? `&page=${page}` : ""}`, }); return data as MediaInfoFetchedAnimeWatch; @@ -41,10 +41,10 @@ export default { ), // GET EPISODES, NO LINKS INCLUDED - getEpisodes: cache(async ({ episodeId }: { episodeId: string }) => { + getMediaEpisodes: cache(async ({ mediaId }: { mediaId: string }) => { try { const { data } = await Axios({ - url: `${BASE_URL}/anime/episodes/${episodeId}`, + url: `${BASE_URL}/anime/${mediaId}/episodes`, }); return data as EpisodesFetchedAnimeWatch; @@ -55,8 +55,7 @@ export default { } }), - // GET EPISODES, NO LINKS INCLUDED - episodesLinks: cache( + getEpisodeLink: cache( async ({ episodeId, server, @@ -68,7 +67,7 @@ export default { }) => { try { const { data } = await Axios({ - url: `${BASE_URL}/anime/episode-srcs?id=${episodeId}${server ? `&server=${server}` : ""}${category ? `&category=${category}` : ""}`, + url: `${BASE_URL}/episode/sources?animeEpisodeId=${episodeId}${server ? `&server=${server}` : ""}${category ? `&category=${category}` : ""}`, }); return data as EpisodeLinksAnimeWatch; diff --git a/app/components/NavigationButtons/index.tsx b/app/components/NavigationButtons/index.tsx index 16739ec5..2c61d079 100644 --- a/app/components/NavigationButtons/index.tsx +++ b/app/components/NavigationButtons/index.tsx @@ -64,7 +64,7 @@ export default function NavigationButtons({ break; case "aniwatch": - const aniwatchResponse = (await aniwatch.episodesLinks({ + const aniwatchResponse = (await aniwatch.getEpisodeLink({ episodeId: "one-piece-100?ep=2142", })) as EpisodeLinksAnimeWatch; diff --git a/app/lib/dataFetch/optimizedFetchAnimeOptions.ts b/app/lib/dataFetch/optimizedFetchAnimeOptions.ts index 1fd67235..58a82444 100644 --- a/app/lib/dataFetch/optimizedFetchAnimeOptions.ts +++ b/app/lib/dataFetch/optimizedFetchAnimeOptions.ts @@ -152,23 +152,23 @@ export async function optimizedFetchOnAniwatch({ (media) => media.id == idToMatch ); - const mediaEpisodesList = (await aniwatch.getEpisodes({ - episodeId: mediaFoundByID?.id || mediasWithSameTitle[0].id, + const mediaEpisodesList = (await aniwatch.getMediaEpisodes({ + mediaId: mediaFoundByID?.id || mediasWithSameTitle[0].id, })) as EpisodesFetchedAnimeWatch; return mediaEpisodesList?.episodes?.length == 0 ? null : { - episodesDub: + episodesDub: mediaFoundByID?.episodes?.dub || mediasWithSameTitle[0]?.episodes?.dub || 0, - episodesSub: + episodesSub: mediaFoundByID?.episodes?.sub || mediasWithSameTitle[0]?.episodes?.sub || 0, - episodes: mediaEpisodesList.episodes, - }; + episodes: mediaEpisodesList.episodes, + }; } return mediasWithSameTitle; diff --git a/app/media/[id]/components/AnimeEpisodesContainer/index.tsx b/app/media/[id]/components/AnimeEpisodesContainer/index.tsx index c89af64a..676305d2 100644 --- a/app/media/[id]/components/AnimeEpisodesContainer/index.tsx +++ b/app/media/[id]/components/AnimeEpisodesContainer/index.tsx @@ -248,10 +248,10 @@ export default function EpisodesContainer({ let mediaEpisodesList: | GogoanimeMediaEpisodes[] | { - episodesDub: number; - episodesSub: number; - episodes: EpisodesFetchedAnimeWatch["episodes"]; - }; + episodesDub: number; + episodesSub: number; + episodes: EpisodesFetchedAnimeWatch["episodes"]; + }; switch (newSourceChose) { case "crunchyroll": @@ -372,8 +372,8 @@ export default function EpisodesContainer({ const endOffset = itemOffset + rangeEpisodesPerPage; - const mediaEpisodes = (await aniwatch.getEpisodes({ - episodeId: id, + const mediaEpisodes = (await aniwatch.getMediaEpisodes({ + mediaId: id, })) as EpisodesFetchedAnimeWatch; setEpisodesList(mediaEpisodes.episodes); @@ -443,30 +443,30 @@ export default function EpisodesContainer({ {currEpisodesSource == "aniwatch" && mediaResultsInfoArray.length > 1 && ( - - Wrong Episodes? Select bellow! - - - - )} + Wrong Episodes? Select bellow! + + + + )} diff --git a/app/watch/[id]/components/VideoPlayer/index.tsx b/app/watch/[id]/components/VideoPlayer/index.tsx index 9c618e75..c4b2a241 100644 --- a/app/watch/[id]/components/VideoPlayer/index.tsx +++ b/app/watch/[id]/components/VideoPlayer/index.tsx @@ -404,7 +404,7 @@ export default function VideoPlayer({ case "aniwatch": nextEpisodeId = (nextEpisodeInfo as EpisodeAnimeWatch).episodeId; - nextEpisode = await aniwatch.episodesLinks({ + nextEpisode = await aniwatch.getEpisodeLink({ episodeId: nextEpisodeId, category: searchParams?.get("dub") == "true" ? "dub" : "sub", }); diff --git a/app/watch/[id]/page.tsx b/app/watch/[id]/page.tsx index feed0a19..a26f5591 100644 --- a/app/watch/[id]/page.tsx +++ b/app/watch/[id]/page.tsx @@ -186,7 +186,7 @@ export default async function WatchEpisode({ } // fetch episode data - episodeDataFetched = (await aniwatch.episodesLinks({ + episodeDataFetched = (await aniwatch.getEpisodeLink({ episodeId: searchParams.q, category: searchParams.dub == "true" ? "dub" : "sub", })) as EpisodeLinksAnimeWatch; From 61cc40ff938393c44039d073cb20c07198acce86 Mon Sep 17 00:00:00 2001 From: Erick Lima Date: Sat, 5 Oct 2024 20:37:59 -0300 Subject: [PATCH 2/2] fix: Aniwatch Fetch Functions Added `.data` property access to Axios response in aniwatch API methods --- app/api/aniwatch.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/api/aniwatch.ts b/app/api/aniwatch.ts index 45a8d3f7..d92e073e 100644 --- a/app/api/aniwatch.ts +++ b/app/api/aniwatch.ts @@ -30,8 +30,8 @@ export default { const { data } = await Axios({ url: `${BASE_URL}/search?q=${query}${page ? `&page=${page}` : ""}`, }); - - return data as MediaInfoFetchedAnimeWatch; + + return data.data as MediaInfoFetchedAnimeWatch; } catch (error) { console.log((error as Error).message); @@ -47,7 +47,7 @@ export default { url: `${BASE_URL}/anime/${mediaId}/episodes`, }); - return data as EpisodesFetchedAnimeWatch; + return data.data as EpisodesFetchedAnimeWatch; } catch (error) { console.log((error as Error).message); @@ -70,7 +70,7 @@ export default { url: `${BASE_URL}/episode/sources?animeEpisodeId=${episodeId}${server ? `&server=${server}` : ""}${category ? `&category=${category}` : ""}`, }); - return data as EpisodeLinksAnimeWatch; + return data.data as EpisodeLinksAnimeWatch; } catch (error) { console.log((error as Error).message);