Skip to content
This repository has been archived by the owner on Jul 14, 2024. It is now read-only.

Commit

Permalink
feat: Use Anify for manga
Browse files Browse the repository at this point in the history
  • Loading branch information
Eltik committed Oct 21, 2023
1 parent d3a579f commit b0a0893
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 40 deletions.
12 changes: 6 additions & 6 deletions lib/anify/page.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
128 changes: 97 additions & 31 deletions lib/anilist/aniAdvanceSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 12 additions & 2 deletions pages/en/manga/[id].js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -120,7 +124,7 @@ export async function getServerSideProps(context) {
}
`,
variables: {
id: parseInt(id),
id: parseInt(aniListId),
},
}),
});
Expand All @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion pages/en/manga/read/[...params].js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b0a0893

Please sign in to comment.