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

107 lines
4.4 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
2013-08-22 08:08:43 +00:00
using FluentValidation;
using FluentValidation.Results;
using NzbDrone.Common.Extensions;
2013-05-03 05:24:52 +00:00
using NzbDrone.Core.Annotations;
using NzbDrone.Core.Parser;
using NzbDrone.Core.ThingiProvider;
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)
{
if (settings.BaseUrl == null)
{
return false;
}
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() && c.AnimeCategories.Empty())
{
context.AddFailure("Either 'Categories' or 'Anime Categories' must be provided");
}
});
RuleFor(c => c.BaseUrl).ValidRootUrl();
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
}
}
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()
{
ApiPath = "/api";
Categories = new[] { 2000, 2010, 2020, 2030, 2035, 2040, 2045, 2050, 2060 };
AnimeCategories = Enumerable.Empty<int>();
MultiLanguages = Enumerable.Empty<int>();
2013-07-31 03:41:46 +00:00
}
2013-05-29 07:06:25 +00:00
[FieldDefinition(0, Label = "URL")]
public string BaseUrl { get; set; }
[FieldDefinition(1, Label = "API Path", HelpText = "Path to the api, usually /api", Advanced = true)]
public string ApiPath { get; set; }
// [FieldDefinition(1, Type = FieldType.Tag, SelectOptions = typeof(Language), Label = "Multi Languages", HelpText = "What languages are normally in a multi release on this indexer?", Advanced = true)]
public IEnumerable<int> MultiLanguages { get; set; }
2013-05-03 05:24:52 +00:00
[FieldDefinition(2, Label = "API Key")]
public string ApiKey { get; set; }
[FieldDefinition(3, Label = "Categories", HelpText = "Comma Separated list, leave blank to disable all categories", Advanced = true)]
public IEnumerable<int> Categories { get; set; }
2013-07-31 03:41:46 +00:00
[FieldDefinition(4, Label = "Anime Categories", HelpText = "Comma Separated list, leave blank to disable anime", Advanced = true)]
public IEnumerable<int> AnimeCategories { get; set; }
[FieldDefinition(5, Label = "Additional Parameters", HelpText = "Additional Newznab parameters", Advanced = true)]
public string AdditionalParameters { get; set; }
[FieldDefinition(6, Label = "Remove year from search string",
HelpText = "Should Radarr remove the year after the title when searching this indexer?", Advanced = true, Type = FieldType.Checkbox)]
public bool RemoveYear { get; set; }
[FieldDefinition(7, Label = "Search by Title",
HelpText = "By default, Radarr will try to search by IMDB ID if your indexer supports that. However, some indexers are not very good at tagging their releases correctly, so you can force Radarr to search that indexer by title instead.",
Advanced = true, Type = FieldType.Checkbox)]
public bool SearchByTitle { get; set; }
// Field 8 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
}
}
}