diff --git a/client/src/app/torrent-status.pipe.ts b/client/src/app/torrent-status.pipe.ts index a0b33d5..8e5b96d 100644 --- a/client/src/app/torrent-status.pipe.ts +++ b/client/src/app/torrent-status.pipe.ts @@ -103,7 +103,7 @@ export class TorrentStatusPipe implements PipeTransform { case RealDebridStatus.Error: return `Torrent error: ${torrent.rdStatusRaw}`; case RealDebridStatus.Finished: - return `Torrent finished, waiting to download`; + return `Torrent finished, waiting for download links`; case RealDebridStatus.Uploading: return `Torrent uploading`; default: diff --git a/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs b/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs index ec40d61..0ce3d6d 100644 --- a/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs +++ b/server/RdtClient.Service/Services/TorrentClients/RealDebridTorrentClient.cs @@ -16,6 +16,7 @@ namespace RdtClient.Service.Services.TorrentClients { private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; + private TimeSpan _offset; public RealDebridTorrentClient(ILogger logger, IHttpClientFactory httpClientFactory) { @@ -38,10 +39,14 @@ namespace RdtClient.Service.Services.TorrentClients var rdtNetClient = new RdNetClient(null, httpClient, 5); rdtNetClient.UseApiAuthentication(apiKey); + // Get the server time to fix up the timezones on results + var serverTime = rdtNetClient.Api.GetIsoTimeAsync().Result; + _offset = serverTime.Offset; + return rdtNetClient; } - private static TorrentClientTorrent Map(Torrent torrent) + private TorrentClientTorrent Map(Torrent torrent) { return new TorrentClientTorrent { @@ -55,7 +60,7 @@ namespace RdtClient.Service.Services.TorrentClients Split = torrent.Split, Progress = torrent.Progress, Status = torrent.Status, - Added = torrent.Added, + Added = ChangeTimeZone(torrent.Added).Value, Files = (torrent.Files ?? new List()).Select(m => new TorrentClientFile { Path = m.Path, @@ -64,7 +69,7 @@ namespace RdtClient.Service.Services.TorrentClients Selected = m.Selected }).ToList(), Links = torrent.Links, - Ended = torrent.Ended, + Ended = ChangeTimeZone(torrent.Ended), Speed = torrent.Speed, Seeders = torrent.Seeders, }; @@ -289,19 +294,41 @@ namespace RdtClient.Service.Services.TorrentClients { var rdTorrent = await GetInfo(torrent.RdId); - var torrentLinks = rdTorrent.Links.Where(m => !String.IsNullOrWhiteSpace(m)).ToList(); + var downloadLinks = rdTorrent.Links.Where(m => !String.IsNullOrWhiteSpace(m)).ToList(); - Log($"Found {torrentLinks} links", torrent); + Log($"Found {downloadLinks.Count} links", torrent); - // Sometimes RD will give you 1 rar with all files, sometimes it will give you 1 link per file. - if (torrent.Files.Count(m => m.Selected) != torrentLinks.Count && - torrent.ManualFiles.Count != torrentLinks.Count && - torrentLinks.Count != 1) + foreach (var link in downloadLinks) { - return null; + Log($"{link}", torrent); } - return torrentLinks; + // Check if all the links are set that have been selected + if (torrent.Files.Count(m => m.Selected) == downloadLinks.Count) + { + return downloadLinks; + } + + // Check if all all the links are set for manual selection + if (torrent.ManualFiles.Count == downloadLinks.Count) + { + return downloadLinks; + } + + // If there is only 1 link, delay for 1 minute to see if more links pop up. + if (downloadLinks.Count == 1 && torrent.RdEnded.HasValue && DateTime.UtcNow > torrent.RdEnded.Value.ToUniversalTime().AddMinutes(1)) + { + var rem = torrent.RdEnded.Value.ToUniversalTime() - DateTimeOffset.UtcNow; + Log($"Delaying {rem.TotalSeconds} more seconds", torrent); + return downloadLinks; + } + + return null; + } + + private DateTimeOffset? ChangeTimeZone(DateTimeOffset? dateTimeOffset) + { + return dateTimeOffset?.Subtract(_offset).ToOffset(_offset); } private void Log(String message, Data.Models.Data.Torrent torrent = null) diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index ab3a511..dc2ccbb 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -177,21 +177,21 @@ namespace RdtClient.Service.Services return; } - var torrentLinks = await _torrentClient.GetDownloadLinks(torrent); + var downloadLinks = await _torrentClient.GetDownloadLinks(torrent); - if (torrentLinks == null) + if (downloadLinks == null) { return; } - foreach (var file in torrentLinks) + foreach (var downloadLink in downloadLinks) { // Make sure downloads don't get added multiple times - var downloadExists = await _downloads.Get(torrent.TorrentId, file); + var downloadExists = await _downloads.Get(torrent.TorrentId, downloadLink); - if (downloadExists == null && !String.IsNullOrWhiteSpace(file)) + if (downloadExists == null && !String.IsNullOrWhiteSpace(downloadLink)) { - await _downloads.Add(torrent.TorrentId, file); + await _downloads.Add(torrent.TorrentId, downloadLink); } } }