aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/source/index.js
blob: 9ec60826f0a34752d43f42cf95e9e8581334f13c (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 { rateLimiterRedis, redis } from "@/lib/redis";
import axios from "axios";

let CONSUMET_URI;
CONSUMET_URI = process.env.API_URI;
if (CONSUMET_URI.endsWith("/")) {
  CONSUMET_URI = CONSUMET_URI.slice(0, -1);
}
const API_KEY = process.env.API_KEY;

async function consumetSource(id) {
  try {
    const { data } = await axios.get(
      `${CONSUMET_URI}/meta/anilist/watch/${id}`
    );
    return data;
  } catch (error) {
    console.error(error);
    return null;
  }
}

async function anifySource(providerId, watchId, episode, id, sub) {
  try {
    const { data } = await axios.get(
      `https://api.anify.tv/sources?providerId=${providerId}&watchId=${encodeURIComponent(
        watchId
      )}&episodeNumber=${episode}&id=${id}&subType=${sub}&apikey=${API_KEY}`
    );
    return data;
  } catch (error) {
    return { error: error.message, status: error.response.status };
  }
}

export default async function handler(req, res) {
  if (req.method !== "POST") {
    return res.status(405).json({ message: "Method not allowed" });
  }

  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}`,
      });
    }
  }

  const { source, providerId, watchId, episode, id, sub = "sub" } = req.body;

  if (source === "anify") {
    const data = await anifySource(providerId, watchId, episode, id, sub);
    return res.status(200).json(data);
  }

  if (source === "consumet") {
    const data = await consumetSource(watchId);
    return res.status(200).json(data);
  }
}