From 4d5dfc34a4f9e6c7a150f18e8e41561d15445b8d Mon Sep 17 00:00:00 2001 From: Roger Far Date: Sun, 21 Nov 2021 14:40:16 -0700 Subject: [PATCH] Add auto import and auto delete functionality. --- .../src/app/settings/settings.component.html | 25 +++++++- client/src/app/settings/settings.component.ts | 12 ++++ client/src/app/torrent-status.pipe.ts | 2 +- server/RdtClient.Data/Data/DataContext.cs | 12 ++++ server/RdtClient.Data/Data/SettingData.cs | 2 + .../Models/Internal/DbSettings.cs | 2 + .../Services/TorrentRunner.cs | 2 +- server/RdtClient.Service/Services/Torrents.cs | 58 ++++++++++++++++--- 8 files changed, 103 insertions(+), 12 deletions(-) diff --git a/client/src/app/settings/settings.component.html b/client/src/app/settings/settings.component.html index 2484ccd..c5be409 100644 --- a/client/src/app/settings/settings.component.html +++ b/client/src/app/settings/settings.component.html @@ -33,7 +33,7 @@ https://real-debrid.com
https://alldebrid.com -
+
At this point only 1 provider can be used at the time.

@@ -57,6 +57,29 @@ https://alldebrid.com/apikeys/.

