parent
9eafb8fe8c
commit
ada650b97c
@ -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<MediaCover.MediaCover> Images { get; set; }
|
||||
public bool Monitored { get; set; }
|
||||
public int QualityProfileId { get; set; }
|
||||
public HashSet<int> 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; }
|
||||
}
|
||||
}
|
@ -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<LidarrSettings>
|
||||
{
|
||||
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<ImportListItemInfo> Fetch()
|
||||
{
|
||||
var artists = new List<ImportListItemInfo>();
|
||||
|
||||
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<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 = _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<ValidationFailure> failures)
|
||||
{
|
||||
failures.AddIfNotNull(_lidarrV1Proxy.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.Lidarr
|
||||
{
|
||||
public class LidarrSettingsValidator : AbstractValidator<LidarrSettings>
|
||||
{
|
||||
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<int>();
|
||||
TagIds = Array.Empty<int>();
|
||||
}
|
||||
|
||||
[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<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.Lidarr
|
||||
{
|
||||
public interface ILidarrV1Proxy
|
||||
{
|
||||
List<LidarrArtist> GetArtists(LidarrSettings settings);
|
||||
List<LidarrProfile> GetProfiles(LidarrSettings settings);
|
||||
List<LidarrTag> 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<LidarrArtist> GetArtists(LidarrSettings settings)
|
||||
{
|
||||
return Execute<LidarrArtist>("/api/v1/artist", settings);
|
||||
}
|
||||
|
||||
public List<LidarrProfile> GetProfiles(LidarrSettings settings)
|
||||
{
|
||||
return Execute<LidarrProfile>("/api/v1/qualityprofile", settings);
|
||||
}
|
||||
|
||||
public List<LidarrTag> GetTags(LidarrSettings settings)
|
||||
{
|
||||
return Execute<LidarrTag>("/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<TResource> Execute<TResource>(string resource, LidarrSettings 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