2017-03-14 18:18:36 +00:00
using System.Collections.Generic ;
2017-01-09 13:30:15 +00:00
using System.Net ;
using Newtonsoft.Json ;
using NzbDrone.Common.Http ;
using NzbDrone.Core.Indexers.Exceptions ;
using NzbDrone.Core.Parser.Model ;
using System.Linq ;
2017-03-10 23:08:49 +00:00
using NzbDrone.Common.Cache ;
using NzbDrone.Common.Extensions ;
2017-01-09 13:30:15 +00:00
namespace NzbDrone.Core.Indexers.PassThePopcorn
{
public class PassThePopcornParser : IParseIndexerResponse
{
private readonly PassThePopcornSettings _settings ;
2017-03-10 23:08:49 +00:00
public ICached < Dictionary < string , string > > AuthCookieCache { get ; set ; }
2017-01-09 13:30:15 +00:00
public PassThePopcornParser ( PassThePopcornSettings settings )
{
_settings = settings ;
}
public IList < ReleaseInfo > ParseResponse ( IndexerResponse indexerResponse )
{
var torrentInfos = new List < ReleaseInfo > ( ) ;
if ( indexerResponse . HttpResponse . StatusCode ! = HttpStatusCode . OK )
{
2017-03-10 23:08:49 +00:00
// Remove cookie cache
AuthCookieCache . Remove ( _settings . BaseUrl . Trim ( ) . TrimEnd ( '/' ) ) ;
throw new IndexerException ( indexerResponse , $"Unexpected response status {indexerResponse.HttpResponse.StatusCode} code from API request" ) ;
2017-01-09 13:30:15 +00:00
}
2017-03-10 23:08:49 +00:00
if ( indexerResponse . HttpResponse . Headers . ContentType ! = HttpAccept . Json . Value )
{
// Remove cookie cache
AuthCookieCache . Remove ( _settings . BaseUrl . Trim ( ) . TrimEnd ( '/' ) ) ;
throw new IndexerException ( indexerResponse , $"Unexpected response header {indexerResponse.HttpResponse.Headers.ContentType} from API request, expected {HttpAccept.Json.Value}" ) ;
}
2017-01-09 13:30:15 +00:00
2017-03-10 23:08:49 +00:00
var jsonResponse = JsonConvert . DeserializeObject < PassThePopcornResponse > ( indexerResponse . Content ) ;
2017-03-14 18:18:36 +00:00
if ( jsonResponse . TotalResults = = "0" | |
jsonResponse . TotalResults . IsNullOrWhiteSpace ( ) | |
2017-03-10 23:08:49 +00:00
jsonResponse . Movies = = null )
2017-01-09 13:30:15 +00:00
{
2017-03-10 23:08:49 +00:00
throw new IndexerException ( indexerResponse , "No results were found" ) ;
2017-01-09 13:30:15 +00:00
}
2017-03-10 23:08:49 +00:00
foreach ( var result in jsonResponse . Movies )
2017-01-09 13:30:15 +00:00
{
2017-03-14 18:18:36 +00:00
foreach ( var torrent in result . Torrents )
{
var id = torrent . Id ;
var title = torrent . ReleaseName ;
2017-04-14 20:27:48 +00:00
IndexerFlags flags = 0 ;
2017-03-14 16:45:19 +00:00
2017-03-14 18:18:36 +00:00
if ( torrent . GoldenPopcorn )
{
2017-04-14 20:27:48 +00:00
flags | = IndexerFlags . PTP_Golden ; //title = $"{title} 🍿";
2017-03-14 18:18:36 +00:00
}
2017-01-09 13:30:15 +00:00
2017-03-14 18:18:36 +00:00
if ( torrent . Checked )
{
2017-04-14 20:27:48 +00:00
flags | = IndexerFlags . PTP_Approved ; //title = $"{title} ✔";
2017-03-14 18:18:36 +00:00
}
2017-01-09 13:30:15 +00:00
// Only add approved torrents
2017-02-10 06:35:47 +00:00
if ( _settings . RequireApproved & & torrent . Checked )
2017-01-09 13:30:15 +00:00
{
torrentInfos . Add ( new PassThePopcornInfo ( )
{
Guid = string . Format ( "PassThePopcorn-{0}" , id ) ,
Title = title ,
Size = long . Parse ( torrent . Size ) ,
DownloadUrl = GetDownloadUrl ( id , jsonResponse . AuthKey , jsonResponse . PassKey ) ,
InfoUrl = GetInfoUrl ( result . GroupId , id ) ,
Seeders = int . Parse ( torrent . Seeders ) ,
Peers = int . Parse ( torrent . Leechers ) + int . Parse ( torrent . Seeders ) ,
PublishDate = torrent . UploadTime . ToUniversalTime ( ) ,
Golden = torrent . GoldenPopcorn ,
Scene = torrent . Scene ,
2017-03-14 02:34:25 +00:00
Approved = torrent . Checked ,
2017-04-14 20:27:48 +00:00
ImdbId = ( result . ImdbId . IsNotNullOrWhiteSpace ( ) ? int . Parse ( result . ImdbId ) : 0 ) ,
IndexerFlags = flags
2017-01-09 13:30:15 +00:00
} ) ;
}
2017-04-14 20:27:48 +00:00
2017-01-09 13:30:15 +00:00
// Add all torrents
2017-02-10 06:35:47 +00:00
else if ( ! _settings . RequireApproved )
2017-01-09 13:30:15 +00:00
{
torrentInfos . Add ( new PassThePopcornInfo ( )
{
Guid = string . Format ( "PassThePopcorn-{0}" , id ) ,
Title = title ,
Size = long . Parse ( torrent . Size ) ,
DownloadUrl = GetDownloadUrl ( id , jsonResponse . AuthKey , jsonResponse . PassKey ) ,
InfoUrl = GetInfoUrl ( result . GroupId , id ) ,
Seeders = int . Parse ( torrent . Seeders ) ,
Peers = int . Parse ( torrent . Leechers ) + int . Parse ( torrent . Seeders ) ,
PublishDate = torrent . UploadTime . ToUniversalTime ( ) ,
Golden = torrent . GoldenPopcorn ,
Scene = torrent . Scene ,
2017-03-14 02:34:25 +00:00
Approved = torrent . Checked ,
2017-04-14 20:27:48 +00:00
ImdbId = ( result . ImdbId . IsNotNullOrWhiteSpace ( ) ? int . Parse ( result . ImdbId ) : 0 ) ,
IndexerFlags = flags
2017-01-09 13:30:15 +00:00
} ) ;
}
// Don't add any torrents
2017-02-10 06:35:47 +00:00
else if ( _settings . RequireApproved & & ! torrent . Checked )
2017-01-09 13:30:15 +00:00
{
continue ;
}
}
}
// prefer golden
2017-02-08 19:26:11 +00:00
if ( _settings . Golden )
{
if ( _settings . Scene )
{
return
torrentInfos . OrderByDescending ( o = > o . PublishDate )
. ThenBy ( o = > ( ( dynamic ) o ) . Golden ? 0 : 1 )
2017-03-14 18:18:36 +00:00
. ThenBy ( o = > ( ( dynamic ) o ) . Scene ? 0 : 1 )
2017-02-08 19:26:11 +00:00
. ToArray ( ) ;
}
2017-03-14 18:18:36 +00:00
return
2017-02-08 19:26:11 +00:00
torrentInfos . OrderByDescending ( o = > o . PublishDate )
. ThenBy ( o = > ( ( dynamic ) o ) . Golden ? 0 : 1 )
. ToArray ( ) ;
}
2017-01-09 13:30:15 +00:00
// prefer scene
2017-02-08 19:26:11 +00:00
if ( _settings . Scene )
{
2017-03-14 18:18:36 +00:00
return
2017-02-08 19:26:11 +00:00
torrentInfos . OrderByDescending ( o = > o . PublishDate )
. ThenBy ( o = > ( ( dynamic ) o ) . Scene ? 0 : 1 )
. ToArray ( ) ;
}
// order by date
2017-03-14 18:18:36 +00:00
return
2017-02-08 19:26:11 +00:00
torrentInfos
. OrderByDescending ( o = > o . PublishDate )
. ToArray ( ) ;
2017-01-09 13:30:15 +00:00
}
private string GetDownloadUrl ( int torrentId , string authKey , string passKey )
{
var url = new HttpUri ( _settings . BaseUrl )
. CombinePath ( "/torrents.php" )
. AddQueryParam ( "action" , "download" )
. AddQueryParam ( "id" , torrentId )
. AddQueryParam ( "authkey" , authKey )
. AddQueryParam ( "torrent_pass" , passKey ) ;
return url . FullUri ;
}
private string GetInfoUrl ( string groupId , int torrentId )
{
var url = new HttpUri ( _settings . BaseUrl )
. CombinePath ( "/torrents.php" )
. AddQueryParam ( "id" , groupId )
. AddQueryParam ( "torrentid" , torrentId ) ;
return url . FullUri ;
}
}
}