New: Custom import lists

Co-authored-by: TheUltimateCoder <no@e.mail>
This commit is contained in:
Ultimate 2022-10-03 19:15:45 +02:00 committed by GitHub
parent 2a7e2674b7
commit 092186dab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 188 additions and 1 deletions

View File

@ -0,0 +1,7 @@
namespace NzbDrone.Core.ImportLists.Custom
{
public class CustomSeries
{
public int TvdbId { get; set; }
}
}

View File

@ -0,0 +1,67 @@
using System.Collections.Generic;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.ImportLists.Custom
{
public class CustomImport : ImportListBase<CustomSettings>
{
private readonly ICustomProxy _customProxy;
public override string Name => "Custom List";
public override ImportListType ListType => ImportListType.Advanced;
public CustomImport(ICustomProxy customProxy,
IImportListStatusService importListStatusService,
IConfigService configService,
IParsingService parsingService,
Logger logger)
: base(importListStatusService, configService, parsingService, logger)
{
_customProxy = customProxy;
}
public override IList<ImportListItemInfo> Fetch()
{
var series = new List<ImportListItemInfo>();
try
{
var remoteSeries = _customProxy.GetSeries(Settings);
foreach (var item in remoteSeries)
{
series.Add(new ImportListItemInfo
{
TvdbId = item.TvdbId
});
}
_importListStatusService.RecordSuccess(Definition.Id);
}
catch
{
_importListStatusService.RecordFailure(Definition.Id);
}
return CleanupListItems(series);
}
public override object RequestAction(string action, IDictionary<string, string> query)
{
return new { };
}
protected override void Test(List<ValidationFailure> failures)
{
failures.AddIfNotNull(_customProxy.Test(Settings));
}
}
}

View File

@ -0,0 +1,79 @@
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.Custom
{
public interface ICustomImportProxy
{
List<CustomSeries> GetSeries(CustomSettings settings);
ValidationFailure Test(CustomSettings settings);
}
public class CustomImportProxy : ICustomImportProxy
{
private readonly IHttpClient _httpClient;
private readonly Logger _logger;
public CustomImportProxy(IHttpClient httpClient, Logger logger)
{
_httpClient = httpClient;
_logger = logger;
}
public List<CustomSeries> GetSeries(CustomSettings settings)
{
return Execute<CustomSeries>(settings);
}
public ValidationFailure Test(CustomSettings settings)
{
try
{
GetSeries(settings);
}
catch (HttpException ex)
{
if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
{
_logger.Error(ex, "There was an authorization issue. We cannot get the list from the provider.");
return new ValidationFailure("BaseUrl", "It seems we are unauthorized to make this request.");
}
_logger.Error(ex, "Unable to send test message");
return new ValidationFailure("BaseUrl", $"We are unable to make the request to that URL. StatusCode: {ex.Response.StatusCode}");
}
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>(CustomSettings settings)
{
if (settings.BaseUrl.IsNullOrWhiteSpace())
{
return new List<TResource>();
}
var baseUrl = settings.BaseUrl.TrimEnd('/');
var request = new HttpRequestBuilder(baseUrl).Accept(HttpAccept.Json).Build();
var response = _httpClient.Get(request);
var results = JsonConvert.DeserializeObject<List<TResource>>(response.Content);
return results;
}
}
}

View File

@ -0,0 +1,33 @@
using FluentValidation;
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.ImportLists.Custom
{
public class CustomSettingsValidator : AbstractValidator<CustomSettings>
{
public CustomSettingsValidator()
{
RuleFor(c => c.BaseUrl).ValidRootUrl();
}
}
public class CustomSettings : IImportListSettings
{
private static readonly CustomSettingsValidator Validator = new CustomSettingsValidator();
public CustomSettings()
{
BaseUrl = "";
}
[FieldDefinition(0, Label = "List URL", HelpText = "The URL for the series list")]
public string BaseUrl { get; set; }
public NzbDroneValidationResult Validate()
{
return new NzbDroneValidationResult(Validator.Validate(this));
}
}
}

View File

@ -5,6 +5,7 @@ namespace NzbDrone.Core.ImportLists
Program,
Plex,
Trakt,
Other
Other,
Advanced
}
}