Skip to content

Commit

Permalink
Refactor AllDebrid class to handle empty environment variables more e…
Browse files Browse the repository at this point in the history
…ffectively
  • Loading branch information
LimeDrive committed Sep 26, 2024
1 parent 8d274ee commit 7346d3e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
19 changes: 5 additions & 14 deletions stream_fusion/utils/debrid/alldebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ def get_headers(self):
"This may lead to account ban.")
logger.warning("Please enable proxied link in the settings.")
raise HTTPException(status_code=500, detail="Proxied link is disabled.")
if settings.ad_use_proxy and not settings.proxy_url:
logger.warning("Proxified AllDebrid stream is enabled, but playback proxy is disabled. "
"This may not work if your on server IP.")
logger.warning("Please enable playback proxy in the settings.")
raise HTTPException(status_code=500, detail="Playback proxy is disabled.")
if settings.ad_token:
return {"Authorization": f"Bearer {settings.ad_token}"}
else:
Expand All @@ -38,27 +33,23 @@ def get_headers(self):
else:
return {"Authorization": f"Bearer {self.config["ADToken"]}"}

def _use_proxy(self):
if settings.ad_use_proxy and settings.proxy_url:
return settings.proxy_url

def add_magnet(self, magnet, ip=None):
url = f"{self.base_url}magnet/upload?agent={self.agent}"
data = {"magnets[]": magnet}
return self.json_response(url, method='post', headers=self.get_headers(), data=data, proxies=self._use_proxy())
return self.json_response(url, method='post', headers=self.get_headers(), data=data)

def add_torrent(self, torrent_file, ip=None):
url = f"{self.base_url}magnet/upload/file?agent={self.agent}"
files = {"files[]": (str(uuid.uuid4()) + ".torrent", torrent_file, 'application/x-bittorrent')}
return self.json_response(url, method='post', headers=self.get_headers(), files=files, proxies=self._use_proxy())
return self.json_response(url, method='post', headers=self.get_headers(), files=files)

def check_magnet_status(self, id, ip=None):
url = f"{self.base_url}magnet/status?agent={self.agent}&id={id}"
return self.json_response(url, method='get', headers=self.get_headers(), proxies=self._use_proxy())
return self.json_response(url, method='get', headers=self.get_headers())

def unrestrict_link(self, link, ip=None):
url = f"{self.base_url}link/unlock?agent={self.agent}&link={link}"
return self.json_response(url, method='get', headers=self.get_headers(), proxies=self._use_proxy())
return self.json_response(url, method='get', headers=self.get_headers())

def get_stream_link(self, query, config, ip=None):
magnet = query['magnet']
Expand Down Expand Up @@ -124,7 +115,7 @@ def get_availability_bulk(self, hashes_or_magnets, ip=None):

url = f"{self.base_url}magnet/instant?agent={self.agent}"
data = {"magnets[]": hashes_or_magnets}
return self.json_response(url, method='post', headers=self.get_headers(), data=data, proxies=self._use_proxy())
return self.json_response(url, method='post', headers=self.get_headers(), data=data)

def __add_magnet_or_torrent(self, magnet, torrent_download=None, ip=None):
torrent_id = ""
Expand Down
32 changes: 18 additions & 14 deletions stream_fusion/utils/debrid/base_debrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class BaseDebrid:
def __init__(self, config):
self.config = config
self.logger = logger
self.__session = requests.Session()
self.__session = self._create_session()

# Limiteurs de débit
self.global_limit = 250
Expand All @@ -22,6 +22,16 @@ def __init__(self, config):
self.global_requests = deque()
self.torrent_requests = deque()

def _create_session(self):
session = requests.Session()
if settings.proxy_url:
self.logger.info(f"Using proxy: {settings.proxy_url}")
session.proxies = {
'http': settings.proxy_url,
'https': settings.proxy_url
}
return session

def _rate_limit(self, requests_queue, limit, period):
current_time = time.time()

Expand All @@ -41,28 +51,22 @@ def _global_rate_limit(self):
def _torrent_rate_limit(self):
self._rate_limit(self.torrent_requests, self.torrent_limit, self.torrent_period)

def json_response(self, url, method='get', data=None, headers=None, files=None, proxies=None):
def json_response(self, url, method='get', data=None, headers=None, files=None):
self._global_rate_limit()

if 'torrents' in url:
self._torrent_rate_limit()

if proxies and not settings.proxy_url:
self.logger.warning("Proxies are enabled, but no proxy URL is provided. "
"Please provide a proxy URL in the settings.")
raise ValueError("Proxy URL is not provided.")


max_attempts = 5
for attempt in range(max_attempts):
try:
if method == 'get':
response = self.__session.get(url, headers=headers, proxies=proxies)
response = self.__session.get(url, headers=headers)
elif method == 'post':
response = self.__session.post(url, data=data, headers=headers, files=files, proxies=proxies)
response = self.__session.post(url, data=data, headers=headers, files=files)
elif method == 'put':
response = self.__session.put(url, data=data, headers=headers, proxies=proxies)
response = self.__session.put(url, data=data, headers=headers)
elif method == 'delete':
response = self.__session.delete(url, headers=headers, proxies=proxies)
response = self.__session.delete(url, headers=headers)
else:
raise ValueError(f"Unsupported HTTP method: {method}")

Expand All @@ -82,7 +86,7 @@ def json_response(self, url, method='get', data=None, headers=None, files=None,

self.logger.error("Max attempts reached. Unable to complete request.")
return None

def wait_for_ready_status(self, check_status_func, timeout=30, interval=5):
self.logger.info(f"Waiting for {timeout} seconds to cache.")
start_time = time.time()
Expand Down

0 comments on commit 7346d3e

Please sign in to comment.