aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/info
diff options
context:
space:
mode:
Diffstat (limited to 'pages/api/v2/info')
-rw-r--r--pages/api/v2/info/[id].js47
-rw-r--r--pages/api/v2/info/index.js60
2 files changed, 60 insertions, 47 deletions
diff --git a/pages/api/v2/info/[id].js b/pages/api/v2/info/[id].js
deleted file mode 100644
index 243756c..0000000
--- a/pages/api/v2/info/[id].js
+++ /dev/null
@@ -1,47 +0,0 @@
-import axios from "axios";
-import { rateLimiterRedis, redis } from "@/lib/redis";
-
-const API_KEY = process.env.API_KEY;
-
-export async function fetchInfo(id) {
- try {
- const { data } = await axios.get(
- `https://api.anify.tv/info/${id}?apikey=${API_KEY}`
- );
- return data;
- } catch (error) {
- console.error("Error fetching data:", error);
- return null;
- }
-}
-
-export default async function handler(req, res) {
- const id = req.query.id;
- let cached;
- if (redis) {
- try {
- const ipAddress = req.socket.remoteAddress;
- await rateLimiterRedis.consume(ipAddress);
- } catch (error) {
- return res.status(429).json({
- error: `Too Many Requests, retry after ${error.msBeforeNext / 1000}`,
- });
- }
- cached = await redis.get(id);
- }
- if (cached) {
- // console.log("Using cached data");
- return res.status(200).json(JSON.parse(cached));
- } else {
- const data = await fetchInfo(id);
- if (data) {
- // console.log("Setting cache");
- if (redis) {
- await redis.set(id, JSON.stringify(data), "EX", 60 * 10);
- }
- return res.status(200).json(data);
- } else {
- return res.status(404).json({ message: "Schedule not found" });
- }
- }
-}
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 });
+ }
+}