From 75169e18629c9aaa3ad03d0eef5d902181ec234e Mon Sep 17 00:00:00 2001 From: Eltik <76538547+Eltik@users.noreply.github.com> Date: Mon, 9 Oct 2023 15:46:04 -0400 Subject: [PATCH 1/2] Small bug fixes --- .env.example | 2 +- pages/api/v2/episode/[id].js | 15 ++++----------- pages/api/v2/source/index.js | 2 +- pages/en/anime/watch/[...info].js | 2 +- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 1c8825b0..b3184ba5 100644 --- a/.env.example +++ b/.env.example @@ -19,4 +19,4 @@ DATABASE_URL="Your postgresql connection url" ## Redis # If you don't want to use redis, just comment the REDIS_URL (press ctrl + / on windows or cmd + / on mac) -REDIS_URL="rediss://username:password@host:port" \ No newline at end of file +REDIS_URL="redis://username:password@host:port" \ No newline at end of file diff --git a/pages/api/v2/episode/[id].js b/pages/api/v2/episode/[id].js index c85982d5..c1fac8bf 100644 --- a/pages/api/v2/episode/[id].js +++ b/pages/api/v2/episode/[id].js @@ -73,15 +73,8 @@ async function fetchAnify(id) { const filtered = data.filter( (item) => item.providerId !== "animepahe" && item.providerId !== "kass" ); - const modifiedData = filtered.map((provider) => { - if (provider.providerId === "gogoanime") { - const reversedEpisodes = [...provider.episodes].reverse(); - return { ...provider, episodes: reversedEpisodes }; - } - return provider; - }); - - return modifiedData; + + return filtered; } catch (error) { console.error("Error fetching and processing data:", error.message); return []; @@ -95,7 +88,7 @@ async function fetchCoverImage(id) { } const { data } = await axios.get( - `https://api.anify.tv/episode-covers/${id}?apikey=${API_KEY}` + `https://api.anify.tv/content-metadata/${id}?apikey=${API_KEY}` ); if (!data) { @@ -163,7 +156,7 @@ export default async function handler(req, res) { ) ); - const rawData = [...consumet, ...anify]; + const rawData = [...consumet, ...(anify[0]?.data ?? [])]; let data = rawData; diff --git a/pages/api/v2/source/index.js b/pages/api/v2/source/index.js index 74a63cb9..f15e47df 100644 --- a/pages/api/v2/source/index.js +++ b/pages/api/v2/source/index.js @@ -21,7 +21,7 @@ async function anifySource(providerId, watchId, episode, id, sub) { const { data } = await axios.get( `https://api.anify.tv/sources?providerId=${providerId}&watchId=${encodeURIComponent( watchId - )}&episode=${episode}&id=${id}&subType=${sub}&apikey=${API_KEY}` + )}&episodeNumber=${episode}&id=${id}&subType=${sub}&apikey=${API_KEY}` ); return data; } catch (error) { diff --git a/pages/en/anime/watch/[...info].js b/pages/en/anime/watch/[...info].js index 0b470a89..f918f869 100644 --- a/pages/en/anime/watch/[...info].js +++ b/pages/en/anime/watch/[...info].js @@ -30,7 +30,7 @@ export async function getServerSideProps(context) { } const proxy = process.env.PROXY_URI; - const disqus = process.env.DISQUS_SHORTNAME; + const disqus = process.env.DISQUS_SHORTNAME || null; const [aniId, provider] = query?.info; const watchId = query?.id; From b0a08935e2722dd885ff7d0687e07c05849dd5fb Mon Sep 17 00:00:00 2001 From: Eltik <76538547+Eltik@users.noreply.github.com> Date: Fri, 20 Oct 2023 20:14:13 -0400 Subject: [PATCH 2/2] feat: Use Anify for manga --- lib/anify/page.js | 12 +-- lib/anilist/aniAdvanceSearch.js | 128 ++++++++++++++++++++++------- pages/en/manga/[id].js | 14 +++- pages/en/manga/read/[...params].js | 11 ++- 4 files changed, 125 insertions(+), 40 deletions(-) diff --git a/lib/anify/page.js b/lib/anify/page.js index 65ed309f..da9e45b6 100644 --- a/lib/anify/page.js +++ b/lib/anify/page.js @@ -1,10 +1,10 @@ import { redis } from "../redis"; // Function to fetch new data -async function fetchData(id, providerId, chapterId, key) { +async function fetchData(id, providerId, chapter, key) { try { const res = await fetch( - `https://api.anify.tv/pages?id=${id}&providerId=${providerId}&readId=${chapterId}&apikey=${key}` + `https://api.anify.tv/pages?id=${id}&providerId=${providerId}&readId=${chapter.id}&chapterNumber=${chapter.number}&apikey=${key}` ); const data = await res.json(); return data; @@ -17,21 +17,21 @@ async function fetchData(id, providerId, chapterId, key) { export default async function getAnifyPage( mediaId, providerId, - chapterId, + chapter, key ) { try { let cached; if (redis) { - cached = await redis.get(chapterId); + cached = await redis.get(chapter.id); } if (cached) { return JSON.parse(cached); } else { - const data = await fetchData(mediaId, providerId, chapterId, key); + const data = await fetchData(mediaId, providerId, chapter, key); if (!data.error) { if (redis) { - await redis.set(chapterId, JSON.stringify(data), "EX", 60 * 10); + await redis.set(chapter.id, JSON.stringify(data), "EX", 60 * 10); } return data; } else { diff --git a/lib/anilist/aniAdvanceSearch.js b/lib/anilist/aniAdvanceSearch.js index 02a5c53e..f4ffcc99 100644 --- a/lib/anilist/aniAdvanceSearch.js +++ b/lib/anilist/aniAdvanceSearch.js @@ -23,37 +23,103 @@ export async function aniAdvanceSearch({ return result; }, {}); - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: advanceSearchQuery, - variables: { - ...(search && { - search: search, - ...(!sort && { sort: "SEARCH_MATCH" }), - }), - ...(type && { type: type }), - ...(seasonYear && { seasonYear: seasonYear }), - ...(season && { - season: season, - ...(!seasonYear && { seasonYear: new Date().getFullYear() }), - }), - ...(categorizedGenres && { ...categorizedGenres }), - ...(format && { format: format }), - // ...(genres && { genres: genres }), - // ...(tags && { tags: tags }), - ...(perPage && { perPage: perPage }), - ...(sort && { sort: sort }), + if (type === "MANGA") { + const response = await fetch("https://api.anify.tv/search-advanced", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + type: "manga", + genres: categorizedGenres, + ...(search && { query: search }), ...(page && { page: page }), + ...(perPage && { perPage: perPage }), + ...(format && { format: format }), + ...(seasonYear && { year: seasonYear }), + ...(type && { type: type }) + }), + }); + + const data = await response.json(); + return { + pageInfo: { + hasNextPage: data.length >= (perPage ?? 20), + currentPage: page, + lastPage: Math.ceil(data.length / (perPage ?? 20)), + perPage: perPage ?? 20, + total: data.length }, - }), - }); - - const datas = await response.json(); - // console.log(datas); - const data = datas.data.Page; - return data; + media: data.map((item) => ({ + averageScore: item.averageRating, + bannerImage: item.bannerImage, + chapters: item.totalChapters, + coverImage: { + color: item.color, + extraLarge: item.coverImage, + large: item.coverImage + }, + description: item.description, + duration: item.duration ?? null, + endDate: { + day: null, + month: null, + year: null + }, + format: item.format, + genres: item.genres, + id: item.id, + isAdult: false, + mediaListEntry: null, + nextAiringEpisode: null, + popularity: item.averagePopularity, + season: null, + seasonYear: item.year, + startDate: { + day: null, + month: null, + year: item.year + }, + status: item.status, + studios: { edges: [] }, + title: { userPreferred: item.title.english ?? item.title.romaji ?? item.title.native }, + type: item.type, + volumes: item.totalVolumes ?? null + })) + }; + } else { + const response = await fetch("https://graphql.anilist.co/", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + query: advanceSearchQuery, + variables: { + ...(search && { + search: search, + ...(!sort && { sort: "SEARCH_MATCH" }), + }), + ...(type && { type: type }), + ...(seasonYear && { seasonYear: seasonYear }), + ...(season && { + season: season, + ...(!seasonYear && { seasonYear: new Date().getFullYear() }), + }), + ...(categorizedGenres && { ...categorizedGenres }), + ...(format && { format: format }), + // ...(genres && { genres: genres }), + // ...(tags && { tags: tags }), + ...(perPage && { perPage: perPage }), + ...(sort && { sort: sort }), + ...(page && { page: page }), + }, + }), + }); + + const datas = await response.json(); + // console.log(datas); + const data = datas.data.Page; + return data; + } } diff --git a/pages/en/manga/[id].js b/pages/en/manga/[id].js index 6f255329..0c67ba60 100644 --- a/pages/en/manga/[id].js +++ b/pages/en/manga/[id].js @@ -90,6 +90,10 @@ export async function getServerSideProps(context) { const key = process.env.API_KEY; const data = await getAnifyInfo(id, key); + const chapters = await fetch("https://api.anify.tv/chapters/" + id).then((res) => res.json()); + + const aniListId = data.mappings.filter((i) => i.providerId === "anilist")[0]?.id || null; + let userManga = null; if (session) { @@ -120,7 +124,7 @@ export async function getServerSideProps(context) { } `, variables: { - id: parseInt(id), + id: parseInt(aniListId), }, }), }); @@ -131,12 +135,18 @@ export async function getServerSideProps(context) { } } - if (!data?.chapters) { + if (!Array.isArray(chapters)) { return { notFound: true, }; } + Object.assign(data, { + chapters: { + data: chapters, + }, + }) + return { props: { info: data, diff --git a/pages/en/manga/read/[...params].js b/pages/en/manga/read/[...params].js index a7769e29..0b7150cd 100644 --- a/pages/en/manga/read/[...params].js +++ b/pages/en/manga/read/[...params].js @@ -247,7 +247,16 @@ export async function getServerSideProps(context) { const session = await getServerSession(context.req, context.res, authOptions); - const data = await getAnifyPage(mediaId, providerId, chapterId, key); + const chapters = await fetch("https://api.anify.tv/chapters/" + mediaId).then((res) => res.json()); + + const currentChapter = chapters.find((x) => x.providerId === providerId)?.chapters.find((x) => x.id === chapterId); + if (!currentChapter) { + return { + notFound: true, + }; + } + + const data = await getAnifyPage(mediaId, providerId, currentChapter, key); if (data.error) { return {