From 39ea2dd32fe4a5d192e8393c06324a813d1db567 Mon Sep 17 00:00:00 2001 From: jtpavlock Date: Sun, 9 Jun 2019 12:54:53 -0500 Subject: [PATCH] New: Ability to set a post-import label in Deluge, rTorrent, qBittorrent, and uTorrent --- .../Download/Clients/Deluge/Deluge.cs | 52 +++++++++--- .../Download/Clients/Deluge/DelugeProxy.cs | 4 +- .../Download/Clients/Deluge/DelugeSettings.cs | 12 ++- .../Clients/QBittorrent/QBittorrent.cs | 83 +++++++++++++++---- .../Clients/QBittorrent/QBittorrentLabel.cs | 10 +++ .../QBittorrent/QBittorrentProxySelector.cs | 2 + .../Clients/QBittorrent/QBittorrentProxyV1.cs | 13 +++ .../Clients/QBittorrent/QBittorrentProxyV2.cs | 15 ++++ .../QBittorrent/QBittorrentSettings.cs | 14 +++- .../Download/Clients/rTorrent/RTorrent.cs | 18 ++++ .../Clients/rTorrent/RTorrentProxy.cs | 39 ++++++--- .../Clients/rTorrent/RTorrentSettings.cs | 11 ++- .../Download/Clients/uTorrent/UTorrent.cs | 28 ++++++- .../Clients/uTorrent/UTorrentProxy.cs | 15 ++++ .../Clients/uTorrent/UTorrentSettings.cs | 9 +- .../Download/CompletedDownloadService.cs | 1 - .../Download/DownloadClientBase.cs | 5 ++ .../Download/DownloadEventHub.cs | 36 ++++++-- src/NzbDrone.Core/Download/IDownloadClient.cs | 1 + src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 20 files changed, 303 insertions(+), 66 deletions(-) create mode 100644 src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentLabel.cs diff --git a/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs b/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs index 7a55bedc8..de5f56d10 100644 --- a/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs +++ b/src/NzbDrone.Core/Download/Clients/Deluge/Deluge.cs @@ -31,6 +31,24 @@ namespace NzbDrone.Core.Download.Clients.Deluge _proxy = proxy; } + public override void MarkItemAsImported(DownloadClientItem downloadClientItem) + { + // set post-import category + if (Settings.TvImportedCategory.IsNotNullOrWhiteSpace() && + Settings.TvImportedCategory != Settings.TvCategory) + { + try + { + _proxy.SetTorrentLabel(downloadClientItem.DownloadId.ToLower(), Settings.TvImportedCategory, Settings); + } + catch (DownloadClientUnavailableException) + { + _logger.Warn("Failed to set torrent post-import label \"{0}\" for {1} in Deluge. Does the label exist?", + Settings.TvImportedCategory, downloadClientItem.Title); + } + } + } + protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) { var actualHash = _proxy.AddTorrentFromMagnet(magnetLink, Settings); @@ -40,13 +58,13 @@ namespace NzbDrone.Core.Download.Clients.Deluge throw new DownloadClientException("Deluge failed to add magnet " + magnetLink); } - if (!Settings.TvCategory.IsNullOrWhiteSpace()) + _proxy.SetTorrentSeedingConfiguration(actualHash, remoteEpisode.SeedConfiguration, Settings); + + if (Settings.TvCategory.IsNotNullOrWhiteSpace()) { - _proxy.SetLabel(actualHash, Settings.TvCategory, Settings); + _proxy.SetTorrentLabel(actualHash, Settings.TvCategory, Settings); } - _proxy.SetTorrentSeedingConfiguration(actualHash, remoteEpisode.SeedConfiguration, Settings); - var isRecentEpisode = remoteEpisode.IsRecentEpisode(); if (isRecentEpisode && Settings.RecentTvPriority == (int)DelugePriority.First || @@ -69,9 +87,9 @@ namespace NzbDrone.Core.Download.Clients.Deluge _proxy.SetTorrentSeedingConfiguration(actualHash, remoteEpisode.SeedConfiguration, Settings); - if (!Settings.TvCategory.IsNullOrWhiteSpace()) + if (Settings.TvCategory.IsNotNullOrWhiteSpace()) { - _proxy.SetLabel(actualHash, Settings.TvCategory, Settings); + _proxy.SetTorrentLabel(actualHash, Settings.TvCategory, Settings); } var isRecentEpisode = remoteEpisode.IsRecentEpisode(); @@ -91,7 +109,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge { IEnumerable torrents; - if (!Settings.TvCategory.IsNullOrWhiteSpace()) + if (Settings.TvCategory.IsNotNullOrWhiteSpace()) { torrents = _proxy.GetTorrentsByLabel(Settings.TvCategory, Settings); } @@ -250,7 +268,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge private ValidationFailure TestCategory() { - if (Settings.TvCategory.IsNullOrWhiteSpace()) + if (Settings.TvCategory.IsNullOrWhiteSpace() && Settings.TvImportedCategory.IsNullOrWhiteSpace()) { return null; } @@ -267,7 +285,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge var labels = _proxy.GetAvailableLabels(Settings); - if (!labels.Contains(Settings.TvCategory)) + if (Settings.TvCategory.IsNotNullOrWhiteSpace() && !labels.Contains(Settings.TvCategory)) { _proxy.AddLabel(Settings.TvCategory, Settings); labels = _proxy.GetAvailableLabels(Settings); @@ -276,7 +294,21 @@ namespace NzbDrone.Core.Download.Clients.Deluge { return new NzbDroneValidationFailure("TvCategory", "Configuration of label failed") { - DetailedDescription = "Sonarr as unable to add the label to Deluge." + DetailedDescription = "Sonarr was unable to add the label to Deluge." + }; + } + } + + if (Settings.TvImportedCategory.IsNotNullOrWhiteSpace() && !labels.Contains(Settings.TvImportedCategory)) + { + _proxy.AddLabel(Settings.TvImportedCategory, Settings); + labels = _proxy.GetAvailableLabels(Settings); + + if (!labels.Contains(Settings.TvImportedCategory)) + { + return new NzbDroneValidationFailure("TvImportedCategory", "Configuration of label failed") + { + DetailedDescription = "Sonarr was unable to add the label to Deluge." }; } } diff --git a/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs b/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs index 80912667f..c34352fe6 100644 --- a/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Deluge/DelugeProxy.cs @@ -19,7 +19,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge string[] GetAvailablePlugins(DelugeSettings settings); string[] GetEnabledPlugins(DelugeSettings settings); string[] GetAvailableLabels(DelugeSettings settings); - void SetLabel(string hash, string label, DelugeSettings settings); + void SetTorrentLabel(string hash, string label, DelugeSettings settings); void SetTorrentConfiguration(string hash, string key, object value, DelugeSettings settings); void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, DelugeSettings settings); void AddLabel(string label, DelugeSettings settings); @@ -185,7 +185,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge ProcessRequest(settings, "label.add", label); } - public void SetLabel(string hash, string label, DelugeSettings settings) + public void SetTorrentLabel(string hash, string label, DelugeSettings settings) { ProcessRequest(settings, "label.set_torrent", hash, label); } diff --git a/src/NzbDrone.Core/Download/Clients/Deluge/DelugeSettings.cs b/src/NzbDrone.Core/Download/Clients/Deluge/DelugeSettings.cs index 025b775fd..aa8564c76 100644 --- a/src/NzbDrone.Core/Download/Clients/Deluge/DelugeSettings.cs +++ b/src/NzbDrone.Core/Download/Clients/Deluge/DelugeSettings.cs @@ -13,6 +13,7 @@ namespace NzbDrone.Core.Download.Clients.Deluge RuleFor(c => c.Port).InclusiveBetween(1, 65535); RuleFor(c => c.TvCategory).Matches("^[-a-z]*$").WithMessage("Allowed characters a-z and -"); + RuleFor(c => c.TvImportedCategory).Matches("^[-a-z]*$").WithMessage("Allowed characters a-z and -"); } } @@ -43,16 +44,19 @@ namespace NzbDrone.Core.Download.Clients.Deluge [FieldDefinition(4, Label = "Category", Type = FieldType.Textbox, HelpText = "Adding a category specific to Sonarr avoids conflicts with unrelated downloads, but it's optional")] public string TvCategory { get; set; } - [FieldDefinition(5, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(DelugePriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] + [FieldDefinition(5, Label = "Post-Import Category", Type = FieldType.Textbox, Advanced = true, HelpText = "Category for Sonarr to set after it has imported the download. Leave blank to disable this feature.")] + public string TvImportedCategory { get; set; } + + [FieldDefinition(6, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(DelugePriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] public int RecentTvPriority { get; set; } - [FieldDefinition(6, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(DelugePriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] + [FieldDefinition(7, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(DelugePriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] public int OlderTvPriority { get; set; } - [FieldDefinition(7, Label = "Add Paused", Type = FieldType.Checkbox)] + [FieldDefinition(8, Label = "Add Paused", Type = FieldType.Checkbox)] public bool AddPaused { get; set; } - [FieldDefinition(8, Label = "Use SSL", Type = FieldType.Checkbox)] + [FieldDefinition(9, Label = "Use SSL", Type = FieldType.Checkbox)] public bool UseSsl { get; set; } public NzbDroneValidationResult Validate() diff --git a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrent.cs b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrent.cs index 645acf386..a449cac6d 100644 --- a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrent.cs +++ b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrent.cs @@ -33,6 +33,24 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent private IQBittorrentProxy Proxy => _proxySelector.GetProxy(Settings); + public override void MarkItemAsImported(DownloadClientItem downloadClientItem) + { + // set post-import category + if (Settings.TvImportedCategory.IsNotNullOrWhiteSpace() && + Settings.TvImportedCategory != Settings.TvCategory) + { + try + { + Proxy.SetTorrentLabel(downloadClientItem.DownloadId.ToLower(), Settings.TvImportedCategory, Settings); + } + catch (DownloadClientException) + { + _logger.Warn("Failed to set post-import torrent label \"{0}\" for {1} in qBittorrent. Does the label exist?", + Settings.TvImportedCategory, downloadClientItem.Title); + } + } + } + protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) { if (!Proxy.GetConfig(Settings).DhtEnabled && !magnetLink.Contains("&tr=")) @@ -42,11 +60,6 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent Proxy.AddTorrentFromUrl(magnetLink, Settings); - if (Settings.TvCategory.IsNotNullOrWhiteSpace()) - { - Proxy.SetTorrentLabel(hash.ToLower(), Settings.TvCategory, Settings); - } - var isRecentEpisode = remoteEpisode.IsRecentEpisode(); if (isRecentEpisode && Settings.RecentTvPriority == (int)QBittorrentPriority.First || @@ -69,18 +82,6 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent { Proxy.AddTorrentFromFile(filename, fileContent, Settings); - try - { - if (Settings.TvCategory.IsNotNullOrWhiteSpace()) - { - Proxy.SetTorrentLabel(hash.ToLower(), Settings.TvCategory, Settings); - } - } - catch (Exception ex) - { - _logger.Warn(ex, "Failed to set the torrent label for {0}.", filename); - } - try { var isRecentEpisode = remoteEpisode.IsRecentEpisode(); @@ -216,6 +217,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent { failures.AddIfNotNull(TestConnection()); if (failures.HasErrors()) return; + failures.AddIfNotNull(TestCategory()); failures.AddIfNotNull(TestPrioritySupport()); failures.AddIfNotNull(TestGetTorrents()); } @@ -293,6 +295,53 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent return null; } + private ValidationFailure TestCategory() + { + if (Settings.TvCategory.IsNullOrWhiteSpace() && Settings.TvImportedCategory.IsNullOrWhiteSpace()) + { + return null; + } + + // api v1 doesn't need to check/add categories as it's done on set + var version = _proxySelector.GetProxy(Settings, true).GetApiVersion(Settings); + if (version < Version.Parse("2.0")) + { + return null; + } + + Dictionary labels = Proxy.GetLabels(Settings); + + if (Settings.TvCategory.IsNotNullOrWhiteSpace() && !labels.ContainsKey(Settings.TvCategory)) + { + Proxy.AddLabel(Settings.TvCategory, Settings); + labels = Proxy.GetLabels(Settings); + + if (!labels.ContainsKey(Settings.TvCategory)) + { + return new NzbDroneValidationFailure("TvCategory", "Configuration of label failed") + { + DetailedDescription = "Sonarr was unable to add the label to qBittorrent." + }; + } + } + + if (Settings.TvImportedCategory.IsNotNullOrWhiteSpace() && !labels.ContainsKey(Settings.TvImportedCategory)) + { + Proxy.AddLabel(Settings.TvImportedCategory, Settings); + labels = Proxy.GetLabels(Settings); + + if (!labels.ContainsKey(Settings.TvImportedCategory)) + { + return new NzbDroneValidationFailure("TvImportedCategory", "Configuration of label failed") + { + DetailedDescription = "Sonarr was unable to add the label to qBittorrent." + }; + } + } + + return null; + } + private ValidationFailure TestPrioritySupport() { var recentPriorityDefault = Settings.RecentTvPriority == (int)QBittorrentPriority.Last; diff --git a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentLabel.cs b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentLabel.cs new file mode 100644 index 000000000..153e8e009 --- /dev/null +++ b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentLabel.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; + +namespace NzbDrone.Core.Download.Clients.QBittorrent +{ + public class QBittorrentLabel + { + public string Name { get; set; } + public string SavePath { get; set; } + } +} diff --git a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxySelector.cs b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxySelector.cs index 41e9719c6..0bde61ce0 100644 --- a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxySelector.cs +++ b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxySelector.cs @@ -23,6 +23,8 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent void RemoveTorrent(string hash, Boolean removeData, QBittorrentSettings settings); void SetTorrentLabel(string hash, string label, QBittorrentSettings settings); + void AddLabel(string label, QBittorrentSettings settings); + Dictionary GetLabels(QBittorrentSettings settings); void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings); void MoveTorrentToTopInQueue(string hash, QBittorrentSettings settings); void PauseTorrent(string hash, QBittorrentSettings settings); diff --git a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV1.cs b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV1.cs index 6d1b8d1c2..069781d74 100644 --- a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV1.cs +++ b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV1.cs @@ -190,6 +190,19 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent } } + public void AddLabel(string label, QBittorrentSettings settings) + { + var request = BuildRequest(settings).Resource("/command/addCategory") + .Post() + .AddFormParameter("category", label); + ProcessRequest(request, settings); + } + + public Dictionary GetLabels(QBittorrentSettings settings) + { + throw new NotSupportedException("qBittorrent api v1 does not support getting all torrent categories"); + } + public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings) { // Not supported on api v1 diff --git a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV2.cs b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV2.cs index 6792c55ba..8140b9055 100644 --- a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV2.cs +++ b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentProxyV2.cs @@ -110,6 +110,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent var request = BuildRequest(settings).Resource("/api/v2/torrents/add") .Post() .AddFormParameter("urls", torrentUrl); + if (settings.TvCategory.IsNotNullOrWhiteSpace()) { request.AddFormParameter("category", settings.TvCategory); @@ -177,6 +178,20 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent ProcessRequest(request, settings); } + public void AddLabel(string label, QBittorrentSettings settings) + { + var request = BuildRequest(settings).Resource("/api/v2/torrents/createCategory") + .Post() + .AddFormParameter("category", label); + ProcessRequest(request, settings); + } + + public Dictionary GetLabels(QBittorrentSettings settings) + { + var request = BuildRequest(settings).Resource("/api/v2/torrents/categories"); + return Json.Deserialize>(ProcessRequest(request, settings)); + } + public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings) { var ratioLimit = seedConfiguration.Ratio.HasValue ? seedConfiguration.Ratio : -2; diff --git a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentSettings.cs b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentSettings.cs index 337c065a9..6756b42b5 100644 --- a/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentSettings.cs +++ b/src/NzbDrone.Core/Download/Clients/QBittorrent/QBittorrentSettings.cs @@ -11,6 +11,9 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent { RuleFor(c => c.Host).ValidHost(); RuleFor(c => c.Port).InclusiveBetween(1, 65535); + + RuleFor(c => c.TvCategory).Matches("^[^/\\\\](((?!//)[^\\\\])*[^/\\\\])?$").WithMessage("Can not contain '\\', '//', or start/end with '/'"); + RuleFor(c => c.TvImportedCategory).Matches("^[^/\\\\](((?!//)[^\\\\])*[^/\\\\])?$").WithMessage("Can not contain '\\', '//', or start/end with '/'"); } } @@ -40,16 +43,19 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent [FieldDefinition(4, Label = "Category", Type = FieldType.Textbox, HelpText = "Adding a category specific to Sonarr avoids conflicts with unrelated downloads, but it's optional")] public string TvCategory { get; set; } - [FieldDefinition(5, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(QBittorrentPriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] + [FieldDefinition(5, Label = "Post-Import Category", Type = FieldType.Textbox, Advanced = true, HelpText = "Category for Sonarr to set after it has imported the download. Leave blank to disable this feature.")] + public string TvImportedCategory { get; set; } + + [FieldDefinition(6, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(QBittorrentPriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] public int RecentTvPriority { get; set; } - [FieldDefinition(6, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(QBittorrentPriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] + [FieldDefinition(7, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(QBittorrentPriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] public int OlderTvPriority { get; set; } - [FieldDefinition(7, Label = "Initial State", Type = FieldType.Select, SelectOptions = typeof(QBittorrentState), HelpText = "Initial state for torrents added to qBittorrent")] + [FieldDefinition(8, Label = "Initial State", Type = FieldType.Select, SelectOptions = typeof(QBittorrentState), HelpText = "Initial state for torrents added to qBittorrent")] public int InitialState { get; set; } - [FieldDefinition(8, Label = "Use SSL", Type = FieldType.Checkbox, HelpText = "Use a secure connection. See Options -> Web UI -> 'Use HTTPS instead of HTTP' in qBittorrent.")] + [FieldDefinition(9, Label = "Use SSL", Type = FieldType.Checkbox, HelpText = "Use a secure connection. See Options -> Web UI -> 'Use HTTPS instead of HTTP' in qBittorrent.")] public bool UseSsl { get; set; } public NzbDroneValidationResult Validate() diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs index 8ff087b90..f14d89f2d 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs @@ -37,6 +37,24 @@ namespace NzbDrone.Core.Download.Clients.RTorrent _rTorrentDirectoryValidator = rTorrentDirectoryValidator; } + public override void MarkItemAsImported(DownloadClientItem downloadClientItem) + { + // set post-import category + if (Settings.TvImportedCategory.IsNotNullOrWhiteSpace() && + Settings.TvImportedCategory != Settings.TvCategory) + { + try + { + _proxy.SetTorrentLabel(downloadClientItem.DownloadId.ToLower(), Settings.TvImportedCategory, Settings); + } + catch (Exception ex) + { + _logger.Warn(ex, "Failed to set torrent post-import label \"{0}\" for {1} in rTorrent. Does the label exist?", + Settings.TvImportedCategory, downloadClientItem.Title); + } + } + } + protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) { var priority = (RTorrentPriority)(remoteEpisode.IsRecentEpisode() ? Settings.RecentTvPriority : Settings.OlderTvPriority); diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs index ff9a4332f..b35063076 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs @@ -18,6 +18,7 @@ namespace NzbDrone.Core.Download.Clients.RTorrent void AddTorrentFromUrl(string torrentUrl, string label, RTorrentPriority priority, string directory, RTorrentSettings settings); void AddTorrentFromFile(string fileName, byte[] fileContent, string label, RTorrentPriority priority, string directory, RTorrentSettings settings); void RemoveTorrent(string hash, RTorrentSettings settings); + void SetTorrentLabel(string hash, string label, RTorrentSettings settings); bool HasHashTorrent(string hash, RTorrentSettings settings); } @@ -44,6 +45,9 @@ namespace NzbDrone.Core.Download.Clients.RTorrent [XmlRpcMethod("d.name")] string GetName(string hash); + [XmlRpcMethod("d.custom1.set")] + string SetLabel(string hash, string label); + [XmlRpcMethod("system.client_version")] string GetVersion(); } @@ -90,20 +94,20 @@ namespace NzbDrone.Core.Download.Clients.RTorrent foreach (object[] torrent in ret) { - var labelDecoded = System.Web.HttpUtility.UrlDecode((string) torrent[3]); + var labelDecoded = System.Web.HttpUtility.UrlDecode((string)torrent[3]); var item = new RTorrentTorrent(); - item.Name = (string) torrent[0]; - item.Hash = (string) torrent[1]; - item.Path = (string) torrent[2]; + item.Name = (string)torrent[0]; + item.Hash = (string)torrent[1]; + item.Path = (string)torrent[2]; item.Category = labelDecoded; - item.TotalSize = (long) torrent[4]; - item.RemainingSize = (long) torrent[5]; - item.DownRate = (long) torrent[6]; - item.Ratio = (long) torrent[7]; - item.IsOpen = Convert.ToBoolean((long) torrent[8]); - item.IsActive = Convert.ToBoolean((long) torrent[9]); - item.IsFinished = Convert.ToBoolean((long) torrent[10]); + item.TotalSize = (long)torrent[4]; + item.RemainingSize = (long)torrent[5]; + item.DownRate = (long)torrent[6]; + item.Ratio = (long)torrent[7]; + item.IsOpen = Convert.ToBoolean((long)torrent[8]); + item.IsActive = Convert.ToBoolean((long)torrent[9]); + item.IsFinished = Convert.ToBoolean((long)torrent[10]); items.Add(item); } @@ -157,6 +161,19 @@ namespace NzbDrone.Core.Download.Clients.RTorrent } } + public void SetTorrentLabel(string hash, string label, RTorrentSettings settings) + { + _logger.Debug("Executing remote method: d.custom1.set"); + + var client = BuildClient(settings); + var response = ExecuteRequest(() => client.SetLabel(hash, label)); + + if (response != label) + { + throw new DownloadClientException("Could not set label to {1} for torrent: {0}.", hash, label); + } + } + public void RemoveTorrent(string hash, RTorrentSettings settings) { _logger.Debug("Executing remote method: d.erase"); diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs index 67a040105..98a87ea4b 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs @@ -52,16 +52,19 @@ namespace NzbDrone.Core.Download.Clients.RTorrent [FieldDefinition(6, Label = "Category", Type = FieldType.Textbox, HelpText = "Adding a category specific to Sonarr avoids conflicts with unrelated downloads, but it's optional.")] public string TvCategory { get; set; } - [FieldDefinition(7, Label = "Directory", Type = FieldType.Textbox, Advanced = true, HelpText = "Optional location to put downloads in, leave blank to use the default rTorrent location")] + [FieldDefinition(7, Label = "Post-Import Category", Type = FieldType.Textbox, Advanced = true, HelpText = "Category for Sonarr to set after it has imported the download. Leave blank to disable this feature.")] + public string TvImportedCategory { get; set; } + + [FieldDefinition(8, Label = "Directory", Type = FieldType.Textbox, Advanced = true, HelpText = "Optional location to put downloads in, leave blank to use the default rTorrent location")] public string TvDirectory { get; set; } - [FieldDefinition(8, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(RTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] + [FieldDefinition(9, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(RTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] public int RecentTvPriority { get; set; } - [FieldDefinition(9, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(RTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] + [FieldDefinition(10, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(RTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] public int OlderTvPriority { get; set; } - [FieldDefinition(10, Label = "Add Stopped", Type = FieldType.Checkbox, HelpText = "Enabling will prevent magnets from downloading before downloading")] + [FieldDefinition(11, Label = "Add Stopped", Type = FieldType.Checkbox, HelpText = "Enabling will prevent magnets from downloading before downloading")] public bool AddStopped { get; set; } public NzbDroneValidationResult Validate() diff --git a/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrent.cs b/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrent.cs index 664c9ba36..5540d8f63 100644 --- a/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrent.cs +++ b/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrent.cs @@ -36,12 +36,32 @@ namespace NzbDrone.Core.Download.Clients.UTorrent _torrentCache = cacheManager.GetCache(GetType(), "differentialTorrents"); } + public override void MarkItemAsImported(DownloadClientItem downloadClientItem) + { + // set post-import category + if (Settings.TvImportedCategory.IsNotNullOrWhiteSpace() && + Settings.TvImportedCategory != Settings.TvCategory) + { + _proxy.SetTorrentLabel(downloadClientItem.DownloadId.ToLower(), Settings.TvImportedCategory, Settings); + + // old label must be explicitly removed + if (Settings.TvCategory.IsNotNullOrWhiteSpace()) + { + _proxy.RemoveTorrentLabel(downloadClientItem.DownloadId.ToLower(), Settings.TvCategory, Settings); + } + } + } + protected override string AddFromMagnetLink(RemoteEpisode remoteEpisode, string hash, string magnetLink) { _proxy.AddTorrentFromUrl(magnetLink, Settings); - _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); _proxy.SetTorrentSeedingConfiguration(hash, remoteEpisode.SeedConfiguration, Settings); + if (Settings.TvCategory.IsNotNullOrWhiteSpace()) + { + _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); + } + var isRecentEpisode = remoteEpisode.IsRecentEpisode(); if (isRecentEpisode && Settings.RecentTvPriority == (int)UTorrentPriority.First || @@ -58,9 +78,13 @@ namespace NzbDrone.Core.Download.Clients.UTorrent protected override string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent) { _proxy.AddTorrentFromFile(filename, fileContent, Settings); - _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); _proxy.SetTorrentSeedingConfiguration(hash, remoteEpisode.SeedConfiguration, Settings); + if (Settings.TvCategory.IsNotNullOrWhiteSpace()) + { + _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); + } + var isRecentEpisode = remoteEpisode.IsRecentEpisode(); if (isRecentEpisode && Settings.RecentTvPriority == (int)UTorrentPriority.First || diff --git a/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentProxy.cs b/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentProxy.cs index 557ae0e6e..684078f1a 100644 --- a/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentProxy.cs @@ -21,6 +21,7 @@ namespace NzbDrone.Core.Download.Clients.UTorrent void RemoveTorrent(string hash, bool removeData, UTorrentSettings settings); void SetTorrentLabel(string hash, string label, UTorrentSettings settings); + void RemoveTorrentLabel(string hash, string label, UTorrentSettings settings); void MoveTorrentToTopInQueue(string hash, UTorrentSettings settings); void SetState(string hash, UTorrentState state, UTorrentSettings settings); } @@ -151,6 +152,20 @@ namespace NzbDrone.Core.Download.Clients.UTorrent ProcessRequest(requestBuilder, settings); } + public void RemoveTorrentLabel(string hash, string label, UTorrentSettings settings) + { + var requestBuilder = BuildRequest(settings) + .AddQueryParam("action", "setprops") + .AddQueryParam("hash", hash); + + requestBuilder.AddQueryParam("s", "label") + .AddQueryParam("v", label) + .AddQueryParam("s", "label") + .AddQueryParam("v", ""); + + ProcessRequest(requestBuilder, settings); + } + public void MoveTorrentToTopInQueue(string hash, UTorrentSettings settings) { var requestBuilder = BuildRequest(settings) diff --git a/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentSettings.cs b/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentSettings.cs index f76aa6457..b18bbce23 100644 --- a/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentSettings.cs +++ b/src/NzbDrone.Core/Download/Clients/uTorrent/UTorrentSettings.cs @@ -41,13 +41,16 @@ namespace NzbDrone.Core.Download.Clients.UTorrent [FieldDefinition(4, Label = "Category", Type = FieldType.Textbox, HelpText = "Adding a category specific to Sonarr avoids conflicts with unrelated downloads, but it's optional")] public string TvCategory { get; set; } - [FieldDefinition(5, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(UTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] + [FieldDefinition(5, Label = "Post-Import Category", Type = FieldType.Textbox, Advanced = true, HelpText = "Category for Sonarr to set after it has imported the download. Leave blank to disable this feature.")] + public string TvImportedCategory { get; set; } + + [FieldDefinition(6, Label = "Recent Priority", Type = FieldType.Select, SelectOptions = typeof(UTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired within the last 14 days")] public int RecentTvPriority { get; set; } - [FieldDefinition(6, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(UTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] + [FieldDefinition(7, Label = "Older Priority", Type = FieldType.Select, SelectOptions = typeof(UTorrentPriority), HelpText = "Priority to use when grabbing episodes that aired over 14 days ago")] public int OlderTvPriority { get; set; } - [FieldDefinition(7, Label = "Initial State", Type = FieldType.Select, SelectOptions = typeof(UTorrentState), HelpText = "Initial state for torrents added to uTorrent")] + [FieldDefinition(8, Label = "Initial State", Type = FieldType.Select, SelectOptions = typeof(UTorrentState), HelpText = "Initial state for torrents added to uTorrent")] public int IntialState { get; set; } public NzbDroneValidationResult Validate() diff --git a/src/NzbDrone.Core/Download/CompletedDownloadService.cs b/src/NzbDrone.Core/Download/CompletedDownloadService.cs index 8a638b27c..0daf23be4 100644 --- a/src/NzbDrone.Core/Download/CompletedDownloadService.cs +++ b/src/NzbDrone.Core/Download/CompletedDownloadService.cs @@ -127,7 +127,6 @@ namespace NzbDrone.Core.Download trackedDownload.Warn(statusMessages); } - } } } diff --git a/src/NzbDrone.Core/Download/DownloadClientBase.cs b/src/NzbDrone.Core/Download/DownloadClientBase.cs index 713935a4c..4dfbe7244 100644 --- a/src/NzbDrone.Core/Download/DownloadClientBase.cs +++ b/src/NzbDrone.Core/Download/DownloadClientBase.cs @@ -147,5 +147,10 @@ namespace NzbDrone.Core.Download return null; } + + public virtual void MarkItemAsImported(DownloadClientItem downloadClientItem) + { + throw new NotSupportedException(this.Name + " does not support marking items as imported"); + } } } diff --git a/src/NzbDrone.Core/Download/DownloadEventHub.cs b/src/NzbDrone.Core/Download/DownloadEventHub.cs index 0168b013e..8d0f50bc1 100644 --- a/src/NzbDrone.Core/Download/DownloadEventHub.cs +++ b/src/NzbDrone.Core/Download/DownloadEventHub.cs @@ -35,15 +35,17 @@ namespace NzbDrone.Core.Download public void Handle(DownloadCompletedEvent message) { - if (!_configService.RemoveCompletedDownloads || - message.TrackedDownload.DownloadItem.Removed || - !message.TrackedDownload.DownloadItem.CanBeRemoved || - message.TrackedDownload.DownloadItem.Status == DownloadItemStatus.Downloading) + if (_configService.RemoveCompletedDownloads && + !message.TrackedDownload.DownloadItem.Removed && + message.TrackedDownload.DownloadItem.CanBeRemoved && + message.TrackedDownload.DownloadItem.Status != DownloadItemStatus.Downloading) { - return; + RemoveFromDownloadClient(message.TrackedDownload); } - - RemoveFromDownloadClient(message.TrackedDownload); + else + { + MarkItemAsImported(message.TrackedDownload); + } } public void Handle(DownloadFailedEvent message) @@ -74,7 +76,25 @@ namespace NzbDrone.Core.Download } catch (Exception e) { - _logger.Error(e, "Couldn't remove item from client {0}", trackedDownload.DownloadItem.Title); + _logger.Error(e, "Couldn't remove item {0} from client {1}", trackedDownload.DownloadItem.Title, downloadClient.Name); + } + } + + private void MarkItemAsImported(TrackedDownload trackedDownload) + { + var downloadClient = _downloadClientProvider.Get(trackedDownload.DownloadClient); + try + { + _logger.Debug("[{0}] Marking download as imported from {1}", trackedDownload.DownloadItem.Title, trackedDownload.DownloadItem.DownloadClient); + downloadClient.MarkItemAsImported(trackedDownload.DownloadItem); + } + catch (NotSupportedException e) + { + _logger.Debug(e.Message); + } + catch (Exception e) + { + _logger.Error(e, "Couldn't mark item {0} as imported from client {1}", trackedDownload.DownloadItem.Title, downloadClient.Name); } } } diff --git a/src/NzbDrone.Core/Download/IDownloadClient.cs b/src/NzbDrone.Core/Download/IDownloadClient.cs index 4b09535af..e5b75b3c2 100644 --- a/src/NzbDrone.Core/Download/IDownloadClient.cs +++ b/src/NzbDrone.Core/Download/IDownloadClient.cs @@ -13,5 +13,6 @@ namespace NzbDrone.Core.Download IEnumerable GetItems(); void RemoveItem(string downloadId, bool deleteData); DownloadClientInfo GetStatus(); + void MarkItemAsImported(DownloadClientItem downloadClientItem); } } diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 5065c9a09..c968e0449 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -152,6 +152,7 @@ +