+
+
+ +
+ When selected, import downloads that are not added through RealDebridClient but have been directly added to + Real-Debrid or AllDebrid. +
+
+
+
+
+ +
+ When selected, cancel and delete downloads that have been removed from Real-Debrid or AllDebrid. +
+
+
diff --git a/client/src/app/settings/settings.component.ts b/client/src/app/settings/settings.component.ts index 28887bf..1c2c65c 100644 --- a/client/src/app/settings/settings.component.ts +++ b/client/src/app/settings/settings.component.ts @@ -27,6 +27,8 @@ export class SettingsComponent implements OnInit { public settingLogLevel: string; public settingProvider: string; + public settingProviderAutoImport: boolean; + public settingProviderAutoDelete: boolean; public settingRealDebridApiKey: string; public settingDownloadPath: string; public settingMappedPath: string; @@ -59,6 +61,8 @@ export class SettingsComponent implements OnInit { this.settingsService.get().subscribe( (results) => { this.settingProvider = this.getSetting(results, 'Provider'); + this.settingProviderAutoImport = this.getSetting(results, 'ProviderAutoImport') === '1'; + this.settingProviderAutoDelete = this.getSetting(results, 'ProviderAutoDelete') === '1'; this.settingRealDebridApiKey = this.getSetting(results, 'RealDebridApiKey'); this.settingLogLevel = this.getSetting(results, 'LogLevel'); this.settingDownloadPath = this.getSetting(results, 'DownloadPath'); @@ -92,6 +96,14 @@ export class SettingsComponent implements OnInit { settingId: 'Provider', value: this.settingProvider, }, + { + settingId: 'ProviderAutoImport', + value: this.settingProviderAutoImport ? '1' : '0', + }, + { + settingId: 'ProviderAutoDelete', + value: this.settingProviderAutoDelete ? '1' : '0', + }, { settingId: 'RealDebridApiKey', value: this.settingRealDebridApiKey, diff --git a/client/src/app/torrent-status.pipe.ts b/client/src/app/torrent-status.pipe.ts index 8e5b96d..4bba89d 100644 --- a/client/src/app/torrent-status.pipe.ts +++ b/client/src/app/torrent-status.pipe.ts @@ -73,7 +73,7 @@ export class TorrentStatusPipe implements PipeTransform { return `Queued for unpacking`; } - const queuedForDownload = torrent.downloads.where((m) => m.downloadQueued && !m.downloadStarted); + const queuedForDownload = torrent.downloads.where((m) => !m.downloadStarted && !m.downloadFinished); if (queuedForDownload.length > 0) { return `Queued for downloading`; diff --git a/server/RdtClient.Data/Data/DataContext.cs b/server/RdtClient.Data/Data/DataContext.cs index 2c9ed11..9b16726 100644 --- a/server/RdtClient.Data/Data/DataContext.cs +++ b/server/RdtClient.Data/Data/DataContext.cs @@ -47,6 +47,18 @@ namespace RdtClient.Data.Data Value = "RealDebrid" }, new Setting + { + SettingId = "ProviderAutoImport", + Type = "Int32", + Value = "0" + }, + new Setting + { + SettingId = "ProviderAutoDelete", + Type = "Int32", + Value = "0" + }, + new Setting { SettingId = "RealDebridApiKey", Type = "String", diff --git a/server/RdtClient.Data/Data/SettingData.cs b/server/RdtClient.Data/Data/SettingData.cs index dbe1437..d1e6a63 100644 --- a/server/RdtClient.Data/Data/SettingData.cs +++ b/server/RdtClient.Data/Data/SettingData.cs @@ -49,6 +49,8 @@ namespace RdtClient.Data.Data Get = new DbSettings { Provider = GetString("Provider"), + ProviderAutoImport = GetInt32("ProviderAutoImport"), + ProviderAutoDelete= GetInt32("ProviderAutoDelete"), RealDebridApiKey = GetString("RealDebridApiKey"), DownloadPath = GetString("DownloadPath"), DownloadClient = GetString("DownloadClient"), diff --git a/server/RdtClient.Data/Models/Internal/DbSettings.cs b/server/RdtClient.Data/Models/Internal/DbSettings.cs index b079dce..5358b1c 100644 --- a/server/RdtClient.Data/Models/Internal/DbSettings.cs +++ b/server/RdtClient.Data/Models/Internal/DbSettings.cs @@ -5,6 +5,8 @@ namespace RdtClient.Data.Models.Internal public class DbSettings { public String Provider { get; set; } + public Int32 ProviderAutoImport { get; set; } + public Int32 ProviderAutoDelete { get; set; } public String RealDebridApiKey { get; set; } public String DownloadPath { get; set; } public String DownloadClient { get; set; } diff --git a/server/RdtClient.Service/Services/TorrentRunner.cs b/server/RdtClient.Service/Services/TorrentRunner.cs index f91ee7c..bd0b8ea 100644 --- a/server/RdtClient.Service/Services/TorrentRunner.cs +++ b/server/RdtClient.Service/Services/TorrentRunner.cs @@ -269,7 +269,7 @@ namespace RdtClient.Service.Services torrents = torrents.Where(m => m.Completed == null).ToList(); // Only poll Real-Debrid every second when a hub is connected, otherwise every 30 seconds - if (_nextUpdate < DateTime.UtcNow && torrents.Count > 0) + if (_nextUpdate < DateTime.UtcNow && ((torrents.Count > 0 && Settings.Get.ProviderAutoImport == 0) || Settings.Get.ProviderAutoImport == 1)) { Log($"Updating torrent info from Real-Debrid"); diff --git a/server/RdtClient.Service/Services/Torrents.cs b/server/RdtClient.Service/Services/Torrents.cs index e6c2b1a..958a2f4 100644 --- a/server/RdtClient.Service/Services/Torrents.cs +++ b/server/RdtClient.Service/Services/Torrents.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging; using MonoTorrent; using System.Text.Json.Serialization; using RdtClient.Data.Data; +using RdtClient.Data.Enums; using RdtClient.Data.Models.Internal; using RdtClient.Data.Models.TorrentClient; using RdtClient.Service.Helpers; @@ -212,22 +213,40 @@ namespace RdtClient.Service.Services foreach (var download in torrent.Downloads) { + var retry = 10; + while (TorrentRunner.ActiveDownloadClients.TryGetValue(download.DownloadId, out var downloadClient)) { Log($"Cancelling download", download, torrent); await downloadClient.Cancel(); - await Task.Delay(100); + await Task.Delay(500); + + retry++; + + if (retry > 5) + { + break; + } } + retry = 10; + while (TorrentRunner.ActiveUnpackClients.TryGetValue(download.DownloadId, out var unpackClient)) { Log($"Cancelling unpack", download, torrent); unpackClient.Cancel(); - await Task.Delay(100); + await Task.Delay(500); + + retry++; + + if (retry > 10) + { + break; + } } } @@ -334,22 +353,43 @@ namespace RdtClient.Service.Services { var torrent = torrents.FirstOrDefault(m => m.RdId == rdTorrent.Id); - if (torrent == null) + // Auto import torrents only torrents that have their files selected + if (torrent == null && Settings.Get.ProviderAutoImport == 1) { - continue; - } + var newTorrent = new Torrent + { + Category = null, + DownloadAction = TorrentDownloadAction.DownloadManual, + FinishedAction = TorrentFinishedAction.None, + DownloadMinSize = 0, + TorrentRetryAttempts = 0, + DownloadRetryAttempts = Settings.Get.DownloadRetryAttempts, + Priority = 0, + RdId = rdTorrent.Id + }; - await UpdateTorrentClientData(torrent, rdTorrent); + await _torrentClient.UpdateData(newTorrent, rdTorrent); + + if (newTorrent.RdStatus == TorrentStatus.WaitingForFileSelection) + { + continue; + } + + await _torrentData.Add(rdTorrent.Id, rdTorrent.Hash, null, false, newTorrent); + } + else + { + await UpdateTorrentClientData(torrent, rdTorrent); + } } foreach (var torrent in torrents) { var rdTorrent = rdTorrents.FirstOrDefault(m => m.Id == torrent.RdId); - if (rdTorrent == null) + if (rdTorrent == null && Settings.Get.ProviderAutoDelete == 1) { - await _downloads.DeleteForTorrent(torrent.TorrentId); - await _torrentData.Delete(torrent.TorrentId); + await Delete(torrent.TorrentId, true, false, true); } } }