From ada650b97c896badbfbb110740f0b0b8ac19ca97 Mon Sep 17 00:00:00 2001 From: Qstick Date: Mon, 18 Jan 2021 23:24:51 -0500 Subject: [PATCH] New: Import from another Lidarr Instance Fixes #1054 --- .../ImportLists/ImportListType.cs | 1 + .../ImportLists/Lidarr/LidarrAPIResource.cs | 27 +++++ .../ImportLists/Lidarr/LidarrImport.cs | 113 ++++++++++++++++++ .../ImportLists/Lidarr/LidarrSettings.cs | 47 ++++++++ .../ImportLists/Lidarr/LidarrV1Proxy.cs | 91 ++++++++++++++ 5 files changed, 279 insertions(+) create mode 100644 src/NzbDrone.Core/ImportLists/Lidarr/LidarrAPIResource.cs create mode 100644 src/NzbDrone.Core/ImportLists/Lidarr/LidarrImport.cs create mode 100644 src/NzbDrone.Core/ImportLists/Lidarr/LidarrSettings.cs create mode 100644 src/NzbDrone.Core/ImportLists/Lidarr/LidarrV1Proxy.cs diff --git a/src/NzbDrone.Core/ImportLists/ImportListType.cs b/src/NzbDrone.Core/ImportLists/ImportListType.cs index 36aa04376..ee9097aae 100644 --- a/src/NzbDrone.Core/ImportLists/ImportListType.cs +++ b/src/NzbDrone.Core/ImportLists/ImportListType.cs @@ -2,6 +2,7 @@ namespace NzbDrone.Core.ImportLists { public enum ImportListType { + Program, Spotify, LastFm, Other diff --git a/src/NzbDrone.Core/ImportLists/Lidarr/LidarrAPIResource.cs b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrAPIResource.cs new file mode 100644 index 000000000..0634e8ab9 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrAPIResource.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; + +namespace NzbDrone.Core.ImportLists.Lidarr +{ + public class LidarrArtist + { + public string ArtistName { get; set; } + public string ForeignArtistId { get; set; } + public string Overview { get; set; } + public List Images { get; set; } + public bool Monitored { get; set; } + public int QualityProfileId { get; set; } + public HashSet Tags { get; set; } + } + + public class LidarrProfile + { + public string Name { get; set; } + public int Id { get; set; } + } + + public class LidarrTag + { + public string Label { get; set; } + public int Id { get; set; } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Lidarr/LidarrImport.cs b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrImport.cs new file mode 100644 index 000000000..1aebdbeb1 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrImport.cs @@ -0,0 +1,113 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using FluentValidation.Results; +using NLog; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Parser; +using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.ImportLists.Lidarr +{ + public class LidarrImport : ImportListBase + { + private readonly ILidarrV1Proxy _lidarrV1Proxy; + public override string Name => "Lidarr"; + + public override ImportListType ListType => ImportListType.Program; + + public LidarrImport(ILidarrV1Proxy lidarrV1Proxy, + IImportListStatusService importListStatusService, + IConfigService configService, + IParsingService parsingService, + Logger logger) + : base(importListStatusService, configService, parsingService, logger) + { + _lidarrV1Proxy = lidarrV1Proxy; + } + + public override IList Fetch() + { + var artists = new List(); + + try + { + var remoteArtists = _lidarrV1Proxy.GetArtists(Settings); + + foreach (var remoteArtist in remoteArtists) + { + if ((!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(remoteArtist.QualityProfileId)) && + (!Settings.TagIds.Any() || Settings.TagIds.Any(x => remoteArtist.Tags.Any(y => y == x)))) + { + artists.Add(new ImportListItemInfo + { + ArtistMusicBrainzId = remoteArtist.ForeignArtistId, + Artist = remoteArtist.ArtistName + }); + } + } + + _importListStatusService.RecordSuccess(Definition.Id); + } + catch + { + _importListStatusService.RecordFailure(Definition.Id); + } + + return CleanupListItems(artists); + } + + public override object RequestAction(string action, IDictionary query) + { + // Return early if there is not an API key + if (Settings.ApiKey.IsNullOrWhiteSpace()) + { + return new + { + devices = new List() + }; + } + + Settings.Validate().Filter("ApiKey").ThrowOnError(); + + if (action == "getProfiles") + { + var devices = _lidarrV1Proxy.GetProfiles(Settings); + + return new + { + options = devices.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) + .Select(d => new + { + Value = d.Id, + Name = d.Name + }) + }; + } + + if (action == "getTags") + { + var devices = _lidarrV1Proxy.GetTags(Settings); + + return new + { + options = devices.OrderBy(d => d.Label, StringComparer.InvariantCultureIgnoreCase) + .Select(d => new + { + Value = d.Id, + Name = d.Label + }) + }; + } + + return new { }; + } + + protected override void Test(List failures) + { + failures.AddIfNotNull(_lidarrV1Proxy.Test(Settings)); + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Lidarr/LidarrSettings.cs b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrSettings.cs new file mode 100644 index 000000000..330bc418f --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrSettings.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.ImportLists.Lidarr +{ + public class LidarrSettingsValidator : AbstractValidator + { + public LidarrSettingsValidator() + { + RuleFor(c => c.BaseUrl).ValidRootUrl(); + RuleFor(c => c.ApiKey).NotEmpty(); + } + } + + public class LidarrSettings : IImportListSettings + { + private static readonly LidarrSettingsValidator Validator = new LidarrSettingsValidator(); + + public LidarrSettings() + { + BaseUrl = ""; + ApiKey = ""; + ProfileIds = Array.Empty(); + TagIds = Array.Empty(); + } + + [FieldDefinition(0, Label = "Full URL", HelpText = "URL, including port, of the Lidarr instance to import from")] + public string BaseUrl { get; set; } + + [FieldDefinition(1, Label = "API Key", Privacy = PrivacyLevel.ApiKey, HelpText = "Apikey of the Lidarr instance to import from")] + public string ApiKey { get; set; } + + [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/Lidarr/LidarrV1Proxy.cs b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrV1Proxy.cs new file mode 100644 index 000000000..6d4459a62 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Lidarr/LidarrV1Proxy.cs @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Net; +using FluentValidation.Results; +using Newtonsoft.Json; +using NLog; +using NzbDrone.Common.Extensions; +using NzbDrone.Common.Http; + +namespace NzbDrone.Core.ImportLists.Lidarr +{ + public interface ILidarrV1Proxy + { + List GetArtists(LidarrSettings settings); + List GetProfiles(LidarrSettings settings); + List GetTags(LidarrSettings settings); + ValidationFailure Test(LidarrSettings settings); + } + + public class LidarrV1Proxy : ILidarrV1Proxy + { + private readonly IHttpClient _httpClient; + private readonly Logger _logger; + + public LidarrV1Proxy(IHttpClient httpClient, Logger logger) + { + _httpClient = httpClient; + _logger = logger; + } + + public List GetArtists(LidarrSettings settings) + { + return Execute("/api/v1/artist", settings); + } + + public List GetProfiles(LidarrSettings settings) + { + return Execute("/api/v1/qualityprofile", settings); + } + + public List GetTags(LidarrSettings settings) + { + return Execute("/api/v1/tag", settings); + } + + public ValidationFailure Test(LidarrSettings settings) + { + try + { + GetArtists(settings); + } + catch (HttpException ex) + { + if (ex.Response.StatusCode == HttpStatusCode.Unauthorized) + { + _logger.Error(ex, "API Key is invalid"); + return new ValidationFailure("ApiKey", "API Key is invalid"); + } + + _logger.Error(ex, "Unable to send test message"); + return new ValidationFailure("ApiKey", "Unable to send test message"); + } + catch (Exception ex) + { + _logger.Error(ex, "Unable to send test message"); + return new ValidationFailure("", "Unable to send test message"); + } + + return null; + } + + private List Execute(string resource, LidarrSettings settings) + { + if (settings.BaseUrl.IsNullOrWhiteSpace() || settings.ApiKey.IsNullOrWhiteSpace()) + { + return new List(); + } + + var baseUrl = settings.BaseUrl.TrimEnd('/'); + + var request = new HttpRequestBuilder(baseUrl).Resource(resource).Accept(HttpAccept.Json) + .SetHeader("X-Api-Key", settings.ApiKey).Build(); + + var response = _httpClient.Get(request); + + var results = JsonConvert.DeserializeObject>(response.Content); + + return results; + } + } +}