From 2ec3a3b5ca737fe4735fb76293cbd740f6aec67c Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sat, 13 Jul 2024 19:40:33 -0600 Subject: [PATCH] Prevent spamming Real-Debrid when a torrent is completed, do more efficient periodic updates. Fetch more torrents in 1 go to prevent too many endpoint requests. --- .../TorrentClients/RealDebridTorrentClient.cs | 55 ++++++++++--------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs index f5955ba..b13e673 100644 --- a/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs @@ -92,12 +92,12 @@ public class RealDebridTorrentClient(ILogger logger, IH public async Task> GetTorrents() { - var page = 0; + var offset = 0; var results = new List(); while (true) { - var pagedResults = await GetClient().Torrents.GetAsync(page, 100); + var pagedResults = await GetClient().Torrents.GetAsync(offset, 5000); results.AddRange(pagedResults); @@ -106,7 +106,7 @@ public class RealDebridTorrentClient(ILogger logger, IH break; } - page += 100; + offset += 5000; } return results.Select(Map).ToList(); @@ -287,42 +287,45 @@ public class RealDebridTorrentClient(ILogger logger, IH return torrent; } - var rdTorrent = await GetInfo(torrent.RdId) ?? throw new($"Resource not found"); - - if (!String.IsNullOrWhiteSpace(rdTorrent.Filename)) + if (torrentClientTorrent == null || torrentClientTorrent.Ended == null) { - torrent.RdName = rdTorrent.Filename; + torrentClientTorrent = await GetInfo(torrent.RdId) ?? throw new($"Resource not found"); } - if (!String.IsNullOrWhiteSpace(rdTorrent.OriginalFilename)) + if (!String.IsNullOrWhiteSpace(torrentClientTorrent.Filename)) { - torrent.RdName = rdTorrent.OriginalFilename; + torrent.RdName = torrentClientTorrent.Filename; } - if (rdTorrent.Bytes > 0) + if (!String.IsNullOrWhiteSpace(torrentClientTorrent.OriginalFilename)) { - torrent.RdSize = rdTorrent.Bytes; - } - else if (rdTorrent.OriginalBytes > 0) - { - torrent.RdSize = rdTorrent.OriginalBytes; + torrent.RdName = torrentClientTorrent.OriginalFilename; } - if (rdTorrent.Files != null) + if (torrentClientTorrent.Bytes > 0) { - torrent.RdFiles = JsonConvert.SerializeObject(rdTorrent.Files); + torrent.RdSize = torrentClientTorrent.Bytes; + } + else if (torrentClientTorrent.OriginalBytes > 0) + { + torrent.RdSize = torrentClientTorrent.OriginalBytes; } - torrent.RdHost = rdTorrent.Host; - torrent.RdSplit = rdTorrent.Split; - torrent.RdProgress = rdTorrent.Progress; - torrent.RdAdded = rdTorrent.Added; - torrent.RdEnded = rdTorrent.Ended; - torrent.RdSpeed = rdTorrent.Speed; - torrent.RdSeeders = rdTorrent.Seeders; - torrent.RdStatusRaw = rdTorrent.Status; + if (torrentClientTorrent.Files != null && torrentClientTorrent.Files.Count > 0) + { + torrent.RdFiles = JsonConvert.SerializeObject(torrentClientTorrent.Files); + } - torrent.RdStatus = rdTorrent.Status switch + torrent.RdHost = torrentClientTorrent.Host; + torrent.RdSplit = torrentClientTorrent.Split; + torrent.RdProgress = torrentClientTorrent.Progress; + torrent.RdAdded = torrentClientTorrent.Added; + torrent.RdEnded = torrentClientTorrent.Ended; + torrent.RdSpeed = torrentClientTorrent.Speed; + torrent.RdSeeders = torrentClientTorrent.Seeders; + torrent.RdStatusRaw = torrentClientTorrent.Status; + + torrent.RdStatus = torrentClientTorrent.Status switch { "magnet_error" => TorrentStatus.Error, "magnet_conversion" => TorrentStatus.Processing,