2018-04-01 18:21:24 +00:00
using System ;
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 ;
2019-12-22 22:08:53 +00:00
using NLog ;
using NzbDrone.Common.Extensions ;
2017-01-09 13:30:15 +00:00
using NzbDrone.Common.Http ;
using NzbDrone.Core.Indexers.Exceptions ;
using NzbDrone.Core.Parser.Model ;
namespace NzbDrone.Core.Indexers.PassThePopcorn
{
public class PassThePopcornParser : IParseIndexerResponse
{
private readonly PassThePopcornSettings _settings ;
2018-04-16 23:21:14 +00:00
private readonly Logger _logger ;
public PassThePopcornParser ( PassThePopcornSettings settings , Logger logger )
2017-01-09 13:30:15 +00:00
{
_settings = settings ;
2018-04-16 23:21:14 +00:00
_logger = logger ;
2017-01-09 13:30:15 +00:00
}
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
2018-04-01 18:21:24 +00:00
if ( indexerResponse . HttpResponse . HasHttpRedirect & & indexerResponse . HttpResponse . Headers [ "Location" ]
. ContainsIgnoreCase ( "login.php" ) )
{
CookiesUpdater ( null , null ) ;
throw new IndexerException ( indexerResponse , "We are being redirected to the PTP login page. Most likely your session expired or was killed. Try testing the indexer in the settings." ) ;
}
2019-12-22 22:08:53 +00:00
2017-03-10 23:08:49 +00:00
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 )
{
2018-04-01 18:21:24 +00:00
if ( indexerResponse . HttpResponse . Request . Url . Path . ContainsIgnoreCase ( "login.php" ) )
{
CookiesUpdater ( null , null ) ;
throw new IndexerException ( indexerResponse , "We are currently on the login page. Most likely your session expired or was killed. Try testing the indexer in the settings." ) ;
}
2019-12-22 22:08:53 +00:00
2017-03-10 23:08:49 +00:00
// Remove cookie cache
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-05-26 09:12:07 +00:00
return torrentInfos ;
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 ;
2019-12-22 21:24:10 +00:00
IndexerFlags flags = 0 ;
2017-03-14 16:45:19 +00:00
2017-03-14 18:18:36 +00:00
if ( torrent . GoldenPopcorn )
{
2022-11-20 18:27:45 +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 )
{
2022-11-20 18:27:45 +00:00
flags | = IndexerFlags . PTP_Approved ; // title = $"{title} ✔";
2017-03-14 18:18:36 +00:00
}
2017-01-09 13:30:15 +00:00
2017-04-17 15:12:09 +00:00
if ( torrent . FreeleechType = = "Freeleech" )
2017-01-09 13:30:15 +00:00
{
2017-04-17 15:12:09 +00:00
flags | = IndexerFlags . G_Freeleech ;
2017-01-09 13:30:15 +00:00
}
2017-04-14 20:27:48 +00:00
2018-05-27 09:05:23 +00:00
if ( torrent . Scene )
{
flags | = IndexerFlags . G_Scene ;
}
2017-04-17 15:12:09 +00:00
// Only add approved torrents
2018-04-16 23:21:14 +00:00
try
{
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 ,
Approved = torrent . Checked ,
2019-12-22 22:08:53 +00:00
ImdbId = result . ImdbId . IsNotNullOrWhiteSpace ( ) ? int . Parse ( result . ImdbId ) : 0 ,
2018-04-16 23:21:14 +00:00
IndexerFlags = flags
} ) ;
}
catch ( Exception e )
{
_logger . Error ( e , "Encountered exception parsing PTP torrent: {" +
$"Size: {torrent.Size}" +
$"UploadTime: {torrent.UploadTime}" +
$"Seeders: {torrent.Seeders}" +
$"Leechers: {torrent.Leechers}" +
$"ReleaseName: {torrent.ReleaseName}" +
$"ID: {torrent.Id}" +
2020-11-12 22:54:27 +00:00
"}. Please immediately report this info on https://github.com/Radarr/Radarr/issues/new?assignees=&labels=bug&template=bug_report.md&title=Encountered%20Exception%20Parsing%20%20PTP%20Torrent." ) ;
2018-04-16 23:21:14 +00:00
throw ;
}
2017-01-09 13:30:15 +00:00
}
}
2019-12-22 22:08:53 +00:00
2017-03-14 18:18:36 +00:00
return
2018-02-01 13:18:52 +00:00
torrentInfos ;
2017-01-09 13:30:15 +00:00
}
2018-04-01 18:21:24 +00:00
public Action < IDictionary < string , string > , DateTime ? > CookiesUpdater { get ; set ; }
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 ;
}
}
}