2017-12-12 04:52:58 +00:00
using System.Collections.Generic ;
2020-01-03 12:49:24 +00:00
using System.Linq ;
2017-09-23 02:48:15 +00:00
using System.Net ;
2020-01-03 12:49:24 +00:00
using NzbDrone.Common.Cache ;
using NzbDrone.Common.Extensions ;
2017-09-23 02:48:15 +00:00
using NzbDrone.Common.Http ;
using NzbDrone.Core.Indexers.Exceptions ;
using NzbDrone.Core.Parser.Model ;
namespace NzbDrone.Core.Indexers.Gazelle
{
public class GazelleParser : IParseIndexerResponse
{
private readonly GazelleSettings _settings ;
public ICached < Dictionary < string , string > > AuthCookieCache { get ; set ; }
public GazelleParser ( GazelleSettings settings )
{
_settings = settings ;
}
public IList < ReleaseInfo > ParseResponse ( IndexerResponse indexerResponse )
{
var torrentInfos = new List < ReleaseInfo > ( ) ;
if ( indexerResponse . HttpResponse . StatusCode ! = HttpStatusCode . OK )
{
// Remove cookie cache
AuthCookieCache . Remove ( _settings . BaseUrl . Trim ( ) . TrimEnd ( '/' ) ) ;
throw new IndexerException ( indexerResponse , $"Unexpected response status {indexerResponse.HttpResponse.StatusCode} code from API request" ) ;
}
if ( ! indexerResponse . HttpResponse . Headers . ContentType . Contains ( 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}" ) ;
}
2018-02-20 03:34:44 +00:00
var jsonResponse = new HttpResponse < GazelleResponse > ( indexerResponse . HttpResponse ) ;
if ( jsonResponse . Resource . Status ! = "success" | |
jsonResponse . Resource . Status . IsNullOrWhiteSpace ( ) | |
jsonResponse . Resource . Response = = null )
2017-09-23 02:48:15 +00:00
{
return torrentInfos ;
}
2018-02-20 03:34:44 +00:00
foreach ( var result in jsonResponse . Resource . Response . Results )
2017-09-23 02:48:15 +00:00
{
if ( result . Torrents ! = null )
{
foreach ( var torrent in result . Torrents )
{
var id = torrent . TorrentId ;
2018-02-20 03:34:44 +00:00
var artist = WebUtility . HtmlDecode ( result . Artist ) ;
var album = WebUtility . HtmlDecode ( result . GroupName ) ;
2017-09-23 02:48:15 +00:00
2020-08-11 20:09:18 +00:00
var title = $"{result.Artist} - {result.GroupName} ({result.GroupYear}) [{torrent.Format} {torrent.Encoding}] [{torrent.Media}]" ;
if ( torrent . HasCue )
{
title + = " [Cue]" ;
}
2024-02-21 04:12:45 +00:00
torrentInfos . Add ( new GazelleInfo
2017-09-23 02:48:15 +00:00
{
Guid = string . Format ( "Gazelle-{0}" , id ) ,
2018-02-20 03:34:44 +00:00
Artist = artist ,
2020-01-03 12:49:24 +00:00
2017-09-23 02:48:15 +00:00
// Splice Title from info to avoid calling API again for every torrent.
2020-08-11 20:09:18 +00:00
Title = WebUtility . HtmlDecode ( title ) ,
2018-02-20 03:34:44 +00:00
Album = album ,
2017-09-23 02:48:15 +00:00
Container = torrent . Encoding ,
Codec = torrent . Format ,
Size = long . Parse ( torrent . Size ) ,
2021-01-12 21:13:20 +00:00
DownloadUrl = GetDownloadUrl ( id ) ,
2017-09-23 02:48:15 +00:00
InfoUrl = GetInfoUrl ( result . GroupId , id ) ,
Seeders = int . Parse ( torrent . Seeders ) ,
Peers = int . Parse ( torrent . Leechers ) + int . Parse ( torrent . Seeders ) ,
PublishDate = torrent . Time . ToUniversalTime ( ) ,
2024-02-21 04:12:45 +00:00
IndexerFlags = GetIndexerFlags ( torrent )
2017-09-23 02:48:15 +00:00
} ) ;
}
}
}
// order by date
return
torrentInfos
. OrderByDescending ( o = > o . PublishDate )
. ToArray ( ) ;
}
2024-02-21 04:12:45 +00:00
private static IndexerFlags GetIndexerFlags ( GazelleTorrent torrent )
{
IndexerFlags flags = 0 ;
if ( torrent . IsFreeLeech | | torrent . IsNeutralLeech | | torrent . IsFreeload | | torrent . IsPersonalFreeLeech )
{
flags | = IndexerFlags . Freeleech ;
}
if ( torrent . Scene )
{
flags | = IndexerFlags . Scene ;
}
return flags ;
}
2021-01-12 21:13:20 +00:00
private string GetDownloadUrl ( int torrentId )
2017-09-23 02:48:15 +00:00
{
var url = new HttpUri ( _settings . BaseUrl )
. CombinePath ( "/torrents.php" )
. AddQueryParam ( "action" , "download" )
. AddQueryParam ( "id" , torrentId )
2021-01-12 21:13:20 +00:00
. AddQueryParam ( "authkey" , _settings . AuthKey )
2023-04-24 14:52:05 +00:00
. AddQueryParam ( "torrent_pass" , _settings . PassKey ) ;
// Orpheus fails to download if usetoken=0 so we need to only add if we will use one
if ( _settings . UseFreeleechToken )
{
url = url . AddQueryParam ( "usetoken" , "1" ) ;
}
2017-09-23 02:48:15 +00:00
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 ;
}
}
}