File size: 6,820 Bytes
de1ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#    This file is part of the AutoAnime distribution.
#    Copyright (c) 2025 Kaif_00z
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3.
#
#    This program is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#    General Public License for more details.
#
# License can be found in <
# https://github.com/kaif-00z/AutoAnimeBot/blob/main/LICENSE > .

# if you are using this following code then don't forgot to give proper
# credit to t.me/kAiF_00z (github.com/kaif-00z)

import aiohttp
from AnilistPython import Anilist


class RawAnimeInfo:
    def __init__(self):
        self.anilist = Anilist()

    async def search(self, query: str):
        raw_data = ((await self.searcher(query)) or {}).get("data") or {}
        try:
            _raw_data = await self.search_anilist(raw_data.get("id"))
        except BaseException:
            _raw_data = {}
        if not raw_data:
            data = {}  # self.alt_anilist(query)
            return data
        data = {}
        data["anime_id"] = raw_data.get("id")
        data["english_title"] = raw_data.get("attributes", {}).get("titles", {}).get(
            "en"
        ) or raw_data.get("attributes", {}).get("titles", {}).get("en_jp")
        data["japanese_title"] = (
            raw_data.get("attributes", {}).get("titles", {}).get("ja_jp")
        )
        data["description"] = raw_data.get("attributes", {}).get("description")
        data["total_eps"] = raw_data.get("attributes", {}).get("episodeCount") or "N/A"
        data["poster_img"] = (
            raw_data.get("attributes", {}).get("posterImage", {}).get("original")
        )
        # anilist score will be better i guess
        # data["score"] = raw_data.get("attributes", {}).get("averageRating") or "N/A"
        data["type"] = raw_data.get("attributes", {}).get("showType") or "TV"
        data["runtime"] = raw_data.get("attributes", {}).get("episodeLength") or 24
        return {**(data if data else {}), **(_raw_data if _raw_data else {})}

    async def searcher(
        self,
        query: str,
    ):
        async with aiohttp.ClientSession() as client:
            try:
                data = await client.get(
                    f"https://kitsu.io/api/edge/anime?filter%5Btext%5D={query.replace(' ', '%20')}"
                )
                links = (await data.json())["data"]
                for index in range(len(links)):
                    res_data = await self.re_searcher(links[index]["links"]["self"])
                    if res_data["data"]["attributes"]["status"] == "tba":
                        continue
                    if "current" != res_data["data"]["attributes"]["status"]:
                        if (
                            res_data["data"]["attributes"]["endDate"]
                            or res_data["data"]["attributes"]["startDate"]
                        ):
                            if "2025" not in (
                                res_data["data"]["attributes"]["endDate"] or ""
                            ):
                                if all(
                                    year
                                    not in (
                                        res_data["data"]["attributes"]["startDate"]
                                        or ""
                                    )
                                    for year in ["2024", "2025"]
                                ):
                                    continue
                    return res_data
            except BaseException:
                raise ValueError("Kitsu: Search Link Not Found")

    async def re_searcher(self, link: str):
        if not link:
            raise ValueError("Kitsu: Link Not Found")
        async with aiohttp.ClientSession() as client:
            try:
                data = await client.get(link)
                return await data.json()
            except BaseException:
                raise ValueError("Kitsu: Link Not Found")

    async def search_anilist(self, kitsu_id):
        if not kitsu_id:
            raise ValueError("Kitsu: ID Not Found")
        async with aiohttp.ClientSession() as client:
            try:
                _data = {}
                res = await client.get(
                    f"https://kitsu.io/api/edge/anime/{kitsu_id}/mappings"
                )
                data = (await res.json())["data"]
                for maps in data:
                    if (
                        maps.get("attributes", {}).get("externalSite")
                        == "anilist/anime"
                    ):
                        _data["anilist_id"] = maps.get("attributes", {}).get(
                            "externalId"
                        )
                        _data["anilist_poster"] = (
                            f"https://img.anili.st/media/{_data['anilist_id']}"
                        )
                        __data = self.anilist_result(_data["anilist_id"])
                        return {**_data, **__data}
            except BaseException:
                raise ValueError("Kitsu: Mapping Failed")

    def anilist_result(self, anilist_id):
        try:
            data = self.anilist.get_anime_with_id(anilist_id)
            return {
                "genres": data.get("genres"),
                "next_airing_ep": data.get("next_airing_ep"),
                "season": data.get("season"),
                "starting_time": data.get("starting_time"),
                "ending_time": data.get("ending_time"),
                "score": data.get("average_score") or "N/A",
            }
        except BaseException:
            return {}

    def alt_anilist(self, anime_name):
        data = self.anilist.get_anime(anime_name)
        _id = self.anilist.get_anime_id(anime_name)
        return {
            "anilist_id": _id,
            "genres": data.get("genres"),
            "next_airing_ep": data.get("next_airing_ep") or {},
            "season": data.get("season"),
            "starting_time": data.get("starting_time"),
            "ending_time": data.get("ending_time"),
            "score": data.get("average_score") or "N/A",
            "english_title": data.get("name_english"),
            "japanese_title": data.get("name_romaji"),
            "description": data.get("desc"),
            "total_eps": data.get("airing_episodes") or "N/A",
            "poster_img": data.get("banner_image"),
            "type": data.get("airing_format") or "TV",
            "runtime": 24,
            "anilist_poster": f"https://img.anili.st/media/{_id}",
        }