Skip to content

Commit

Permalink
Add Proxy Support to Real Debrid (#418)
Browse files Browse the repository at this point in the history
* Add Proxy Support to Real Debrid

* Update realdebrid.py
  • Loading branch information
DrMxrcy authored Jun 17, 2024
1 parent c5412ab commit 6e7512d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
9 changes: 8 additions & 1 deletion backend/controllers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,16 @@ async def health(request: Request):
async def get_rd_user():
api_key = settings_manager.settings.downloaders.real_debrid.api_key
headers = {"Authorization": f"Bearer {api_key}"}

proxy = settings_manager.settings.downloaders.real_debrid.proxy_url if settings_manager.settings.downloaders.real_debrid.proxy_enabled else None

response = requests.get(
"https://api.real-debrid.com/rest/1.0/user", headers=headers, timeout=10
"https://api.real-debrid.com/rest/1.0/user",
headers=headers,
proxies=proxy if proxy else None,
timeout=10
)

if response.status_code != 200:
return {"success": False, "message": response.json()}

Expand Down
18 changes: 14 additions & 4 deletions backend/program/downloaders/realdebrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, hash_cache):
self.settings = settings_manager.settings.downloaders.real_debrid
self.download_settings = settings_manager.settings.downloaders
self.auth_headers = {"Authorization": f"Bearer {self.settings.api_key}"}
self.proxy = self.settings.proxy_url if self.settings.proxy_enabled else None
self.initialized = self.validate()
if not self.initialized:
return
Expand All @@ -55,8 +56,11 @@ def validate(self) -> bool:
if not isinstance(self.download_settings.episode_filesize_max, int) or self.download_settings.episode_filesize_max < -1:
logger.error("Real-Debrid episode filesize max is not set or invalid.")
return False
if self.settings.proxy_enabled and not self.settings.proxy_url:
logger.error("Proxy is enabled but no proxy URL is provided.")
return False
try:
response = ping(f"{RD_BASE_URL}/user", additional_headers=self.auth_headers)
response = ping(f"{RD_BASE_URL}/user", additional_headers=self.auth_headers, proxies=self.proxy)
if response.ok:
user_info = response.json()
return user_info.get("premium", 0) > 0
Expand All @@ -66,6 +70,7 @@ def validate(self) -> bool:
logger.exception(f"Failed to validate Real-Debrid settings: {e}")
return False


def run(self, item: MediaItem) -> Generator[MediaItem, None, None]:
"""Download media item from real-debrid.com"""
if (item.file and item.folder):
Expand Down Expand Up @@ -137,7 +142,7 @@ def _chunked(lst: List, n: int) -> Generator[List, None, None]:
for stream_chunk in _chunked(filtered_streams, 5):
streams = "/".join(stream_chunk)
try:
response = get(f"{RD_BASE_URL}/torrents/instantAvailability/{streams}/", additional_headers=self.auth_headers, response_type=dict)
response = get(f"{RD_BASE_URL}/torrents/instantAvailability/{streams}/", additional_headers=self.auth_headers, proxies=self.proxy, response_type=dict)
if response.is_ok and self._evaluate_stream_response(response.data, processed_stream_hashes, item):
return True
except Exception as e:
Expand Down Expand Up @@ -522,6 +527,7 @@ def add_magnet(self, item: MediaItem) -> str:
f"{RD_BASE_URL}/torrents/addMagnet",
{"magnet": f"magnet:?xt=urn:btih:{hash}&dn=&tr="},
additional_headers=self.auth_headers,
proxies=self.proxy
)
if response.is_ok:
return response.data.id
Expand All @@ -539,7 +545,8 @@ def get_torrent_info(self, request_id: str) -> dict:
try:
response = get(
f"{RD_BASE_URL}/torrents/info/{request_id}",
additional_headers=self.auth_headers
additional_headers=self.auth_headers,
proxies=self.proxy
)
if response.is_ok:
return response.data
Expand All @@ -562,18 +569,21 @@ def select_files(self, request_id: str, item: MediaItem) -> bool:
f"{RD_BASE_URL}/torrents/selectFiles/{request_id}",
{"files": ",".join(files.keys())},
additional_headers=self.auth_headers,
proxies=self.proxy
)
return response.is_ok
except Exception as e:
logger.error(f"Error selecting files for {item.log_string}: {e}")
return False


def get_torrents(self, limit: int) -> dict[str, SimpleNamespace]:
"""Get torrents from real-debrid.com"""
try:
response = get(
f"{RD_BASE_URL}/torrents?limit={str(limit)}",
additional_headers=self.auth_headers
additional_headers=self.auth_headers,
proxies=self.proxy
)
if response.is_ok and response.data:
# Example response.data:
Expand Down
2 changes: 2 additions & 0 deletions backend/program/settings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def __setattr__(self, name, value):
class DebridModel(Observable):
enabled: bool = False
api_key: str = ""
proxy_enabled: bool = False
proxy_url: str = ""


class TorboxModel(Observable):
Expand Down

0 comments on commit 6e7512d

Please sign in to comment.