@ -43,6 +43,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
}
private IQBittorrentProxy Proxy = > _proxySelector . GetProxy ( Settings ) ;
private Version ProxyApiVersion = > _proxySelector . GetApiVersion ( Settings ) ;
public override void MarkItemAsImported ( DownloadClientItem downloadClientItem )
{
@ -70,21 +71,49 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
throw new NotSupportedException ( "Magnet Links without trackers not supported if DHT is disabled" ) ;
}
Proxy . AddTorrentFromUrl ( magnetLink , Settings ) ;
var setShareLimits = remoteAlbum . SeedConfiguration ! = null & & ( remoteAlbum . SeedConfiguration . Ratio . HasValue | | remoteAlbum . SeedConfiguration . SeedTime . HasValue ) ;
var addHasSetShareLimits = setShareLimits & & ProxyApiVersion > = new Version ( 2 , 8 , 1 ) ;
var isRecentAlbum = remoteAlbum . IsRecentAlbum ( ) ;
var moveToTop = ( isRecentAlbum & & Settings . RecentTvPriority = = ( int ) QBittorrentPriority . First ) | | ( ! isRecentAlbum & & Settings . OlderTvPriority = = ( int ) QBittorrentPriority . First ) ;
var forceStart = ( QBittorrentState ) Settings . InitialState = = QBittorrentState . ForceStart ;
Proxy . AddTorrentFromUrl ( magnetLink , addHasSetShareLimits & & setShareLimits ? remoteAlbum . SeedConfiguration : null , Settings ) ;
if ( ( isRecentAlbum & & Settings . RecentTvPriority = = ( int ) QBittorrentPriority . First ) | |
( ! isRecentAlbum & & Settings . OlderTvPriority = = ( int ) QBittorrentPriority . First ) )
if ( ( ! addHasSetShareLimits & & setShareLimits ) | | moveToTop | | forceStart )
{
Proxy . MoveTorrentToTopInQueue ( hash . ToLower ( ) , Settings ) ;
}
if ( ! WaitForTorrent ( hash ) )
{
return hash ;
}
SetInitialState ( hash . ToLower ( ) ) ;
if ( ! addHasSetShareLimits & & setShareLimits )
{
Proxy . SetTorrentSeedingConfiguration ( hash . ToLower ( ) , remoteAlbum . SeedConfiguration , Settings ) ;
}
if ( remoteAlbum . SeedConfiguration ! = null & & ( remoteAlbum . SeedConfiguration . Ratio . HasValue | | remoteAlbum . SeedConfiguration . SeedTime . HasValue ) )
{
Proxy . SetTorrentSeedingConfiguration ( hash . ToLower ( ) , remoteAlbum . SeedConfiguration , Settings ) ;
if ( moveToTop )
{
try
{
Proxy . MoveTorrentToTopInQueue ( hash . ToLower ( ) , Settings ) ;
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Failed to set the torrent priority for {0}." , hash ) ;
}
}
if ( forceStart )
{
try
{
Proxy . SetForceStart ( hash . ToLower ( ) , true , Settings ) ;
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Failed to set ForceStart for {0}." , hash ) ;
}
}
}
return hash ;
@ -92,31 +121,76 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
protected override string AddFromTorrentFile ( RemoteAlbum remoteAlbum , string hash , string filename , byte [ ] fileContent )
{
Proxy . AddTorrentFromFile ( filename , fileContent , Settings ) ;
var setShareLimits = remoteAlbum . SeedConfiguration ! = null & & ( remoteAlbum . SeedConfiguration . Ratio . HasValue | | remoteAlbum . SeedConfiguration . SeedTime . HasValue ) ;
var addHasSetShareLimits = setShareLimits & & ProxyApiVersion > = new Version ( 2 , 8 , 1 ) ;
var isRecentAlbum = remoteAlbum . IsRecentAlbum ( ) ;
var moveToTop = ( isRecentAlbum & & Settings . RecentTvPriority = = ( int ) QBittorrentPriority . First ) | | ( ! isRecentAlbum & & Settings . OlderTvPriority = = ( int ) QBittorrentPriority . First ) ;
var forceStart = ( QBittorrentState ) Settings . InitialState = = QBittorrentState . ForceStart ;
try
Proxy . AddTorrentFromFile ( filename , fileContent , addHasSetShareLimits ? remoteAlbum . SeedConfiguration : null , Settings ) ;
if ( ( ! addHasSetShareLimits & & setShareLimits ) | | moveToTop | | forceStart )
{
var isRecentAlbum = remoteAlbum . IsRecentAlbum ( ) ;
if ( ! WaitForTorrent ( hash ) )
{
return hash ;
}
if ( ( isRecentAlbum & & Settings . RecentTvPriority = = ( int ) QBittorrentPriority . First ) | |
( ! isRecentAlbum & & Settings . OlderTvPriority = = ( int ) QBittorrentPriority . First ) )
if ( ! addHasSetShareLimits & & setShareLimits )
{
Proxy . MoveTorrentToTopInQueue ( hash . ToLower ( ) , Settings ) ;
Proxy . SetTorrentSeedingConfiguration ( hash . ToLower ( ) , remoteAlbum . SeedConfiguration , Settings ) ;
}
if ( moveToTop )
{
try
{
Proxy . MoveTorrentToTopInQueue ( hash . ToLower ( ) , Settings ) ;
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Failed to set the torrent priority for {0}." , hash ) ;
}
}
if ( forceStart )
{
try
{
Proxy . SetForceStart ( hash . ToLower ( ) , true , Settings ) ;
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Failed to set ForceStart for {0}." , hash ) ;
}
}
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Failed to set the torrent priority for {0}." , filename ) ;
}
SetInitialState ( hash . ToLower ( ) ) ;
return hash ;
}
protected bool WaitForTorrent ( string hash )
{
var count = 5 ;
if ( remoteAlbum . SeedConfiguration ! = null & & ( remoteAlbum . SeedConfiguration . Ratio . HasValue | | remoteAlbum . SeedConfiguration . SeedTime . HasValue ) )
while ( count ! = 0 )
{
Proxy . SetTorrentSeedingConfiguration ( hash . ToLower ( ) , remoteAlbum . SeedConfiguration , Settings ) ;
try
{
Proxy . GetTorrentProperties ( hash . ToLower ( ) , Settings ) ;
return true ;
}
catch
{
}
_logger . Trace ( "Torrent '{0}' not yet visible in qbit, waiting 100ms." , hash ) ;
System . Threading . Thread . Sleep ( 100 ) ;
count - - ;
}
return hash ;
_logger . Warn ( "Failed to load torrent '{0}' within 500 ms, skipping additional parameters." , hash ) ;
return false ;
}
public override string Name = > "qBittorrent" ;
@ -488,29 +562,6 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
return null ;
}
private void SetInitialState ( string hash )
{
try
{
switch ( ( QBittorrentState ) Settings . InitialState )
{
case QBittorrentState . ForceStart :
Proxy . SetForceStart ( hash , true , Settings ) ;
break ;
case QBittorrentState . Start :
Proxy . ResumeTorrent ( hash , Settings ) ;
break ;
case QBittorrentState . Pause :
Proxy . PauseTorrent ( hash , Settings ) ;
break ;
}
}
catch ( Exception ex )
{
_logger . Warn ( ex , "Failed to set inital state for {0}." , hash ) ;
}
}
protected TimeSpan ? GetRemainingTime ( QBittorrentTorrent torrent )
{
if ( torrent . Eta < 0 | | torrent . Eta > 365 * 24 * 3600 )