From 519a5ca75cff3db02d82dbe16d3db6b991c96cce Mon Sep 17 00:00:00 2001 From: Qstick Date: Tue, 18 Aug 2020 23:21:44 -0400 Subject: [PATCH] New: Allow Sonarr List Sync by Source Tag Fixes #3966 --- .../Annotations/FieldDefinitionAttribute.cs | 1 + .../ImportLists/Sonarr/SonarrAPIResource.cs | 7 ++++ .../ImportLists/Sonarr/SonarrImport.cs | 38 +++++++++++++------ .../ImportLists/Sonarr/SonarrSettings.cs | 6 ++- .../ImportLists/Sonarr/SonarrV3Proxy.cs | 6 +++ 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs b/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs index 46aecfa6a..0d2700df8 100644 --- a/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs +++ b/src/NzbDrone.Core/Annotations/FieldDefinitionAttribute.cs @@ -37,6 +37,7 @@ namespace NzbDrone.Core.Annotations public int Order { get; private set; } public string Label { get; set; } public string Hint { get; set; } + public string RequestAction { get; set; } } public class FieldSelectOption diff --git a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs index 628fd6180..2ba89f52b 100644 --- a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs @@ -14,6 +14,7 @@ namespace NzbDrone.Core.ImportLists.Sonarr public int Year { get; set; } public string TitleSlug { get; set; } public int QualityProfileId { get; set; } + public HashSet Tags { get; set; } } public class SonarrProfile @@ -21,4 +22,10 @@ namespace NzbDrone.Core.ImportLists.Sonarr public string Name { get; set; } public int Id { get; set; } } + + public class SonarrTag + { + public string Label { get; set; } + public int Id { get; set; } + } } diff --git a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrImport.cs b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrImport.cs index 9a9f4a5c0..4c8793fc8 100644 --- a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrImport.cs +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrImport.cs @@ -38,7 +38,8 @@ namespace NzbDrone.Core.ImportLists.Sonarr foreach (var item in remoteSeries) { - if (!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(item.QualityProfileId)) + if ((!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(item.QualityProfileId)) && + (!Settings.TagIds.Any() || Settings.TagIds.Any(tagId => item.Tags.Any(itemTagId => itemTagId == tagId)))) { series.Add(new ImportListItemInfo { @@ -60,24 +61,24 @@ namespace NzbDrone.Core.ImportLists.Sonarr public override object RequestAction(string action, IDictionary query) { - if (action == "getDevices") + // Return early if there is not an API key + if (Settings.ApiKey.IsNullOrWhiteSpace()) { - // Return early if there is not an API key - if (Settings.ApiKey.IsNullOrWhiteSpace()) + return new { - return new - { - devices = new List() - }; - } + devices = new List() + }; + } + if (action == "getProfiles") + { Settings.Validate().Filter("ApiKey").ThrowOnError(); - var devices = _sonarrV3Proxy.GetProfiles(Settings); + var profiles = _sonarrV3Proxy.GetProfiles(Settings); return new { - options = devices.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) + options = profiles.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) .Select(d => new { id = d.Id, @@ -86,6 +87,21 @@ namespace NzbDrone.Core.ImportLists.Sonarr }; } + if (action == "getTags") + { + var tags = _sonarrV3Proxy.GetTags(Settings); + + return new + { + options = tags.OrderBy(d => d.Label, StringComparer.InvariantCultureIgnoreCase) + .Select(d => new + { + id = d.Id, + name = d.Label + }) + }; + } + return new { }; } diff --git a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs index 5af1c5b6c..2cc883b8f 100644 --- a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs @@ -23,6 +23,7 @@ namespace NzbDrone.Core.ImportLists.Sonarr BaseUrl = ""; ApiKey = ""; ProfileIds = new int[] { }; + TagIds = new int[] { }; } [FieldDefinition(0, Label = "Full URL", HelpText = "URL, including port, of the Sonarr V3 instance to import from")] @@ -31,9 +32,12 @@ namespace NzbDrone.Core.ImportLists.Sonarr [FieldDefinition(1, Label = "API Key", HelpText = "Apikey of the Sonarr V3 instance to import from")] public string ApiKey { get; set; } - [FieldDefinition(2, Type = FieldType.Device, Label = "Profiles", HelpText = "Profiles from the source instance to import from")] + [FieldDefinition(2, Type = FieldType.Select, SelectOptionsProviderAction = "getProfiles", Label = "Profiles", HelpText = "Profiles from the source instance to import from")] public IEnumerable ProfileIds { get; set; } + [FieldDefinition(3, Type = FieldType.Select, SelectOptionsProviderAction = "getTags", Label = "Tags", HelpText = "Tags from the source instance to import from")] + public IEnumerable TagIds { get; set; } + public NzbDroneValidationResult Validate() { return new NzbDroneValidationResult(Validator.Validate(this)); diff --git a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrV3Proxy.cs b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrV3Proxy.cs index ce68abba2..46038b491 100644 --- a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrV3Proxy.cs +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrV3Proxy.cs @@ -13,6 +13,7 @@ namespace NzbDrone.Core.ImportLists.Sonarr { List GetSeries(SonarrSettings settings); List GetProfiles(SonarrSettings settings); + List GetTags(SonarrSettings settings); ValidationFailure Test(SonarrSettings settings); } @@ -37,6 +38,11 @@ namespace NzbDrone.Core.ImportLists.Sonarr return Execute("/api/v3/qualityprofile", settings); } + public List GetTags(SonarrSettings settings) + { + return Execute("/api/v3/tag", settings); + } + public ValidationFailure Test(SonarrSettings settings) { try