2019-06-14 03:54:25 +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 ;
2020-04-11 04:26:09 +00:00
using NzbDrone.Core.Languages ;
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-06-12 12:47:05 +00:00
if ( settings . BaseUrl = = null )
2013-12-02 22:11:17 +00:00
{
return false ;
}
2017-06-12 12:47:05 +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-02 20:27:49 +00:00
RuleFor ( c = > c ) . Custom ( ( c , context ) = >
2015-05-31 07:35:25 +00:00
{
2020-06-20 19:55:31 +00:00
if ( c . Categories . Empty ( ) )
2015-05-31 07:35:25 +00:00
{
2020-06-20 19:55:31 +00:00
context . AddFailure ( "'Categories' must be provided" ) ;
2015-05-31 07:35:25 +00:00
}
} ) ;
2017-06-12 12:47:05 +00:00
RuleFor ( c = > c . BaseUrl ) . ValidRootUrl ( ) ;
2019-06-14 03:54:25 +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-06-12 12:47:05 +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 ( )
{
2019-06-14 03:54:25 +00:00
ApiPath = "/api" ;
2021-03-30 02:27:01 +00:00
Categories = new [ ] { 2000 , 2010 , 2020 , 2030 , 2040 , 2045 , 2050 , 2060 } ;
2019-10-14 21:47:23 +00:00
MultiLanguages = new List < int > ( ) ;
2013-07-31 03:41:46 +00:00
}
2013-05-29 07:06:25 +00:00
[FieldDefinition(0, Label = "URL")]
2017-06-12 12:47:05 +00:00
public string BaseUrl { get ; set ; }
2019-06-14 03:54:25 +00:00
[FieldDefinition(1, Label = "API Path", HelpText = "Path to the api, usually /api", Advanced = true)]
public string ApiPath { get ; set ; }
2020-12-20 15:21:51 +00:00
[FieldDefinition(1, Type = FieldType.Select, SelectOptions = typeof(RealLanguageFieldConverter), Label = "Multi Languages", HelpText = "What languages are normally in a multi release on this indexer?", Advanced = true)]
2018-08-05 14:28:05 +00:00
public IEnumerable < int > MultiLanguages { get ; set ; }
2013-05-03 05:24:52 +00:00
2020-10-17 03:27:01 +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-04-22 23:36:39 +00:00
[FieldDefinition(3, Label = "Categories", Type = FieldType.Select, SelectOptionsProviderAction = "newznabCategories", HelpText = "Drop down list, leave blank to disable all categories", Advanced = true)]
2015-09-26 08:45:13 +00:00
public IEnumerable < int > Categories { get ; set ; }
2013-07-31 03:41:46 +00:00
2018-08-05 14:28:05 +00:00
[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
2019-12-22 22:08:53 +00:00
[ 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 ) ]
2017-10-07 18:09:22 +00:00
public bool RemoveYear { get ; set ; }
2018-08-05 14:28:05 +00:00
// Field 8 is used by TorznabSettings MinimumSeeders
2017-06-12 12:47:05 +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
}
2019-06-14 03:54:25 +00:00
}