Skip to content

Commit

Permalink
Update functions to use Aniwatch v2.0
Browse files Browse the repository at this point in the history
Merge pull request #115 from ErickLimaS/adjust-anilist-api-v2
  • Loading branch information
ErickLimaS authored Oct 5, 2024
2 parents 9a009cb + 61cc40f commit 00b679d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 49 deletions.
21 changes: 10 additions & 11 deletions app/api/aniwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -28,10 +28,10 @@ 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;
return data.data as MediaInfoFetchedAnimeWatch;
} catch (error) {
console.log((error as Error).message);

Expand All @@ -41,22 +41,21 @@ 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;
return data.data as EpisodesFetchedAnimeWatch;
} catch (error) {
console.log((error as Error).message);

return null;
}
}),

// GET EPISODES, NO LINKS INCLUDED
episodesLinks: cache(
getEpisodeLink: cache(
async ({
episodeId,
server,
Expand All @@ -68,10 +67,10 @@ 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;
return data.data as EpisodeLinksAnimeWatch;
} catch (error) {
console.log((error as Error).message);

Expand Down
2 changes: 1 addition & 1 deletion app/components/NavigationButtons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions app/lib/dataFetch/optimizedFetchAnimeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 29 additions & 29 deletions app/media/[id]/components/AnimeEpisodesContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -443,30 +443,30 @@ export default function EpisodesContainer({
<AnimatePresence>
{currEpisodesSource == "aniwatch" &&
mediaResultsInfoArray.length > 1 && (
<motion.div
id={styles.select_media_container}
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
>
<small>Wrong Episodes? Select bellow!</small>

<select
onChange={(e) =>
handleRefetchMediaEpisodesFromSelectTag(e.target.value)
}
defaultValue={checkAnilistTitleMisspelling(
mediaInfo.title.romaji || mediaInfo.title.native
).toLowerCase()}
<motion.div
id={styles.select_media_container}
initial={{ height: 0, opacity: 0 }}
animate={{ height: "auto", opacity: 1 }}
exit={{ height: 0, opacity: 0 }}
>
{mediaResultsInfoArray?.map((media, key) => (
<option key={key} value={media.id.toLowerCase()}>
{media.name}
</option>
))}
</select>
</motion.div>
)}
<small>Wrong Episodes? Select bellow!</small>

<select
onChange={(e) =>
handleRefetchMediaEpisodesFromSelectTag(e.target.value)
}
defaultValue={checkAnilistTitleMisspelling(
mediaInfo.title.romaji || mediaInfo.title.native
).toLowerCase()}
>
{mediaResultsInfoArray?.map((media, key) => (
<option key={key} value={media.id.toLowerCase()}>
{media.name}
</option>
))}
</select>
</motion.div>
)}
</AnimatePresence>
</div>

Expand Down
2 changes: 1 addition & 1 deletion app/watch/[id]/components/VideoPlayer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
});
Expand Down
2 changes: 1 addition & 1 deletion app/watch/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 00b679d

Please sign in to comment.