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.
This commit is contained in:
mhdzumair
2024-12-01 17:52:40 +05:30
parent 211b22f114
commit 603a57027e
2 changed files with 19 additions and 4 deletions
+2
View File
@@ -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,
+17 -4
View File
@@ -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,
)