File size: 3,841 Bytes
dcb132a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import flask
import requests
import src.functions.config

trailerBP = flask.Blueprint("trailer", __name__)


@trailerBP.route("/api/v1/trailer/<id>")
async def trailerFunction(id):
    a = flask.request.args.get("a")  # AUTH
    t = flask.request.args.get("t")  # TYPE
    api = flask.request.args.get("api")  # API
    config = src.functions.config.readConfig()

    if (
        any(a == account["auth"] for account in config["account_list"])
        or config.get("auth") == False
    ):
        if api == "tmdb":
            trailers = requests.get(
                "https://api.themoviedb.org/3/%s/%s/videos?api_key=%s&language=%s"
                % (t, id, config.get("tmdb_api_key"), config.get("language", "en"))
            ).json()
            if trailers:
                if len(trailers.get("results", [])) > 0:
                    trailer = next(
                        (
                            i
                            for i in trailers["results"]
                            if i["official"] == True
                            and i["type"] == "Trailer"
                            and i["site"] == "YouTube"
                        ),
                        next(
                            (
                                i
                                for i in trailers["results"]
                                if i["type"] == "Trailer" and i["site"] == "YouTube"
                            ),
                            trailers["results"][0],
                        ),
                    )
                    return (
                        {
                            "code": 200,
                            "content": trailer,
                            "message": "Trailer found successfully.",
                            "success": True,
                        },
                        200,
                    )
            return (
                {
                    "code": 404,
                    "content": None,
                    "message": "Trailer could not be found.",
                    "success": False,
                },
                404,
            )
        elif api == "anilist":
            query = """

                query ($id: Int) {

                    Media(id: $id, type: ANIME) {

                        trailer {

                            id

                            site

                        }

                    }

                }

            """
            variables = {"id": id}
            response = requests.post(
                "https://graphql.anilist.co",
                json={"query": query, "variables": variables},
            ).json()
            if response != None:
                if response.get("data", {}).get("Media", {}).get("trailer"):
                    trailer = response["data"]["Media"]["trailer"]
                    if trailer.get("site") == "youtube":
                        trailer = {
                            "type": "trailer",
                            "site": "YouTube",
                            "key": trailer.get("id"),
                        }
                        return (
                            {
                                "code": 200,
                                "content": trailer,
                                "message": "Trailer found successfully.",
                                "success": True,
                            },
                            200,
                        )
            return (
                {
                    "code": 404,
                    "content": None,
                    "message": "Trailer could not be found.",
                    "success": False,
                },
                404,
            )