diff options
| author | Factiven <[email protected]> | 2024-01-15 09:49:15 +0700 |
|---|---|---|
| committer | Factiven <[email protected]> | 2024-01-15 09:49:15 +0700 |
| commit | ff31a12ff6a757d08f35a09cdfcf452419716c71 (patch) | |
| tree | 05b59a53dc4899c7d17430279cb45ae6d7be0d32 /pages | |
| parent | Added seeking buttons and fixed vercel error (#113) (diff) | |
| download | moopa-beta.tar.xz moopa-beta.zip | |
Refactor episode fetching and data formattingbeta
Diffstat (limited to 'pages')
| -rw-r--r-- | pages/api/v2/episode/[id].tsx | 27 |
1 files changed, 14 insertions, 13 deletions
diff --git a/pages/api/v2/episode/[id].tsx b/pages/api/v2/episode/[id].tsx index ddf5635..6837038 100644 --- a/pages/api/v2/episode/[id].tsx +++ b/pages/api/v2/episode/[id].tsx @@ -44,9 +44,9 @@ function filterData(data: RawEpisodeData[], type: "sub" | "dub") { ...item, episodes: Object?.entries(item.episodes[type]).map( ([id, episode]) => ({ - ...episode, + ...episode }) - ), + ) }; } } @@ -61,23 +61,23 @@ async function fetchConsumet(id?: string | string[] | undefined) { try { const fetchData = async (dub?: any) => { const { data } = await axios.get<ConsumetInfo>( - `${CONSUMET_URI}/meta/anilist/info/${id}${dub ? "?dub=true" : ""}` + `${CONSUMET_URI}/meta/anilist/episodes/${id}${dub ? "?dub=true" : ""}` ); if (data?.message === "Anime not found" && data?.length < 1) { return []; } if (dub) { - if (!data?.episodes?.some((i) => i.id.includes("dub"))) return []; + if (!data?.some((i) => i.id.includes("dub"))) return []; } - const reformatted = data.episodes?.map((item) => ({ + const reformatted = data?.map((item) => ({ id: item.id, title: item?.title || null, img: item?.image || null, number: item?.number || null, createdAt: item?.airDate || null, - description: item?.description || null, + description: item?.description || null })); return reformatted; @@ -85,7 +85,7 @@ async function fetchConsumet(id?: string | string[] | undefined) { const [subData, dubData] = await Promise.all([ fetchData(), - fetchData(true), + fetchData(true) ]); if (subData.every((i) => i.id?.includes("dub"))) { @@ -104,9 +104,9 @@ async function fetchConsumet(id?: string | string[] | undefined) { providerId: "gogoanime", episodes: { sub: isAscending(subData) ? subData : subData.reverse(), - dub: isAscending(dubData) ? dubData : dubData.reverse(), - }, - }, + dub: isAscending(dubData) ? dubData : dubData.reverse() + } + } ]; return array; @@ -194,7 +194,7 @@ export default async function handler( error: `Too Many Requests, retry after ${getTimeFromMs( error.msBeforeNext )}`, - remaining: error.remainingPoints, + remaining: error.remainingPoints }); } @@ -255,7 +255,7 @@ export default async function handler( const [consumet, anify, cover] = await Promise.all([ fetchConsumet(id), fetchAnify(id), - fetchCoverImage(id, meta), + fetchCoverImage(id, meta) ]); let subDub = "sub"; @@ -276,7 +276,8 @@ export default async function handler( // !cover?.some((item: { img: null }) => item.img === null) && cover?.length > 0 ) { - if (redis) await redis.set(`meta:${id}`, JSON.stringify(cover)); + if (redis) + await redis.set(`meta:${id}`, JSON.stringify(cover), "EX", cacheTime); data = await appendMetaToEpisodes(filteredData, cover); } |