File size: 5,667 Bytes
01d9265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import aiohttp
import asyncio

from RTN import parse

from comet.utils.general import is_video
from comet.utils.logger import logger


class Premiumize:
    def __init__(self, session: aiohttp.ClientSession, debrid_api_key: str):
        self.session = session
        self.proxy = None

        self.api_url = "https://premiumize.me/api"
        self.debrid_api_key = debrid_api_key

    async def check_premium(self):
        try:
            check_premium = await self.session.get(
                f"{self.api_url}/account/info?apikey={self.debrid_api_key}"
            )
            check_premium = await check_premium.text()
            if (
                '"status":"success"' in check_premium
                and '"premium_until":null' not in check_premium
            ):
                return True
        except Exception as e:
            logger.warning(
                f"Exception while checking premium status on Premiumize: {e}"
            )

        return False

    async def get_instant(self, chunk: list):
        try:
            response = await self.session.get(
                f"{self.api_url}/cache/check?apikey={self.debrid_api_key}&items[]={'&items[]='.join(chunk)}"
            )

            response = await response.json()
            response["hashes"] = chunk

            return response
        except Exception as e:
            logger.warning(
                f"Exception while checking hash instant availability on Premiumize: {e}"
            )

    async def get_files(
        self, torrent_hashes: list, type: str, season: str, episode: str, kitsu: bool
    ):
        chunk_size = 100
        chunks = [
            torrent_hashes[i : i + chunk_size]
            for i in range(0, len(torrent_hashes), chunk_size)
        ]

        tasks = []
        for chunk in chunks:
            tasks.append(self.get_instant(chunk))

        responses = await asyncio.gather(*tasks)

        availability = []
        for response in responses:
            if not response:
                continue

            availability.append(response)

        files = {}

        if type == "series":
            for result in availability:
                if result["status"] != "success":
                    continue

                responses = result["response"]
                filenames = result["filename"]
                filesizes = result["filesize"]
                hashes = result["hashes"]
                for index, response in enumerate(responses):
                    if not response:
                        continue

                    if not filesizes[index]:
                        continue

                    filename = filenames[index]

                    if "sample" in filename:
                        continue

                    filename_parsed = parse(filename)
                    if episode not in filename_parsed.episodes:
                        continue

                    if kitsu:
                        if filename_parsed.seasons:
                            continue
                    else:
                        if season not in filename_parsed.seasons:
                            continue

                    files[hashes[index]] = {
                        "index": f"{season}|{episode}",
                        "title": filename,
                        "size": int(filesizes[index]),
                    }
        else:
            for result in availability:
                if result["status"] != "success":
                    continue

                responses = result["response"]
                filenames = result["filename"]
                filesizes = result["filesize"]
                hashes = result["hashes"]
                for index, response in enumerate(responses):
                    if response is False:
                        continue

                    if not filesizes[index]:
                        continue

                    filename = filenames[index]

                    if "sample" in filename:
                        continue

                    files[hashes[index]] = {
                        "index": 0,
                        "title": filename,
                        "size": int(filesizes[index]),
                    }

        return files

    async def generate_download_link(self, hash: str, index: str):
        try:
            add_magnet = await self.session.post(
                f"{self.api_url}/transfer/directdl?apikey={self.debrid_api_key}&src=magnet:?xt=urn:btih:{hash}",
            )
            add_magnet = await add_magnet.json()

            season = None
            if "|" in index:
                index = index.split("|")
                season = int(index[0])
                episode = int(index[1])

            content = add_magnet["content"]
            for file in content:
                filename = file["path"]
                if "/" in filename:
                    filename = filename.split("/")[1]

                if not is_video(filename):
                    content.remove(file)
                    continue

                if season is not None:
                    filename_parsed = parse(filename)
                    if (
                        season in filename_parsed.seasons
                        and episode in filename_parsed.episodes
                    ):
                        return file["link"]

            max_size_item = max(content, key=lambda x: x["size"])
            return max_size_item["link"]
        except Exception as e:
            logger.warning(
                f"Exception while getting download link from Premiumize for {hash}|{index}: {e}"
            )