From 603a57027e2b65fdfafbb7dc71d1cbdf41bef620 Mon Sep 17 00:00:00 2001 From: mhdzumair Date: Sun, 1 Dec 2024 17:52:40 +0530 Subject: [PATCH] Enhance torrent handling for RealDebrid integration This commit refines the cache update process to only consider downloaded torrents, improving efficiency by avoiding unnecessary cache checks. It also adds error handling for invalid magnet links and introduces a semaphore for deleting torrents to control concurrency, enhancing robustness and reliability in torrent management. --- streaming_providers/realdebrid/client.py | 2 ++ streaming_providers/realdebrid/utils.py | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/streaming_providers/realdebrid/client.py b/streaming_providers/realdebrid/client.py index 3e99523..93197a5 100644 --- a/streaming_providers/realdebrid/client.py +++ b/streaming_providers/realdebrid/client.py @@ -34,6 +34,8 @@ class RealDebrid(DebridClient): raise ProviderException( "Active torrents limit reached", "torrent_limit.mp4" ) + case 30: + raise ProviderException("Invalid magnet link", "transfer_error.mp4") async def _make_request( self, diff --git a/streaming_providers/realdebrid/utils.py b/streaming_providers/realdebrid/utils.py index 2884eb9..2107e7d 100644 --- a/streaming_providers/realdebrid/utils.py +++ b/streaming_providers/realdebrid/utils.py @@ -178,12 +178,14 @@ async def add_new_torrent(rd_client, magnet_link, info_hash): async def update_rd_cache_status( streams: list[TorrentStreams], user_data: UserData, user_ip: str, **kwargs ): - """Updates the cache status of streams based on RealDebrid's or Zilean's instant availability.""" + """Updates the cache status of streams based on user's downloaded torrents in RealDebrid.""" try: downloaded_hashes = set( await fetch_downloaded_info_hashes_from_rd(user_data, user_ip, **kwargs) ) + if not downloaded_hashes: + return for stream in streams: stream.cached = stream.id in downloaded_hashes @@ -199,8 +201,12 @@ async def fetch_downloaded_info_hashes_from_rd( async with RealDebrid( token=user_data.streaming_provider.token, user_ip=user_ip ) as rd_client: - available_torrents = await rd_client.get_user_torrent_list() - return [torrent["hash"] for torrent in available_torrents] + available_torrents = await rd_client.get_user_torrent_list(filter_="active") + return [ + torrent["hash"] + for torrent in available_torrents + if torrent["status"] == "downloaded" + ] except ProviderException: return [] @@ -212,8 +218,15 @@ async def delete_all_watchlist_rd(user_data: UserData, user_ip: str, **kwargs): token=user_data.streaming_provider.token, user_ip=user_ip ) as rd_client: torrents = await rd_client.get_user_torrent_list() + semaphore = asyncio.Semaphore(3) + + async def delete_torrent(torrent_id): + async with semaphore: + await rd_client.delete_torrent(torrent_id) + await asyncio.gather( - *[rd_client.delete_torrent(torrent["id"]) for torrent in torrents] + *[delete_torrent(torrent["id"]) for torrent in torrents], + return_exceptions=True, )