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 {