aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/info/index.js
blob: 4bebfaee922b5628e9469cba7dda28befaf89980 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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}`)
      .catch((err) => {
        return {
          data: null,
        };
      });

    if (!data) {
      return null;
    }

    const { data: Chapters } = await axios.get(
      `https://api.anify.tv/chapters/${data.id}`
    );

    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);
    if (redis) {
      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" });
    }

    if (redis)
      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 });
  }
}