Lidarr/src/NzbDrone.Core/Indexers/Newznab/NewznabSettings.cs

90 lines
3.3 KiB
C#
Raw Normal View History

2013-07-31 03:41:46 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
2013-08-22 08:08:43 +00:00
using FluentValidation;
using NzbDrone.Common.Extensions;
2013-05-03 05:24:52 +00:00
using NzbDrone.Core.Annotations;
2013-08-22 08:08:43 +00:00
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Indexers.Newznab
{
2013-08-26 04:01:03 +00:00
public class NewznabSettingsValidator : AbstractValidator<NewznabSettings>
{
private static readonly string[] ApiKeyWhiteList =
{
"nzbs.org",
"nzb.su",
"dognzb.cr",
"nzbplanet.net",
"nzbid.org",
"nzbndx.com",
"nzbindex.in"
};
private static bool ShouldHaveApiKey(NewznabSettings settings)
{
2017-10-08 03:54:13 +00:00
if (settings.BaseUrl == null)
{
return false;
}
2017-10-08 03:54:13 +00:00
return ApiKeyWhiteList.Any(c => settings.BaseUrl.ToLowerInvariant().Contains(c));
}
private static readonly Regex AdditionalParametersRegex = new Regex(@"(&.+?\=.+?)+", RegexOptions.Compiled);
2013-08-26 04:01:03 +00:00
public NewznabSettingsValidator()
{
RuleFor(c => c).Custom((c, context) =>
{
if (c.Categories.Empty())
{
context.AddFailure("'Categories' must be provided");
}
});
2017-10-08 03:54:13 +00:00
RuleFor(c => c.BaseUrl).ValidRootUrl();
2017-10-26 03:08:37 +00:00
RuleFor(c => c.ApiPath).ValidUrlBase("/api");
RuleFor(c => c.ApiKey).NotEmpty().When(ShouldHaveApiKey);
RuleFor(c => c.AdditionalParameters).Matches(AdditionalParametersRegex)
.When(c => !c.AdditionalParameters.IsNullOrWhiteSpace());
2013-08-26 04:01:03 +00:00
}
}
2017-10-08 03:54:13 +00:00
public class NewznabSettings : IIndexerSettings
{
2013-08-26 04:01:03 +00:00
private static readonly NewznabSettingsValidator Validator = new NewznabSettingsValidator();
2013-07-31 03:41:46 +00:00
public NewznabSettings()
{
2017-10-26 03:08:37 +00:00
ApiPath = "/api";
Categories = new[] { 3000, 3010, 3030, 3040 };
2013-07-31 03:41:46 +00:00
}
2013-05-29 07:06:25 +00:00
[FieldDefinition(0, Label = "URL")]
2017-10-08 03:54:13 +00:00
public string BaseUrl { get; set; }
2013-05-03 05:24:52 +00:00
2017-10-26 03:08:37 +00:00
[FieldDefinition(1, Label = "API Path", HelpText = "Path to the api, usually /api", Advanced = true)]
public string ApiPath { get; set; }
[FieldDefinition(2, Label = "API Key", Privacy = PrivacyLevel.ApiKey)]
public string ApiKey { get; set; }
[FieldDefinition(3, Label = "Categories", Type = FieldType.Select, SelectOptionsProviderAction = "newznabCategories", HelpText = "Drop down list; at least one category must be selected.")]
public IEnumerable<int> Categories { get; set; }
2013-07-31 03:41:46 +00:00
[FieldDefinition(4, Type = FieldType.Number, Label = "Early Download Limit", HelpText = "Time before release date Lidarr will download from this indexer, empty is no limit", Unit = "days", Advanced = true)]
public int? EarlyReleaseLimit { get; set; }
[FieldDefinition(5, Label = "Additional Parameters", HelpText = "Additional Newznab parameters", Advanced = true)]
public string AdditionalParameters { get; set; }
// Field 6 is used by TorznabSettings MinimumSeeders
// If you need to add another field here, update TorznabSettings as well and this comment
2016-02-11 20:49:22 +00:00
public virtual NzbDroneValidationResult Validate()
2013-08-22 08:08:43 +00:00
{
return new NzbDroneValidationResult(Validator.Validate(this));
2013-08-22 08:08:43 +00:00
}
}
}