2017-09-26 02:31:52 +00:00
using System ;
2014-05-13 17:57:46 +00:00
using System.Linq ;
using System.Collections.Generic ;
using NzbDrone.Common.Disk ;
using NzbDrone.Common.Http ;
using NzbDrone.Common.Extensions ;
using NzbDrone.Core.MediaFiles.TorrentInfo ;
using NzbDrone.Core.Parser.Model ;
using NzbDrone.Core.Configuration ;
using NzbDrone.Core.Validation ;
using NLog ;
using FluentValidation.Results ;
using System.Net ;
using NzbDrone.Core.RemotePathMappings ;
namespace NzbDrone.Core.Download.Clients.Deluge
{
public class Deluge : TorrentClientBase < DelugeSettings >
{
private readonly IDelugeProxy _proxy ;
public Deluge ( IDelugeProxy proxy ,
ITorrentFileInfoReader torrentFileInfoReader ,
IHttpClient httpClient ,
IConfigService configService ,
IDiskProvider diskProvider ,
IRemotePathMappingService remotePathMappingService ,
Logger logger )
2014-12-23 01:17:48 +00:00
: base ( torrentFileInfoReader , httpClient , configService , diskProvider , remotePathMappingService , logger )
2014-05-13 17:57:46 +00:00
{
_proxy = proxy ;
}
2017-08-14 02:58:42 +00:00
protected override string AddFromMagnetLink ( RemoteAlbum remoteAlbum , string hash , string magnetLink )
2014-05-13 17:57:46 +00:00
{
var actualHash = _proxy . AddTorrentFromMagnet ( magnetLink , Settings ) ;
if ( ! Settings . TvCategory . IsNullOrWhiteSpace ( ) )
{
_proxy . SetLabel ( actualHash , Settings . TvCategory , Settings ) ;
}
_proxy . SetTorrentConfiguration ( actualHash , "remove_at_ratio" , false , Settings ) ;
2017-08-14 02:58:42 +00:00
var isRecentAlbum = remoteAlbum . IsRecentAlbum ( ) ;
2014-05-13 17:57:46 +00:00
2017-08-14 02:58:42 +00:00
if ( isRecentAlbum & & Settings . RecentTvPriority = = ( int ) DelugePriority . First | |
! isRecentAlbum & & Settings . OlderTvPriority = = ( int ) DelugePriority . First )
2014-05-13 17:57:46 +00:00
{
_proxy . MoveTorrentToTopInQueue ( actualHash , Settings ) ;
}
return actualHash . ToUpper ( ) ;
}
2017-08-14 02:58:42 +00:00
protected override string AddFromTorrentFile ( RemoteAlbum remoteAlbum , string hash , string filename , byte [ ] fileContent )
2014-05-13 17:57:46 +00:00
{
var actualHash = _proxy . AddTorrentFromFile ( filename , fileContent , Settings ) ;
2017-10-29 02:57:03 +00:00
if ( actualHash . IsNullOrWhiteSpace ( ) )
{
throw new DownloadClientException ( "Deluge failed to add torrent " + filename ) ;
}
2014-05-13 17:57:46 +00:00
if ( ! Settings . TvCategory . IsNullOrWhiteSpace ( ) )
{
_proxy . SetLabel ( actualHash , Settings . TvCategory , Settings ) ;
}
_proxy . SetTorrentConfiguration ( actualHash , "remove_at_ratio" , false , Settings ) ;
2017-08-14 02:58:42 +00:00
var isRecentEpisode = remoteAlbum . IsRecentAlbum ( ) ;
2014-05-13 17:57:46 +00:00
if ( isRecentEpisode & & Settings . RecentTvPriority = = ( int ) DelugePriority . First | |
! isRecentEpisode & & Settings . OlderTvPriority = = ( int ) DelugePriority . First )
{
_proxy . MoveTorrentToTopInQueue ( actualHash , Settings ) ;
}
return actualHash . ToUpper ( ) ;
}
2016-12-09 06:54:15 +00:00
public override string Name = > "Deluge" ;
2015-04-25 16:22:53 +00:00
2014-05-13 17:57:46 +00:00
public override IEnumerable < DownloadClientItem > GetItems ( )
{
IEnumerable < DelugeTorrent > torrents ;
2017-10-27 03:21:06 +00:00
if ( ! Settings . TvCategory . IsNullOrWhiteSpace ( ) )
2014-05-13 17:57:46 +00:00
{
2017-10-27 03:21:06 +00:00
torrents = _proxy . GetTorrentsByLabel ( Settings . TvCategory , Settings ) ;
2014-05-13 17:57:46 +00:00
}
2017-10-27 03:21:06 +00:00
else
2014-05-13 17:57:46 +00:00
{
2017-10-27 03:21:06 +00:00
torrents = _proxy . GetTorrents ( Settings ) ;
2014-05-13 17:57:46 +00:00
}
var items = new List < DownloadClientItem > ( ) ;
foreach ( var torrent in torrents )
{
var item = new DownloadClientItem ( ) ;
2014-12-19 00:26:42 +00:00
item . DownloadId = torrent . Hash . ToUpper ( ) ;
2014-05-13 17:57:46 +00:00
item . Title = torrent . Name ;
item . Category = Settings . TvCategory ;
item . DownloadClient = Definition . Name ;
2014-10-12 22:00:03 +00:00
var outputPath = _remotePathMappingService . RemapRemoteToLocal ( Settings . Host , new OsPath ( torrent . DownloadPath ) ) ;
item . OutputPath = outputPath + torrent . Name ;
2014-05-13 17:57:46 +00:00
item . RemainingSize = torrent . Size - torrent . BytesDownloaded ;
item . RemainingTime = TimeSpan . FromSeconds ( torrent . Eta ) ;
item . TotalSize = torrent . Size ;
if ( torrent . State = = DelugeTorrentStatus . Error )
{
2014-11-22 02:10:24 +00:00
item . Status = DownloadItemStatus . Warning ;
item . Message = "Deluge is reporting an error" ;
2014-05-13 17:57:46 +00:00
}
else if ( torrent . IsFinished & & torrent . State ! = DelugeTorrentStatus . Checking )
{
item . Status = DownloadItemStatus . Completed ;
}
else if ( torrent . State = = DelugeTorrentStatus . Queued )
{
item . Status = DownloadItemStatus . Queued ;
}
else if ( torrent . State = = DelugeTorrentStatus . Paused )
{
item . Status = DownloadItemStatus . Paused ;
}
else
{
item . Status = DownloadItemStatus . Downloading ;
}
// Here we detect if Deluge is managing the torrent and whether the seed criteria has been met. This allows drone to delete the torrent as appropriate.
2017-09-26 02:31:52 +00:00
item . CanMoveFiles = item . CanBeRemoved = ( torrent . IsAutoManaged & & torrent . StopAtRatio & & torrent . Ratio > = torrent . StopRatio & & torrent . State = = DelugeTorrentStatus . Paused ) ;
2014-05-13 17:57:46 +00:00
items . Add ( item ) ;
}
return items ;
}
2014-12-23 01:17:48 +00:00
public override void RemoveItem ( string downloadId , bool deleteData )
2014-05-13 17:57:46 +00:00
{
2014-12-23 01:17:48 +00:00
_proxy . RemoveTorrent ( downloadId . ToLower ( ) , deleteData , Settings ) ;
2014-05-13 17:57:46 +00:00
}
2017-10-08 03:54:13 +00:00
public override DownloadClientInfo GetStatus ( )
2014-05-13 17:57:46 +00:00
{
var config = _proxy . GetConfig ( Settings ) ;
2014-10-12 22:00:03 +00:00
var destDir = new OsPath ( config . GetValueOrDefault ( "download_location" ) as string ) ;
2014-05-13 17:57:46 +00:00
if ( config . GetValueOrDefault ( "move_completed" , false ) . ToString ( ) = = "True" )
{
2014-10-12 22:00:03 +00:00
destDir = new OsPath ( config . GetValueOrDefault ( "move_completed_path" ) as string ) ;
2014-05-13 17:57:46 +00:00
}
2017-10-08 03:54:13 +00:00
var status = new DownloadClientInfo
2014-05-13 17:57:46 +00:00
{
IsLocalhost = Settings . Host = = "127.0.0.1" | | Settings . Host = = "localhost"
} ;
2014-10-12 22:00:03 +00:00
if ( ! destDir . IsEmpty )
2014-05-13 17:57:46 +00:00
{
2014-10-12 22:00:03 +00:00
status . OutputRootFolders = new List < OsPath > { _remotePathMappingService . RemapRemoteToLocal ( Settings . Host , destDir ) } ;
2014-05-13 17:57:46 +00:00
}
return status ;
}
protected override void Test ( List < ValidationFailure > failures )
{
failures . AddIfNotNull ( TestConnection ( ) ) ;
2014-11-19 19:02:44 +00:00
if ( failures . Any ( ) ) return ;
2014-05-13 17:57:46 +00:00
failures . AddIfNotNull ( TestCategory ( ) ) ;
failures . AddIfNotNull ( TestGetTorrents ( ) ) ;
}
private ValidationFailure TestConnection ( )
{
try
{
_proxy . GetVersion ( Settings ) ;
}
catch ( DownloadClientAuthenticationException ex )
{
2017-10-08 05:14:26 +00:00
_logger . Error ( ex , ex . Message ) ;
2014-05-13 17:57:46 +00:00
return new NzbDroneValidationFailure ( "Password" , "Authentication failed" ) ;
}
catch ( WebException ex )
{
2017-10-08 05:14:26 +00:00
_logger . Error ( ex , "Unable to test connection" ) ;
2014-12-07 07:23:11 +00:00
switch ( ex . Status )
2014-05-13 17:57:46 +00:00
{
2014-12-07 07:23:11 +00:00
case WebExceptionStatus . ConnectFailure :
return new NzbDroneValidationFailure ( "Host" , "Unable to connect" )
{
DetailedDescription = "Please verify the hostname and port."
} ;
case WebExceptionStatus . ConnectionClosed :
return new NzbDroneValidationFailure ( "UseSsl" , "Verify SSL settings" )
{
DetailedDescription = "Please verify your SSL configuration on both Deluge and NzbDrone."
} ;
case WebExceptionStatus . SecureChannelFailure :
return new NzbDroneValidationFailure ( "UseSsl" , "Unable to connect through SSL" )
{
DetailedDescription = "Drone is unable to connect to Deluge using SSL. This problem could be computer related. Please try to configure both drone and Deluge to not use SSL."
} ;
default :
2015-10-03 17:45:26 +00:00
return new NzbDroneValidationFailure ( string . Empty , "Unknown exception: " + ex . Message ) ;
2014-05-13 17:57:46 +00:00
}
}
catch ( Exception ex )
{
2017-10-08 05:14:26 +00:00
_logger . Error ( ex , "Failed to test connection" ) ;
2015-10-03 17:45:26 +00:00
return new NzbDroneValidationFailure ( string . Empty , "Unknown exception: " + ex . Message ) ;
2014-05-13 17:57:46 +00:00
}
return null ;
}
private ValidationFailure TestCategory ( )
{
if ( Settings . TvCategory . IsNullOrWhiteSpace ( ) )
{
return null ;
}
2014-12-23 01:17:48 +00:00
2014-05-13 17:57:46 +00:00
var enabledPlugins = _proxy . GetEnabledPlugins ( Settings ) ;
if ( ! enabledPlugins . Contains ( "Label" ) )
{
return new NzbDroneValidationFailure ( "TvCategory" , "Label plugin not activated" )
{
DetailedDescription = "You must have the Label plugin enabled in Deluge to use categories."
} ;
}
var labels = _proxy . GetAvailableLabels ( Settings ) ;
if ( ! labels . Contains ( Settings . TvCategory ) )
{
_proxy . AddLabel ( Settings . TvCategory , Settings ) ;
labels = _proxy . GetAvailableLabels ( Settings ) ;
if ( ! labels . Contains ( Settings . TvCategory ) )
{
return new NzbDroneValidationFailure ( "TvCategory" , "Configuration of label failed" )
{
2017-03-30 03:49:38 +00:00
DetailedDescription = "Lidarr as unable to add the label to Deluge."
2014-05-13 17:57:46 +00:00
} ;
}
}
return null ;
}
private ValidationFailure TestGetTorrents ( )
{
try
{
_proxy . GetTorrents ( Settings ) ;
}
catch ( Exception ex )
{
2017-10-08 05:14:26 +00:00
_logger . Error ( ex , "Unable to get torrents" ) ;
2015-10-03 17:45:26 +00:00
return new NzbDroneValidationFailure ( string . Empty , "Failed to get the list of torrents: " + ex . Message ) ;
2014-05-13 17:57:46 +00:00
}
return null ;
}
}
}