2013-07-31 03:41:46 +00:00
using System.Collections.Generic ;
2013-12-01 21:33:53 +00:00
using System.Linq ;
2014-05-19 19:14:41 +00:00
using System.Text.RegularExpressions ;
2013-08-22 08:08:43 +00:00
using FluentValidation ;
2014-12-02 06:26:25 +00:00
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 ;
2013-05-02 01:59:09 +00:00
namespace NzbDrone.Core.Indexers.Newznab
{
2013-08-26 04:01:03 +00:00
public class NewznabSettingsValidator : AbstractValidator < NewznabSettings >
{
2013-12-01 21:33:53 +00:00
private static readonly string [ ] ApiKeyWhiteList =
{
"nzbs.org" ,
"nzb.su" ,
"dognzb.cr" ,
"nzbplanet.net" ,
"nzbid.org" ,
"nzbndx.com" ,
2013-12-02 22:41:19 +00:00
"nzbindex.in"
2013-12-01 21:33:53 +00:00
} ;
private static bool ShouldHaveApiKey ( NewznabSettings settings )
{
2017-10-08 03:54:13 +00:00
if ( settings . BaseUrl = = null )
2013-12-02 22:11:17 +00:00
{
return false ;
}
2017-10-08 03:54:13 +00:00
return ApiKeyWhiteList . Any ( c = > settings . BaseUrl . ToLowerInvariant ( ) . Contains ( c ) ) ;
2013-12-01 21:33:53 +00:00
}
2014-05-19 19:14:41 +00:00
private static readonly Regex AdditionalParametersRegex = new Regex ( @"(&.+?\=.+?)+" , RegexOptions . Compiled ) ;
2013-08-26 04:01:03 +00:00
public NewznabSettingsValidator ( )
{
2019-09-12 01:26:54 +00:00
RuleFor ( c = > c ) . Custom ( ( c , context ) = >
2015-05-31 07:35:25 +00:00
{
2019-09-12 01:26:54 +00:00
if ( c . Categories . Empty ( ) )
2015-05-31 07:35:25 +00:00
{
2019-09-12 01:26:54 +00:00
context . AddFailure ( "'Categories' must be provided" ) ;
2015-05-31 07:35:25 +00:00
}
} ) ;
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" ) ;
2013-12-01 21:33:53 +00:00
RuleFor ( c = > c . ApiKey ) . NotEmpty ( ) . When ( ShouldHaveApiKey ) ;
2015-05-31 07:35:25 +00:00
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-05-02 01:59:09 +00:00
{
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" ;
2020-10-08 21:33:13 +00:00
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 ; }
2020-09-22 20:31:58 +00:00
[FieldDefinition(2, Label = "API Key", Privacy = PrivacyLevel.ApiKey)]
2015-09-26 08:45:13 +00:00
public string ApiKey { get ; set ; }
2013-05-02 01:59:09 +00:00
2021-08-04 01:16:00 +00:00
[FieldDefinition(3, Label = "Categories", Type = FieldType.Select, SelectOptionsProviderAction = "newznabCategories", HelpText = "Drop down list; at least one category must be selected.")]
2015-09-26 08:45:13 +00:00
public IEnumerable < int > Categories { get ; set ; }
2013-07-31 03:41:46 +00:00
2018-09-21 02:21:26 +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)]
2015-09-26 08:45:13 +00:00
public string AdditionalParameters { get ; set ; }
2014-05-19 19:14:41 +00:00
2018-09-21 02:21:26 +00:00
// Field 6 is used by TorznabSettings MinimumSeeders
2017-10-26 02:30:31 +00:00
// 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
{
2015-03-17 04:33:34 +00:00
return new NzbDroneValidationResult ( Validator . Validate ( this ) ) ;
2013-08-22 08:08:43 +00:00
}
2013-05-02 01:59:09 +00:00
}
2017-09-23 02:27:26 +00:00
}