diff --git a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs new file mode 100644 index 000000000..628fd6180 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrAPIResource.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; + +namespace NzbDrone.Core.ImportLists.Sonarr +{ + public class SonarrSeries + { + public string Title { get; set; } + public string SortTitle { get; set; } + public int TvdbId { get; set; } + public string Overview { get; set; } + public List Images { get; set; } + public bool Monitored { get; set; } + public int Year { get; set; } + public string TitleSlug { get; set; } + public int QualityProfileId { get; set; } + } + + public class SonarrProfile + { + public string Name { 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 new file mode 100644 index 000000000..9a9f4a5c0 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrImport.cs @@ -0,0 +1,97 @@ +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.Validation; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.ImportLists.Sonarr +{ + public class SonarrImport : ImportListBase + { + private readonly ISonarrV3Proxy _sonarrV3Proxy; + public override string Name => "Sonarr"; + + public override ImportListType ListType => ImportListType.Program; + + public SonarrImport(ISonarrV3Proxy sonarrV3Proxy, + IImportListStatusService importListStatusService, + IConfigService configService, + IParsingService parsingService, + Logger logger) + : base(importListStatusService, configService, parsingService, logger) + { + _sonarrV3Proxy = sonarrV3Proxy; + } + + public override IList Fetch() + { + var series = new List(); + + try + { + var remoteSeries = _sonarrV3Proxy.GetSeries(Settings); + + foreach (var item in remoteSeries) + { + if (!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(item.QualityProfileId)) + { + series.Add(new ImportListItemInfo + { + TvdbId = item.TvdbId, + Title = item.Title + }); + } + } + + _importListStatusService.RecordSuccess(Definition.Id); + } + catch + { + _importListStatusService.RecordFailure(Definition.Id); + } + + return CleanupListItems(series); + } + + 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 new + { + devices = new List() + }; + } + + Settings.Validate().Filter("ApiKey").ThrowOnError(); + + var devices = _sonarrV3Proxy.GetProfiles(Settings); + + return new + { + options = devices.OrderBy(d => d.Name, StringComparer.InvariantCultureIgnoreCase) + .Select(d => new + { + id = d.Id, + name = d.Name + }) + }; + } + + return new { }; + } + + protected override void Test(List failures) + { + failures.AddIfNotNull(_sonarrV3Proxy.Test(Settings)); + } + } +} diff --git a/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs new file mode 100644 index 000000000..5af1c5b6c --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrSettings.cs @@ -0,0 +1,42 @@ +using System.Collections.Generic; +using FluentValidation; +using NzbDrone.Core.Annotations; +using NzbDrone.Core.Validation; + +namespace NzbDrone.Core.ImportLists.Sonarr +{ + public class SonarrSettingsValidator : AbstractValidator + { + public SonarrSettingsValidator() + { + RuleFor(c => c.BaseUrl).ValidRootUrl(); + RuleFor(c => c.ApiKey).NotEmpty(); + } + } + + public class SonarrSettings : IImportListSettings + { + private static readonly SonarrSettingsValidator Validator = new SonarrSettingsValidator(); + + public SonarrSettings() + { + BaseUrl = ""; + ApiKey = ""; + ProfileIds = new int[] { }; + } + + [FieldDefinition(0, Label = "Full URL", HelpText = "URL, including port, of the Sonarr V3 instance to import from")] + public string BaseUrl { get; set; } + + [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")] + public IEnumerable ProfileIds { 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 new file mode 100644 index 000000000..ce68abba2 --- /dev/null +++ b/src/NzbDrone.Core/ImportLists/Sonarr/SonarrV3Proxy.cs @@ -0,0 +1,85 @@ +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.Sonarr +{ + public interface ISonarrV3Proxy + { + List GetSeries(SonarrSettings settings); + List GetProfiles(SonarrSettings settings); + ValidationFailure Test(SonarrSettings settings); + } + + public class SonarrV3Proxy : ISonarrV3Proxy + { + private readonly IHttpClient _httpClient; + private readonly Logger _logger; + + public SonarrV3Proxy(IHttpClient httpClient, Logger logger) + { + _httpClient = httpClient; + _logger = logger; + } + + public List GetSeries(SonarrSettings settings) + { + return Execute("/api/v3/series", settings); + } + + public List GetProfiles(SonarrSettings settings) + { + return Execute("/api/v3/qualityprofile", settings); + } + + public ValidationFailure Test(SonarrSettings settings) + { + try + { + GetSeries(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, SonarrSettings 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; + } + } +}