@ -5,10 +5,12 @@ using System.Net;
using System.Text ;
using System.Threading.Tasks ;
using FluentValidation.Results ;
using MonoTorrent ;
using NLog ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Common.Http ;
using NzbDrone.Core.Configuration ;
using NzbDrone.Core.Exceptions ;
using NzbDrone.Core.Http.CloudFlare ;
using NzbDrone.Core.Indexers.Events ;
using NzbDrone.Core.Indexers.Exceptions ;
@ -100,6 +102,93 @@ namespace NzbDrone.Core.Indexers
return FetchReleases ( g = > SetCookieFunctions ( g ) . GetSearchRequests ( searchCriteria ) ) ;
}
public override async Task < byte [ ] > Download ( Uri link )
{
Cookies = GetCookies ( ) ;
var request = await GetDownloadRequest ( link ) ;
if ( request . Url . Scheme = = "magnet" )
{
ValidateMagnet ( request . Url . FullUri ) ;
return Encoding . UTF8 . GetBytes ( request . Url . FullUri ) ;
}
if ( request . RateLimit < RateLimit )
{
request . RateLimit = RateLimit ;
}
byte [ ] fileData ;
try
{
var response = await _httpClient . ExecuteProxiedAsync ( request , Definition ) ;
fileData = response . ResponseData ;
_logger . Debug ( "Downloaded for release finished ({0} bytes from {1})" , fileData . Length , link . AbsoluteUri ) ;
}
catch ( HttpException ex )
{
if ( ex . Response . StatusCode = = HttpStatusCode . NotFound )
{
_logger . Error ( ex , "Downloading file for release failed since it no longer exists ({0})" , link . AbsoluteUri ) ;
throw new ReleaseUnavailableException ( "Downloading nzb failed" , ex ) ;
}
if ( ex . Response . StatusCode = = HttpStatusCode . TooManyRequests )
{
_logger . Error ( "API Grab Limit reached for {0}" , link . AbsoluteUri ) ;
}
else
{
_logger . Error ( ex , "Downloading for release failed ({0})" , link . AbsoluteUri ) ;
}
throw new ReleaseDownloadException ( "Downloading failed" , ex ) ;
}
catch ( WebException ex )
{
_logger . Error ( ex , "Downloading for release failed ({0})" , link . AbsoluteUri ) ;
throw new ReleaseDownloadException ( "Downloading failed" , ex ) ;
}
catch ( Exception )
{
_indexerStatusService . RecordFailure ( Definition . Id ) ;
_logger . Error ( "Downloading failed" ) ;
throw ;
}
ValidateDownloadData ( fileData ) ;
return fileData ;
}
protected virtual Task < HttpRequest > GetDownloadRequest ( Uri link )
{
var requestBuilder = new HttpRequestBuilder ( link . AbsoluteUri ) ;
if ( Cookies ! = null )
{
requestBuilder . SetCookies ( Cookies ) ;
}
var request = requestBuilder . Build ( ) ;
request . AllowAutoRedirect = FollowRedirect ;
return Task . FromResult ( request ) ;
}
protected virtual void ValidateDownloadData ( byte [ ] fileData )
{
}
protected void ValidateMagnet ( string link )
{
MagnetLink . Parse ( link ) ;
}
protected IIndexerRequestGenerator SetCookieFunctions ( IIndexerRequestGenerator generator )
{
//A func ensures cookies are always updated to the latest. This way, the first page could update the cookies and then can be reused by the second page.