aboutsummaryrefslogtreecommitdiff
path: root/lib/consumet
diff options
context:
space:
mode:
authorFactiven <[email protected]>2023-10-22 19:43:17 +0700
committerGitHub <[email protected]>2023-10-22 19:43:17 +0700
commitf801f8f422954b884a6541321dba0669ee9d6173 (patch)
treee0d5e106b99e9b4e0a4c4bf7bb0464617db85b8d /lib/consumet
parentBump @babel/traverse from 7.22.8 to 7.23.2 (#90) (diff)
downloadmoopa-4.2.0.tar.xz
moopa-4.2.0.zip
Update v4.2.0 (#93)v4.2.0
Diffstat (limited to 'lib/consumet')
-rw-r--r--lib/consumet/manga/getChapters.js80
-rw-r--r--lib/consumet/manga/getPage.js49
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 };
+ }
+}