@ -9,21 +9,79 @@ namespace NzbDrone.Core.Indexers.Newznab
{
{
public class NewznabRequestGenerator : IIndexerRequestGenerator
public class NewznabRequestGenerator : IIndexerRequestGenerator
{
{
public Int32 MaxPages { get ; set ; }
private readonly INewznabCapabilitiesProvider _capabilitiesProvider ;
public Int32 PageSize { get ; set ; }
public int MaxPages { get ; set ; }
public int PageSize { get ; set ; }
public NewznabSettings Settings { get ; set ; }
public NewznabSettings Settings { get ; set ; }
public NewznabRequestGenerator ( )
public NewznabRequestGenerator ( INewznabCapabilitiesProvider capabilitiesProvider )
{
{
_capabilitiesProvider = capabilitiesProvider ;
MaxPages = 30 ;
MaxPages = 30 ;
PageSize = 100 ;
PageSize = 100 ;
}
}
private bool SupportsSearch
{
get
{
var capabilities = _capabilitiesProvider . GetCapabilities ( Settings ) ;
return capabilities . SupportedSearchParameters ! = null & &
capabilities . SupportedSearchParameters . Contains ( "q" ) ;
}
}
private bool SupportsTvSearch
{
get
{
var capabilities = _capabilitiesProvider . GetCapabilities ( Settings ) ;
return capabilities . SupportedTvSearchParameters ! = null & &
capabilities . SupportedTvSearchParameters . Contains ( "q" ) & &
capabilities . SupportedTvSearchParameters . Contains ( "season" ) & &
capabilities . SupportedTvSearchParameters . Contains ( "ep" ) ;
}
}
private bool SupportsTvdbSearch
{
get
{
var capabilities = _capabilitiesProvider . GetCapabilities ( Settings ) ;
return capabilities . SupportedTvSearchParameters ! = null & &
capabilities . SupportedTvSearchParameters . Contains ( "tvdbid" ) & &
capabilities . SupportedTvSearchParameters . Contains ( "season" ) & &
capabilities . SupportedTvSearchParameters . Contains ( "ep" ) ;
}
}
private bool SupportsTvRageSearch
{
get
{
var capabilities = _capabilitiesProvider . GetCapabilities ( Settings ) ;
return capabilities . SupportedTvSearchParameters ! = null & &
capabilities . SupportedTvSearchParameters . Contains ( "rid" ) & &
capabilities . SupportedTvSearchParameters . Contains ( "season" ) & &
capabilities . SupportedTvSearchParameters . Contains ( "ep" ) ;
}
}
public virtual IList < IEnumerable < IndexerRequest > > GetRecentRequests ( )
public virtual IList < IEnumerable < IndexerRequest > > GetRecentRequests ( )
{
{
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories . Concat ( Settings . AnimeCategories ) , "tvsearch" , "" ) ) ;
var capabilities = _capabilitiesProvider . GetCapabilities ( Settings ) ;
if ( capabilities . SupportedTvSearchParameters ! = null )
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories . Concat ( Settings . AnimeCategories ) , "tvsearch" , "" ) ) ;
}
return pageableRequests ;
return pageableRequests ;
}
}
@ -32,20 +90,28 @@ namespace NzbDrone.Core.Indexers.Newznab
{
{
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
if ( searchCriteria . Series . Tv RageId > 0 )
if ( searchCriteria . Series . Tv dbId > 0 & & SupportsTvdbSearch )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
String . Format ( "&rid={0}&season={1}&ep={2}" ,
string . Format ( "&tvdbid={0}&season={1}&ep={2}" ,
searchCriteria . Series . TvdbId ,
searchCriteria . SeasonNumber ,
searchCriteria . EpisodeNumber ) ) ) ;
}
else if ( searchCriteria . Series . TvRageId > 0 & & SupportsTvRageSearch )
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
string . Format ( "&rid={0}&season={1}&ep={2}" ,
searchCriteria . Series . TvRageId ,
searchCriteria . Series . TvRageId ,
searchCriteria . SeasonNumber ,
searchCriteria . SeasonNumber ,
searchCriteria . EpisodeNumber ) ) ) ;
searchCriteria . EpisodeNumber ) ) ) ;
}
}
else
else if ( SupportsTvSearch )
{
{
foreach ( var queryTitle in searchCriteria . QueryTitles )
foreach ( var queryTitle in searchCriteria . QueryTitles )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
S tring. Format ( "&q={0}&season={1}&ep={2}" ,
s tring. Format ( "&q={0}&season={1}&ep={2}" ,
NewsnabifyTitle ( queryTitle ) ,
NewsnabifyTitle ( queryTitle ) ,
searchCriteria . SeasonNumber ,
searchCriteria . SeasonNumber ,
searchCriteria . EpisodeNumber ) ) ) ;
searchCriteria . EpisodeNumber ) ) ) ;
@ -59,19 +125,26 @@ namespace NzbDrone.Core.Indexers.Newznab
{
{
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
if ( searchCriteria . Series . TvRageId > 0 )
if ( searchCriteria . Series . TvdbId > 0 & & SupportsTvdbSearch )
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
string . Format ( "&tvdbid={0}&season={1}" ,
searchCriteria . Series . TvdbId ,
searchCriteria . SeasonNumber ) ) ) ;
}
else if ( searchCriteria . Series . TvRageId > 0 & & SupportsTvRageSearch )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
String . Format ( "&rid={0}&season={1}" ,
s tring. Format ( "&rid={0}&season={1}" ,
searchCriteria . Series . TvRageId ,
searchCriteria . Series . TvRageId ,
searchCriteria . SeasonNumber ) ) ) ;
searchCriteria . SeasonNumber ) ) ) ;
}
}
else
else if ( SupportsTvSearch )
{
{
foreach ( var queryTitle in searchCriteria . QueryTitles )
foreach ( var queryTitle in searchCriteria . QueryTitles )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
S tring. Format ( "&q={0}&season={1}" ,
s tring. Format ( "&q={0}&season={1}" ,
NewsnabifyTitle ( queryTitle ) ,
NewsnabifyTitle ( queryTitle ) ,
searchCriteria . SeasonNumber ) ) ) ;
searchCriteria . SeasonNumber ) ) ) ;
}
}
@ -84,19 +157,26 @@ namespace NzbDrone.Core.Indexers.Newznab
{
{
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
if ( searchCriteria . Series . TvRageId > 0 )
if ( searchCriteria . Series . TvdbId > 0 & & SupportsTvdbSearch )
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
string . Format ( "&tvdbid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}" ,
searchCriteria . Series . TvdbId ,
searchCriteria . AirDate ) ) ) ;
}
else if ( searchCriteria . Series . TvRageId > 0 & & SupportsTvRageSearch )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
String . Format ( "&rid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}" ,
s tring. Format ( "&rid={0}&season={1:yyyy}&ep={1:MM}/{1:dd}" ,
searchCriteria . Series . TvRageId ,
searchCriteria . Series . TvRageId ,
searchCriteria . AirDate ) ) ) ;
searchCriteria . AirDate ) ) ) ;
}
}
else
else if ( SupportsTvSearch )
{
{
foreach ( var queryTitle in searchCriteria . QueryTitles )
foreach ( var queryTitle in searchCriteria . QueryTitles )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories , "tvsearch" ,
S tring. Format ( "&q={0}&season={1:yyyy}&ep={1:MM}/{1:dd}" ,
s tring. Format ( "&q={0}&season={1:yyyy}&ep={1:MM}/{1:dd}" ,
NewsnabifyTitle ( queryTitle ) ,
NewsnabifyTitle ( queryTitle ) ,
searchCriteria . AirDate ) ) ) ;
searchCriteria . AirDate ) ) ) ;
}
}
@ -109,12 +189,15 @@ namespace NzbDrone.Core.Indexers.Newznab
{
{
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
foreach ( var queryTitle in searchCriteria . QueryTitles )
if ( SupportsSearch )
{
{
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . AnimeCategories , "search" ,
foreach ( var queryTitle in searchCriteria . QueryTitles )
String . Format ( "&q={0}+{1:00}" ,
{
NewsnabifyTitle ( queryTitle ) ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . AnimeCategories , "search" ,
searchCriteria . AbsoluteEpisodeNumber ) ) ) ;
string . Format ( "&q={0}+{1:00}" ,
NewsnabifyTitle ( queryTitle ) ,
searchCriteria . AbsoluteEpisodeNumber ) ) ) ;
}
}
}
return pageableRequests ;
return pageableRequests ;
@ -124,29 +207,32 @@ namespace NzbDrone.Core.Indexers.Newznab
{
{
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
var pageableRequests = new List < IEnumerable < IndexerRequest > > ( ) ;
foreach ( var queryTitle in searchCriteria . EpisodeQueryTitles )
if ( SupportsSearch )
{
{
var query = queryTitle . Replace ( '+' , ' ' ) ;
foreach ( var queryTitle in searchCriteria . EpisodeQueryTitles )
query = System . Web . HttpUtility . UrlEncode ( query ) ;
{
var query = queryTitle . Replace ( '+' , ' ' ) ;
query = System . Web . HttpUtility . UrlEncode ( query ) ;
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories . Concat ( Settings . AnimeCategories ) , "search" ,
pageableRequests . AddIfNotNull ( GetPagedRequests ( MaxPages , Settings . Categories . Concat ( Settings . AnimeCategories ) , "search" ,
String . Format ( "&q={0}" ,
string . Format ( "&q={0}" ,
query ) ) ) ;
query ) ) ) ;
}
}
}
return pageableRequests ;
return pageableRequests ;
}
}
private IEnumerable < IndexerRequest > GetPagedRequests ( Int32 maxPages , IEnumerable < Int32 > categories , String searchType , S tring parameters )
private IEnumerable < IndexerRequest > GetPagedRequests ( int maxPages , IEnumerable < int > categories , string searchType , s tring parameters )
{
{
if ( categories . Empty ( ) )
if ( categories . Empty ( ) )
{
{
yield break ;
yield break ;
}
}
var categoriesQuery = S tring. Join ( "," , categories . Distinct ( ) ) ;
var categoriesQuery = s tring. Join ( "," , categories . Distinct ( ) ) ;
var baseUrl = S tring. Format ( "{0}/api?t={1}&cat={2}&extended=1{3}" , Settings . Url . TrimEnd ( '/' ) , searchType , categoriesQuery , Settings . AdditionalParameters ) ;
var baseUrl = s tring. Format ( "{0}/api?t={1}&cat={2}&extended=1{3}" , Settings . Url . TrimEnd ( '/' ) , searchType , categoriesQuery , Settings . AdditionalParameters ) ;
if ( Settings . ApiKey . IsNotNullOrWhiteSpace ( ) )
if ( Settings . ApiKey . IsNotNullOrWhiteSpace ( ) )
{
{
@ -155,18 +241,18 @@ namespace NzbDrone.Core.Indexers.Newznab
if ( PageSize = = 0 )
if ( PageSize = = 0 )
{
{
yield return new IndexerRequest ( S tring. Format ( "{0}{1}" , baseUrl , parameters ) , HttpAccept . Rss ) ;
yield return new IndexerRequest ( s tring. Format ( "{0}{1}" , baseUrl , parameters ) , HttpAccept . Rss ) ;
}
}
else
else
{
{
for ( var page = 0 ; page < maxPages ; page + + )
for ( var page = 0 ; page < maxPages ; page + + )
{
{
yield return new IndexerRequest ( S tring. Format ( "{0}&offset={1}&limit={2}{3}" , baseUrl , page * PageSize , PageSize , parameters ) , HttpAccept . Rss ) ;
yield return new IndexerRequest ( s tring. Format ( "{0}&offset={1}&limit={2}{3}" , baseUrl , page * PageSize , PageSize , parameters ) , HttpAccept . Rss ) ;
}
}
}
}
}
}
private static String NewsnabifyTitle ( S tring title )
private static string NewsnabifyTitle ( s tring title )
{
{
return title . Replace ( "+" , "%20" ) ;
return title . Replace ( "+" , "%20" ) ;
}
}