Bulk asynchronous downloader for multiple file hosts
Python asynciobunkrcyberdropdownloaderdropboxfilestergofilegoogle-drivemediafiremegamega-nzonedrivepatreonpawchivepinterestpixeldraintransfer-ittwitchtwittervk
Update api.py
Jacoba91edf981435
diff --git a/cyberdrop_dl/managers/real_debrid/api.py b/cyberdrop_dl/managers/real_debrid/api.pyindex 0d8b35e22..f43ea869a 100755--- a/cyberdrop_dl/managers/real_debrid/api.py+++ b/cyberdrop_dl/managers/real_debrid/api.py@@ -1,22 +1,24 @@-from requests import Session-from pathlib import Path-from yarl import URL-from requests.exceptions import RequestException-from cyberdrop_dl.managers.real_debrid.errors import RealDebridError-from datetime import date, datetime, timedeltaimport timefrom contextlib import contextmanager-+from datetime import date, datetime, timedelta+from pathlib import Pathfrom typing import TYPE_CHECKING, Optional+from requests import Session+from requests.exceptions import RequestException+from yarl import URL++from cyberdrop_dl.managers.real_debrid.errors import RealDebridError+if TYPE_CHECKING:from requests import ResponseMAGNET_PREFIX = 'magnet:?xt=urn:btih:'DATE_FORMAT = "%Y-%m-%d %H:%M:%S"DATE_ISO_FORMAT = "%Y-%m-%dT%H:%M:%S%z"-DATE_JSON_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"-RATE_LIMIT = 250 # per minute+DATE_JSON_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ"+RATE_LIMIT = 250 # per minute+class RealDebridApi:"""@@ -35,7 +37,7 @@ class RealDebridApi:API_ENTRYPOINT = URL('https://api.real-debrid.com/rest/1.0')API_OAUTH_ENTRYPOINT = URL("https://api.real-debrid.com/oauth/v2/")- def __init__(self, api_token: Optional[str] = None, convert_special_types: bool= False):+ def __init__(self, api_token: Optional[str] = None, convert_special_types: bool = False):self._session = Session()self._last_request_time = 0self._convert_special_types = convert_special_types@@ -50,24 +52,24 @@ class RealDebridApi:self.hosts = Hosts(self)self.settings = Settings(self)self.update_token(api_token)-+def get(self, path: str, *, entrypoint: URL = API_ENTRYPOINT, **query_params):- response = self._session.get(url = entrypoint / path, params= query_params)+ response = self._session.get(url=entrypoint / path, params=query_params)return self.handle_response(response)def post(self, path: str, *, entrypoint: URL = API_ENTRYPOINT, **data):response = self._session.post(entrypoint / path, data=data)return self.handle_response(response)-- def put(self, path:str, filepath: Path, *, entrypoint: URL = API_ENTRYPOINT, **query_params):++ def put(self, path: str, filepath: Path, *, entrypoint: URL = API_ENTRYPOINT, **query_params):with open(filepath, 'rb') as file:- response = self._session.put(entrypoint/ path, data=file, params=query_params)+ response = self._session.put(entrypoint / path, data=file, params=query_params)return self.handle_response(response, path)def delete(self, path: str, *, entrypoint: URL = API_ENTRYPOINT):request = self._session.delete(entrypoint / path)return self.handle_response(request)-+def update_token(self, new_token: str) -> None:self._api_token = new_tokenself._session.headers.update({'Authorization': f"Bearer {self._api_token}"})@@ -78,22 +80,22 @@ class RealDebridApi:JSONResp: dict = response.json()return JSONRespexcept RequestException:- raise RealDebridError(response)+ raise RealDebridError(response)except AttributeError:return response.text-+@contextmanagerdef rate_limiter(self, buffer: float = 0.2):"""Context manager to rate limit API requestsBuffer is % of RATE_LIMIT"""- actual_rate_limit = RATE_LIMIT * ( 1 - buffer)+ actual_rate_limit = RATE_LIMIT * (1 - buffer)elapsed = time.time() - self._last_request_time- wait_time = max(0, (1 / actual_rate_limit ) - elapsed)- time.sleep(wait_time)- self._last_request_time = time.time()- yield-+ wait_time = max(0, (1 / actual_rate_limit) - elapsed)+ time.sleep(wait_time)+ self._last_request_time = time.time()+ yield+class OAuth:'''RealDebrid authentication via OAuth API'''@@ -102,9 +104,10 @@ class OAuth:self.api = apiself.grant_type = 'http://oauth.net/grant_type/device/1.0'- def get_devide_code(self, client_id: str, new_credentials:bool=False) -> dict:+ def get_devide_code(self, client_id: str, new_credentials: bool = False) -> dict:"""Get authentication data"""- JSONResp = self.api.get('device/code', entrypoint=self.api.API_OAUTH_ENTRYPOINT, client_id= client_id, new_credentials=new_credentials)+ JSONResp = self.api.get('device/code', entrypoint=self.api.API_OAUTH_ENTRYPOINT, client_id=client_id,+ new_credentials=new_credentials)if self.api._convert_special_types:JSONResp['expires_in'] = timedelta(seconds=JSONResp['expires_in'])JSONResp['verification_url'] = URL(JSONResp['verification_url'])@@ -112,23 +115,26 @@ class OAuth:def get_credentials(self, client_id: str, device_code: str):"""Verify authentication data and get credentials"""- return self.api.get('device/credentials', entrypoint=self.api.API_OAUTH_ENTRYPOINT, client_id = client_id, code=device_code)-+ return self.api.get('device/credentials', entrypoint=self.api.API_OAUTH_ENTRYPOINT, client_id=client_id,+ code=device_code)+def get_token(self, client_id: str, client_secret: str, device_code: str):"""Get token from credentials"""- JSONResp = self.api.post('token', entrypoint=self.api.API_OAUTH_ENTRYPOINT, client_id= client_id, client_secret=client_secret, code=device_code, grant_type = self.grant_type)+ JSONResp = self.api.post('token', entrypoint=self.api.API_OAUTH_ENTRYPOINT, client_id=client_id,+ client_secret=client_secret, code=device_code, grant_type=self.grant_type)if self.api._convert_special_types:JSONResp['expires_in'] = timedelta(seconds=JSONResp['expires_in'])self.api.update_token(JSONResp['access_token'])return JSONResp-+def get_new_token(self, client_id: str, client_secret: str, refresh_token: str):"""Get a new token"""- return self.get_token(client_id= client_id, client_secret=client_secret, device_code=refresh_token)-+ return self.get_token(client_id=client_id, client_secret=client_secret, device_code=refresh_token)+def refresh_token(self, client_id: str, client_secret: str, refresh_token: str):"""Calls `get_new_token`"""- return self.get_new_token(client_id= client_id, client_secret=client_secret, refresh_token=refresh_token)+ return self.get_new_token(client_id=client_id, client_secret=client_secret, refresh_token=refresh_token)+class System:def __init__(self, api: RealDebridApi):@@ -144,7 +150,7 @@ class System:if self.api._convert_special_types:return datetime.strptime(date_str, DATE_FORMAT)return date_str-+def iso_time(self):'''Get server time as ISO (with timezone). This request does not require authentication'''date_str = self.api.get('time/iso')@@ -152,6 +158,7 @@ class System:return datetime.strptime(date_str, DATE_ISO_FORMAT)return date_str+class User:def __init__(self, api: RealDebridApi):self.api = api@@ -163,31 +170,32 @@ class User:JSONResp['avatar'] = URL(JSONResp['avatar'])JSONResp['premium'] = timedelta(seconds=JSONResp['avatar'])JSONResp['avatar'] = URL(JSONResp['avatar'])- JSONResp['expiration'] = datetime.strptime(JSONResp['expiration'], "%Y-%m-%dT%H:%M:%S.%fZ")+ JSONResp['expiration'] = datetime.strptime(JSONResp['expiration'], "%Y-%m-%dT%H:%M:%S.%fZ")return JSONResp+class Unrestrict:def __init__(self, api: RealDebridApi):self.api = api- def check(self, link: URL, password: Optional[str] =None):+ def check(self, link: URL, password: Optional[str] = None):"""Check if a file is downloadable on the concerned hoster. This request does not require authentication"""return self.api.post('unrestrict/check', link=link, password=password)- def link(self, link: URL, password: Optional[str] = None, remote: bool=False) -> dict:+ def link(self, link: URL, password: Optional[str] = None, remote: bool = False) -> dict:"""Unrestrict a hoster link and get a new unrestricted link"""JSONResp = self.api.post('unrestrict/link', link=link, password=password, remote=remote)if self.api._convert_special_types:- JSONResp['download'] = URL(JSONResp['download'] )+ JSONResp['download'] = URL(JSONResp['download'])return JSONResp-+def folder(self, link: URL) -> list:"""Unrestrict a hoster folder link and get individual links, returns an empty array if no links found"""links = self.api.post('unrestrict/folder', link=link)if self.api._convert_special_types:links = [URL(link) for link in links]return links-+def container_file(self, filepath: Path) -> dict:"""Decrypt a container file (RSDF, CCF, CCF3, DLC)"""return self.api.put('unrestrict/containerFile', filepath=filepath)@@ -195,7 +203,8 @@ class Unrestrict:def container_link(self, link: URL) -> dict:"""Decrypt a container file from a link"""return self.api.post('unrestrict/containerLink', link=link)-++class Traffic:def __init__(self, api: RealDebridApi):self.api = api@@ -204,14 +213,15 @@ class Traffic:"""Get traffic informations for limited hosters (limits, current usage, extra packages)"""return self.api.get('traffic')- def details(self, start: date = None, end: date=None):+ def details(self, start: date = None, end: date = None):"""Get traffic details on each hoster used during a defined period"""- JSONResp = self.api.get('traffic/details', start=start, end=end)+ JSONResp = self.api.get('traffic/details', start=start, end=end)if self.api._convert_special_types:JSONResp = {datetime.strptime(key, "%Y-%m-%d").date(): value- for key, value in JSONResp.items()}+ for key, value in JSONResp.items()}return JSONResp-++class Streaming:def __init__(self, api: RealDebridApi):self.api = api@@ -223,12 +233,13 @@ class Streaming:def media_info(self, id: str):"""Get detailed media information for the given file id, {id} from /downloads or /unrestrict/link"""return self.api.get(f"/streaming/mediaInfos/{id}")-++class Downloads:def __init__(self, api: RealDebridApi):self.api = api- def get(self, offset: int =None, page: int=None, limit:int=None ):+ def get(self, offset: int = None, page: int = None, limit: int = None):"""Get user downloads list"""JSONResp = self.api.get('downloads', offset=offset, page=page, limit=limit)if self.api._convert_special_types:@@ -240,12 +251,14 @@ class Downloads:"""Delete a link from downloads list, returns 204 HTTP code"""return self.api.delete(f'/downloads/delete/{id}')+class Torrents:def __init__(self, api: RealDebridApi):self.api = api- self.POSIBLE_STATUS = ['magnet_error', 'magnet_conversion', 'waiting_files_selection', 'queued', 'downloading', 'downloaded', 'error', 'virus', 'compressing', 'uploading', 'dead']+ self.POSIBLE_STATUS = ['magnet_error', 'magnet_conversion', 'waiting_files_selection', 'queued', 'downloading',+ 'downloaded', 'error', 'virus', 'compressing', 'uploading', 'dead']- def get(self, offset:int=None, page:int=None, limit:int=None, filter: str=None ):+ def get(self, offset: int = None, page: int = None, limit: int = None, filter: str = None):"""Get user torrents list"""JSONResp: list[dict] = self.api.get('torrents', offset=offset, page=page, limit=limit, filter=filter)if self.api._convert_special_types:@@ -253,10 +266,10 @@ class Torrents:torrent['added'] = datetime.strptime(torrent['added'], DATE_JSON_FORMAT)if torrent.get('ended'):torrent['ended'] = datetime.strptime(torrent['ended'], DATE_JSON_FORMAT)-+return JSONResp- def info(self, id:str):+ def info(self, id: str):"""Get all informations on the asked torrent"""JSONResp: list[dict] = self.api.get(f'/torrents/info/{id}')if self.api._convert_special_types:@@ -264,7 +277,7 @@ class Torrents:torrent['added'] = datetime.strptime(torrent['added'], DATE_JSON_FORMAT)if torrent.get('ended'):torrent['ended'] = datetime.strptime(torrent['ended'], DATE_JSON_FORMAT)-+return JSONRespdef instant_availability(self, *hash: str):@@ -276,62 +289,64 @@ class Torrents:def active_count(self):"""Get currently active torrents number and the current maximum limit"""return self.api.get('torrents/activeCount')-+def available_hosts(self):"""Get available hosts to upload the torrent to"""return self.api.get('torrents/availableHosts')- def add_file(self, filepath:Path, host:Optional[str]=None):+ def add_file(self, filepath: Path, host: Optional[str] = None):"""Add a torrent file to download, return a 201 HTTP code"""return self.api.put('torrents/addTorrent', filepath=filepath, host=host)-- def add_magnet(self, magnet:str, host:Optional[str]=None):++ def add_magnet(self, magnet: str, host: Optional[str] = None):"""Add a magnet link to download, returns a 201 HTTP code"""if MAGNET_PREFIX not in magnet:magnet = f"{MAGNET_PREFIX}{magnet}"return self.api.post('torrents/addMagnet', magnet=magnet, host=host)-- def select_files(self, id:str, *files:str):++ def select_files(self, id: str, *files: str):"""Select files of a torrent to start it, returns 204 HTTP codefiles = Selected files IDs (comma separated) or 'all'"""return self.api.post(f'/torrents/selectFiles/{id}', files=','.join(files))-- def delete(self, id:str):++ def delete(self, id: str):"""Delete a torrent from torrents list, returns 204 HTTP code"""return self.api.delete(f'/torrents/delete/{id}')+class Hosts:- def __init__(self, api:RealDebridApi):+ def __init__(self, api: RealDebridApi):self.api = apidef get(self):"""Get supported hosts. This request does not require authentication"""- return self.api.get('hosts')-+ return self.api.get('hosts')+def status(self):"""Get status of supported hosters or not and their status on competitors"""- JSONResp: dict[dict] = self.api.get('hosts/status')+ JSONResp: dict[dict] = self.api.get('hosts/status')if self.api._convert_special_types:for host in JSONResp:host['check_time'] = datetime.strptime(host['check_time'], DATE_JSON_FORMAT)if host.get('competitor_status'):for competitor in host['competitor_status']:competitor['check_time'] = datetime.strptime(competitor['check_time'], DATE_JSON_FORMAT)-+return JSONRespdef regex(self) -> list:"""Get all supported links Regex, useful to find supported links inside a document. This request does not require authentication"""- return self.api.get('hosts/regex')+ return self.api.get('hosts/regex')def regex_folder(self) -> list:"""Get all supported folder Regex, useful to find supported links inside a document. This request does not require authentication"""- return self.api.get('hosts/regexFolder')+ return self.api.get('hosts/regexFolder')def domains(self):"""Get all hoster domains supported on the service. This request does not require authentication"""- return self.api.get('hosts/domains')+ return self.api.get('hosts/domains')+class Settings:def __init__(self, api: RealDebridApi):@@ -340,24 +355,23 @@ class Settings:def get(self):"""Get current user settings with possible values to update"""return self.api.get('settings')-- def update(self, setting_name: str, setting_value:str):++ def update(self, setting_name: str, setting_value: str):"""Update a user setting, returns 204 HTTP code"""return self.api.post('settings/update', setting_name=setting_name, setting_value=setting_value)-+def convert_points(self):"""Convert fidelity points, returns 204 HTTP code"""- return self.api.post('settings/convertPoints')+ return self.api.post('settings/convertPoints')def change_password(self):"""Send the verification email to change the password, returns 204 HTTP code"""- return self.api.post('settings/changePassword')+ return self.api.post('settings/changePassword')- def avatar_file(self, filepath:Path):+ def avatar_file(self, filepath: Path):"""Upload a new user avatar image, returns 204 HTTP code"""return self.api.put('settings/avatarFile', filepath=filepath)-+def avatar_delete(self):"""Reset user avatar image to default, returns 204 HTTP code"""return self.api.delete('settings/avatarDelete')-