parent
9a78c8b512
commit
907b7dc429
@ -0,0 +1,27 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NzbDrone.Core.ImportLists.Readarr
|
||||
{
|
||||
public class ReadarrAuthor
|
||||
{
|
||||
public string AuthorName { get; set; }
|
||||
public string ForeignAuthorId { get; set; }
|
||||
public string Overview { get; set; }
|
||||
public List<MediaCover.MediaCover> Images { get; set; }
|
||||
public bool Monitored { get; set; }
|
||||
public int QualityProfileId { get; set; }
|
||||
public HashSet<int> Tags { get; set; }
|
||||
}
|
||||
|
||||
public class ReadarrProfile
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class ReadarrTag
|
||||
{
|
||||
public string Label { get; set; }
|
||||
public int Id { get; set; }
|
||||
}
|
||||
}
|
@ -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.Readarr
|
||||
{
|
||||
public class ReadarrImport : ImportListBase<ReadarrSettings>
|
||||
{
|
||||
private readonly IReadarrV1Proxy _readarrV1Proxy;
|
||||
public override string Name => "Readarr";
|
||||
|
||||
public override ImportListType ListType => ImportListType.Program;
|
||||
|
||||
public ReadarrImport(IReadarrV1Proxy readarrV1Proxy,
|
||||
IImportListStatusService importListStatusService,
|
||||
IConfigService configService,
|
||||
IParsingService parsingService,
|
||||
Logger logger)
|
||||
: base(importListStatusService, configService, parsingService, logger)
|
||||
{
|
||||
_readarrV1Proxy = readarrV1Proxy;
|
||||
}
|
||||
|
||||
public override IList<ImportListItemInfo> Fetch()
|
||||
{
|
||||
var authors = new List<ImportListItemInfo>();
|
||||
|
||||
try
|
||||
{
|
||||
var remoteAuthors = _readarrV1Proxy.GetAuthors(Settings);
|
||||
|
||||
foreach (var remoteAuthor in remoteAuthors)
|
||||
{
|
||||
if ((!Settings.ProfileIds.Any() || Settings.ProfileIds.Contains(remoteAuthor.QualityProfileId)) &&
|
||||
(!Settings.TagIds.Any() || Settings.TagIds.Any(x => remoteAuthor.Tags.Any(y => y == x))))
|
||||
{
|
||||
authors.Add(new ImportListItemInfo
|
||||
{
|
||||
AuthorGoodreadsId = remoteAuthor.ForeignAuthorId,
|
||||
Author = remoteAuthor.AuthorName
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_importListStatusService.RecordSuccess(Definition.Id);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_importListStatusService.RecordFailure(Definition.Id);
|
||||
}
|
||||
|
||||
return CleanupListItems(authors);
|
||||
}
|
||||
|
||||
public override object RequestAction(string action, IDictionary<string, string> query)
|
||||
{
|
||||
// Return early if there is not an API key
|
||||
if (Settings.ApiKey.IsNullOrWhiteSpace())
|
||||
{
|
||||
return new
|
||||
{
|
||||
devices = new List<object>()
|
||||
};
|
||||
}
|
||||
|
||||
Settings.Validate().Filter("ApiKey").ThrowOnError();
|
||||
|
||||
if (action == "getProfiles")
|
||||
{
|
||||
var devices = _readarrV1Proxy.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 = _readarrV1Proxy.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<ValidationFailure> failures)
|
||||
{
|
||||
failures.AddIfNotNull(_readarrV1Proxy.Test(Settings));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FluentValidation;
|
||||
using NzbDrone.Core.Annotations;
|
||||
using NzbDrone.Core.Validation;
|
||||
|
||||
namespace NzbDrone.Core.ImportLists.Readarr
|
||||
{
|
||||
public class ReadarrSettingsValidator : AbstractValidator<ReadarrSettings>
|
||||
{
|
||||
public ReadarrSettingsValidator()
|
||||
{
|
||||
RuleFor(c => c.BaseUrl).ValidRootUrl();
|
||||
RuleFor(c => c.ApiKey).NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
public class ReadarrSettings : IImportListSettings
|
||||
{
|
||||
private static readonly ReadarrSettingsValidator Validator = new ReadarrSettingsValidator();
|
||||
|
||||
public ReadarrSettings()
|
||||
{
|
||||
BaseUrl = "";
|
||||
ApiKey = "";
|
||||
ProfileIds = Array.Empty<int>();
|
||||
TagIds = Array.Empty<int>();
|
||||
}
|
||||
|
||||
[FieldDefinition(0, Label = "Full URL", HelpText = "URL, including port, of the Readarr instance to import from")]
|
||||
public string BaseUrl { get; set; }
|
||||
|
||||
[FieldDefinition(1, Label = "API Key", Privacy = PrivacyLevel.ApiKey, HelpText = "Apikey of the Readarr 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<int> ProfileIds { get; set; }
|
||||
|
||||
[FieldDefinition(3, Type = FieldType.Select, SelectOptionsProviderAction = "getTags", Label = "Tags", HelpText = "Tags from the source instance to import from")]
|
||||
public IEnumerable<int> TagIds { get; set; }
|
||||
|
||||
public NzbDroneValidationResult Validate()
|
||||
{
|
||||
return new NzbDroneValidationResult(Validator.Validate(this));
|
||||
}
|
||||
}
|
||||
}
|
@ -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.Readarr
|
||||
{
|
||||
public interface IReadarrV1Proxy
|
||||
{
|
||||
List<ReadarrAuthor> GetAuthors(ReadarrSettings settings);
|
||||
List<ReadarrProfile> GetProfiles(ReadarrSettings settings);
|
||||
List<ReadarrTag> GetTags(ReadarrSettings settings);
|
||||
ValidationFailure Test(ReadarrSettings settings);
|
||||
}
|
||||
|
||||
public class ReadarrV1Proxy : IReadarrV1Proxy
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public ReadarrV1Proxy(IHttpClient httpClient, Logger logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public List<ReadarrAuthor> GetAuthors(ReadarrSettings settings)
|
||||
{
|
||||
return Execute<ReadarrAuthor>("/api/v1/author", settings);
|
||||
}
|
||||
|
||||
public List<ReadarrProfile> GetProfiles(ReadarrSettings settings)
|
||||
{
|
||||
return Execute<ReadarrProfile>("/api/v1/qualityprofile", settings);
|
||||
}
|
||||
|
||||
public List<ReadarrTag> GetTags(ReadarrSettings settings)
|
||||
{
|
||||
return Execute<ReadarrTag>("/api/v1/tag", settings);
|
||||
}
|
||||
|
||||
public ValidationFailure Test(ReadarrSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
GetAuthors(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<TResource> Execute<TResource>(string resource, ReadarrSettings settings)
|
||||
{
|
||||
if (settings.BaseUrl.IsNullOrWhiteSpace() || settings.ApiKey.IsNullOrWhiteSpace())
|
||||
{
|
||||
return new List<TResource>();
|
||||
}
|
||||
|
||||
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<List<TResource>>(response.Content);
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue