diff options
Diffstat (limited to 'lib/consumet')
| -rw-r--r-- | lib/consumet/manga/getChapters.js | 80 | ||||
| -rw-r--r-- | lib/consumet/manga/getPage.js | 49 |
2 files changed, 129 insertions, 0 deletions
diff --git a/lib/consumet/manga/getChapters.js b/lib/consumet/manga/getChapters.js new file mode 100644 index 0000000..7a19bbc --- /dev/null +++ b/lib/consumet/manga/getChapters.js @@ -0,0 +1,80 @@ +let API_URL; +API_URL = process.env.API_URI; +// remove / from the end of the url if it exists +if (API_URL.endsWith("/")) { + API_URL = API_URL.slice(0, -1); +} + +async function fetchInfo(id) { + try { + const providers = [ + "mangadex", + "mangahere", + "mangakakalot", + // "mangapark", + // "mangapill", + "mangasee123", + ]; + let datas = []; + + async function promiseMe(provider) { + try { + const data = await fetch( + `${API_URL}/meta/anilist-manga/info/${id}?provider=${provider}` + ).then((res) => { + if (!res.ok) { + switch (res.status) { + case 404: { + return null; + } + } + } + return res.json(); + }); + if (data.chapters.length > 0) { + datas.push({ + providerId: provider, + chapters: data.chapters, + }); + } + } catch (error) { + console.error(`Error fetching data for provider '${provider}':`, error); + } + } + + await Promise.all(providers.map((provider) => promiseMe(provider))); + + return datas; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function getConsumetChapters(id, redis) { + try { + let cached; + let chapters; + + if (redis) { + cached = await redis.get(`chapter:${id}`); + } + + if (cached) { + chapters = JSON.parse(cached); + } else { + chapters = await fetchInfo(id); + } + + if (chapters?.length === 0) { + return null; + } + if (redis) { + await redis.set(`chapter:${id}`, JSON.stringify(chapters), "EX", 60 * 60); // 1 hour + } + + return chapters; + } catch (error) { + return { error }; + } +} diff --git a/lib/consumet/manga/getPage.js b/lib/consumet/manga/getPage.js new file mode 100644 index 0000000..832c1d7 --- /dev/null +++ b/lib/consumet/manga/getPage.js @@ -0,0 +1,49 @@ +let API_URL; +API_URL = process.env.API_URI; +// remove / from the end of the url if it exists +if (API_URL.endsWith("/")) { + API_URL = API_URL.slice(0, -1); +} + +// Function to fetch new data +async function fetchData(id, providerId, chapterId, key) { + try { + const res = await fetch( + `${API_URL}/meta/anilist-manga/read?chapterId=${chapterId}&provider=${providerId}` + ); + const data = await res.json(); + return data; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function getConsumetPages( + mediaId, + providerId, + chapterId, + key +) { + try { + // let cached; + // if (redis) { + // cached = await redis.get(chapterId); + // } + // if (cached) { + // return JSON.parse(cached); + // } else { + const data = await fetchData(mediaId, providerId, chapterId, key); + if (!data.error) { + // if (redis) { + // await redis.set(chapterId, JSON.stringify(data), "EX", 60 * 10); + // } + return data; + } else { + return { message: "Manga/Novel not found :(" }; + } + // } + } catch (error) { + return { error }; + } +} |