-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
50 lines (39 loc) · 1.62 KB
/
base.py
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
import asyncio
from hashlib import md5
from aiohttp import ClientSession
from megadebrid.parsers.configparser import MegaConfigParser
class MegaDebrid:
DOMAIN = "www.mega-debrid.eu"
def __init__(self, *args, **kwargs) -> None:
self.config = MegaConfigParser(config_path=kwargs.pop("config", None))
ajax_config = (
self.config.get_ajax_info() if kwargs.pop("is_ajax", False) else {}
)
headers = self.set_headers(ajax_config["USER-AGENT"]) if ajax_config else {}
cookies = ajax_config["COOKIES"] if ajax_config else {}
self.session = ClientSession(headers=headers, cookies=cookies)
async def __aenter__(self):
# Called when enter in 'async with MegaDebrid() as megadebrid:'
return self
async def __aexit__(self, *err):
# Called when leave 'async with MegaDebrid() as megadebrid:'
await self.session.close()
self.session = None
@property
def base_url(self) -> str:
return f"https://{ self.DOMAIN }"
def set_headers(self, user_agent) -> dict:
"""
Return the required headers to make the request.
Important: 'User-Agent' must be the same as the one that generated the cookies.
"""
return {
"User-Agent": user_agent,
"Accept": "application/json, text/javascript, */*; q=0.01",
"Referer": "https://www.mega-debrid.eu/index.php",
"X-Requested-With": "XMLHttpRequest",
}
@staticmethod
def hash_passwd(password):
"""Password is md5 encoded"""
return md5(password.encode()).hexdigest() if password else ""