aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/info/index.js
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 /pages/api/v2/info/index.js
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 'pages/api/v2/info/index.js')
-rw-r--r--pages/api/v2/info/index.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/pages/api/v2/info/index.js b/pages/api/v2/info/index.js
new file mode 100644
index 0000000..95770bd
--- /dev/null
+++ b/pages/api/v2/info/index.js
@@ -0,0 +1,60 @@
+import { redis } from "@/lib/redis";
+import axios from "axios";
+
+const API_KEY = process.env.API_KEY;
+
+export async function fetchInfo(id) {
+ try {
+ // console.log(id);
+ const { data } = await axios
+ .get(`https://api.anify.tv/info/${id}?apikey=${API_KEY}`)
+ .catch((err) => {
+ return {
+ data: null,
+ };
+ });
+
+ if (!data) {
+ return null;
+ }
+
+ const { data: Chapters } = await axios.get(
+ `https://api.anify.tv/chapters/${data.id}?apikey=${API_KEY}`
+ );
+
+ if (!Chapters) {
+ return null;
+ }
+
+ return { id: data.id, chapters: Chapters };
+ } catch (error) {
+ console.error("Error fetching data:", error);
+ return null;
+ }
+}
+
+export default async function handler(req, res) {
+ //const [romaji, english, native] = req.query.title;
+ const { id } = req.query;
+ try {
+ let cached;
+ // const data = await fetchInfo(id);
+ cached = await redis.get(`manga:${id}`);
+
+ if (cached) {
+ return res.status(200).json(JSON.parse(cached));
+ }
+
+ const manga = await fetchInfo(id);
+
+ if (!manga) {
+ return res.status(404).json({ error: "Manga not found" });
+ }
+
+ await redis.set(`manga:${id}`, JSON.stringify(manga), "ex", 60 * 60 * 24);
+
+ res.status(200).json(manga);
+ } catch (error) {
+ res.status(500).json({ error: error.message });
+ }
+}