@ -1,8 +1,10 @@
using System ;
using System.Collections.Generic ;
using System.Collections.Specialized ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Http ;
using NzbDrone.Core.IndexerSearch.Definitions ;
using NzbDrone.Core.Parser ;
namespace NzbDrone.Core.Indexers.FileList
{
@ -13,108 +15,140 @@ namespace NzbDrone.Core.Indexers.FileList
public Func < IDictionary < string , string > > GetCookies { get ; set ; }
public Action < IDictionary < string , string > , DateTime ? > CookiesUpdater { get ; set ; }
public virtual IndexerPageableRequestChain GetSearchRequests ( Movie SearchCriteria searchCriteria )
public IndexerPageableRequestChain GetSearchRequests ( Tv SearchCriteria searchCriteria )
{
var pageableRequests = new IndexerPageableRequestChain ( ) ;
var parameters = GetDefaultParameters ( ) ;
if ( searchCriteria . ImdbId . IsNotNullOrWhiteSpace ( ) )
{
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=imdb&query={0}" , searchCriteria . FullImdbId ) ) ) ;
}
else if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
if ( searchCriteria . ImdbId . IsNotNullOrWhiteSpace ( ) | | searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
var titleYearSearchQuery = string . Format ( "{0}" , searchCriteria . SanitizedSearchTerm ) ;
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=name&query={0}" , titleYearSearchQuery . Trim ( ) ) ) ) ;
}
else
{
pageableRequests . Add ( GetRequest ( "latest-torrents" , searchCriteria . Categories , "" ) ) ;
parameters . Add ( "action" , "search-torrents" ) ;
if ( searchCriteria . ImdbId . IsNotNullOrWhiteSpace ( ) )
{
parameters . Add ( "type" , "imdb" ) ;
parameters . Add ( "query" , searchCriteria . FullImdbId ) ;
}
else if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
parameters . Add ( "type" , "name" ) ;
parameters . Add ( "query" , searchCriteria . SanitizedSearchTerm . Trim ( ) ) ;
}
if ( searchCriteria . Season . HasValue )
{
parameters . Add ( "season" , searchCriteria . Season . ToString ( ) ) ;
parameters . Add ( "episode" , searchCriteria . Episode ) ;
}
}
pageableRequests . Add ( GetRequest ( searchCriteria , parameters ) ) ;
return pageableRequests ;
}
public IndexerPageableRequestChain GetSearchRequests ( MusicSearchCriteria searchCriteria )
public virtual IndexerPageableRequestChain GetSearchRequests ( M ovie SearchCriteria searchCriteria )
{
var pageableRequests = new IndexerPageableRequestChain ( ) ;
if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
var parameters = GetDefaultParameters ( ) ;
if ( searchCriteria . ImdbId . IsNotNullOrWhiteSpace ( ) )
{
var titleYearSearchQuery = string . Format ( "{0}" , searchCriteria . SanitizedSearchTerm ) ;
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=name&query={0}" , titleYearSearchQuery . Trim ( ) ) ) ) ;
parameters . Add ( "action" , "search-torrents" ) ;
parameters . Add ( "type" , "imdb" ) ;
parameters . Add ( "query" , searchCriteria . FullImdbId ) ;
}
else
else if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
pageableRequests . Add ( GetRequest ( "latest-torrents" , searchCriteria . Categories , "" ) ) ;
parameters . Add ( "action" , "search-torrents" ) ;
parameters . Add ( "type" , "name" ) ;
parameters . Add ( "query" , searchCriteria . SanitizedSearchTerm . Trim ( ) ) ;
}
pageableRequests . Add ( GetRequest ( searchCriteria , parameters ) ) ;
return pageableRequests ;
}
public IndexerPageableRequestChain GetSearchRequests ( Tv SearchCriteria searchCriteria )
public IndexerPageableRequestChain GetSearchRequests ( Music SearchCriteria searchCriteria )
{
var pageableRequests = new IndexerPageableRequestChain ( ) ;
var parameters = GetDefaultParameters ( ) ;
if ( searchCriteria . ImdbId . IsNotNullOrWhiteSpace ( ) )
{
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=imdb&query={0}&season={1}&episode={2}" , searchCriteria . FullImdbId , searchCriteria . Season , searchCriteria . Episode ) ) ) ;
}
else if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
var titleYearSearchQuery = string . Format ( "{0}" , searchCriteria . SanitizedSearchTerm ) ;
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=name&query={0}&season={1}&episode={2}" , titleYearSearchQuery . Trim ( ) , searchCriteria . Season , searchCriteria . Episode ) ) ) ;
}
else
if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
pageableRequests . Add ( GetRequest ( "latest-torrents" , searchCriteria . Categories , "" ) ) ;
parameters . Add ( "action" , "search-torrents" ) ;
parameters . Add ( "type" , "name" ) ;
parameters . Add ( "query" , searchCriteria . SanitizedSearchTerm . Trim ( ) ) ;
}
pageableRequests . Add ( GetRequest ( searchCriteria , parameters ) ) ;
return pageableRequests ;
}
public IndexerPageableRequestChain GetSearchRequests ( BookSearchCriteria searchCriteria )
{
var pageableRequests = new IndexerPageableRequestChain ( ) ;
var parameters = GetDefaultParameters ( ) ;
if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
var titleYearSearchQuery = string . Format ( "{0}" , searchCriteria . SanitizedSearchTerm ) ;
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=name&query={0}" , titleYearSearchQuery . Trim ( ) ) ) ) ;
}
else
{
pageableRequests . Add ( GetRequest ( "latest-torrents" , searchCriteria . Categories , "" ) ) ;
parameters . Add ( "action" , "search-torrents" ) ;
parameters . Add ( "type" , "name" ) ;
parameters . Add ( "query" , searchCriteria . SanitizedSearchTerm . Trim ( ) ) ;
}
pageableRequests . Add ( GetRequest ( searchCriteria , parameters ) ) ;
return pageableRequests ;
}
public IndexerPageableRequestChain GetSearchRequests ( BasicSearchCriteria searchCriteria )
{
var pageableRequests = new IndexerPageableRequestChain ( ) ;
var parameters = GetDefaultParameters ( ) ;
if ( searchCriteria . SearchTerm . IsNotNullOrWhiteSpace ( ) )
{
var titleYearSearchQuery = string . Format ( "{0}" , searchCriteria . SanitizedSearchTerm ) ;
pageableRequests . Add ( GetRequest ( "search-torrents" , searchCriteria . Categories , string . Format ( "&type=name&query={0}" , titleYearSearchQuery . Trim ( ) ) ) ) ;
}
else
{
pageableRequests . Add ( GetRequest ( "latest-torrents" , searchCriteria . Categories , "" ) ) ;
parameters . Add ( "action" , "search-torrents" ) ;
parameters . Add ( "type" , "name" ) ;
parameters . Add ( "query" , searchCriteria . SanitizedSearchTerm . Trim ( ) ) ;
}
pageableRequests . Add ( GetRequest ( searchCriteria , parameters ) ) ;
return pageableRequests ;
}
private IEnumerable < IndexerRequest > GetRequest ( string searchType , int [ ] categories , string parameters )
private IEnumerable < IndexerRequest > GetRequest ( SearchCriteriaBase searchCriteria , NameValueCollection parameters )
{
var categoriesQuery = string . Join ( "," , Capabilities . Categories . MapTorznabCapsToTrackers ( categories ) ) ;
if ( parameters . Get ( "action" ) is null )
{
parameters . Add ( "action" , "latest-torrents" ) ;
}
parameters . Add ( "category" , string . Join ( "," , Capabilities . Categories . MapTorznabCapsToTrackers ( searchCriteria . Categories ) ) ) ;
var baseUrl = string . Format ( "{0}/api.php?action={1}&category={2}&username={3}&passkey={4}{5}" , Settings . BaseUrl . TrimEnd ( '/' ) , searchType , categoriesQuery , Settings . Username . Trim ( ) , Settings . Passkey . Trim ( ) , parameters ) ;
var searchUrl = $"{Settings.BaseUrl.TrimEnd('/')}/api.php?{parameters.GetQueryString()}" ;
yield return new IndexerRequest ( searchUrl , HttpAccept . Json ) ;
}
private NameValueCollection GetDefaultParameters ( )
{
var parameters = new NameValueCollection
{
{ "username" , Settings . Username . Trim ( ) } ,
{ "passkey" , Settings . Passkey . Trim ( ) }
} ;
if ( Settings . FreeleechOnly )
{
baseUrl + = "&freeleech=1" ;
parameters. Add ( "freeleech" , "1" ) ;
}
yield return new IndexerRequest ( baseUrl , HttpAccept . Json ) ;
return parameters ;
}
}
}