aboutsummaryrefslogtreecommitdiff
path: root/pages/api/v2/source/index.js
blob: 51ac5ec6cb96c2bea15856404334cc85402a760a (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
import axios from "axios";

const CONSUMET_URI = process.env.API_URI;
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
      )}&episode=${episode}&id=${id}&subType=${sub}&apikey=${API_KEY}`
    );
    return data;
  } catch (error) {
    return null;
  }
}

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

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