Radarr/src/NzbDrone.Core/Download/Clients/Transmission/TransmissionBase.cs

314 lines
12 KiB
C#
Raw Normal View History

2016-05-03 07:58:28 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using NLog;
using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Blocklisting;
2016-05-03 07:58:28 +00:00
using NzbDrone.Core.Configuration;
using NzbDrone.Core.MediaFiles.TorrentInfo;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.RemotePathMappings;
using NzbDrone.Core.Validation;
namespace NzbDrone.Core.Download.Clients.Transmission
{
public abstract class TransmissionBase : TorrentClientBase<TransmissionSettings>
{
protected readonly ITransmissionProxy _proxy;
public TransmissionBase(ITransmissionProxy proxy,
ITorrentFileInfoReader torrentFileInfoReader,
IHttpClient httpClient,
IConfigService configService,
IDiskProvider diskProvider,
IRemotePathMappingService remotePathMappingService,
IBlocklistService blocklistService,
2016-05-03 07:58:28 +00:00
Logger logger)
: base(torrentFileInfoReader, httpClient, configService, diskProvider, remotePathMappingService, blocklistService, logger)
2016-05-03 07:58:28 +00:00
{
_proxy = proxy;
}
public override IEnumerable<DownloadClientItem> GetItems()
{
2019-07-10 02:05:32 +00:00
var configFunc = new Lazy<TransmissionConfig>(() => _proxy.GetConfig(Settings));
var torrents = _proxy.GetTorrents(Settings);
2016-05-03 07:58:28 +00:00
var items = new List<DownloadClientItem>();
foreach (var torrent in torrents)
{
// If totalsize == 0 the torrent is a magnet downloading metadata
2019-12-22 22:08:53 +00:00
if (torrent.TotalSize == 0)
{
continue;
}
2016-05-03 07:58:28 +00:00
var outputPath = new OsPath(torrent.DownloadDir);
2017-01-13 16:13:41 +00:00
if (Settings.MovieDirectory.IsNotNullOrWhiteSpace())
2016-05-03 07:58:28 +00:00
{
2019-12-22 22:08:53 +00:00
if (!new OsPath(Settings.MovieDirectory).Contains(outputPath))
{
continue;
}
2016-05-03 07:58:28 +00:00
}
2017-01-13 16:13:41 +00:00
else if (Settings.MovieCategory.IsNotNullOrWhiteSpace())
2016-05-03 07:58:28 +00:00
{
var directories = outputPath.FullPath.Split('\\', '/');
2019-12-22 22:08:53 +00:00
if (!directories.Contains(Settings.MovieCategory))
{
continue;
}
2016-05-03 07:58:28 +00:00
}
outputPath = _remotePathMappingService.RemapRemoteToLocal(Settings.Host, outputPath);
var item = new DownloadClientItem();
item.DownloadId = torrent.HashString.ToUpper();
2017-01-13 16:13:41 +00:00
item.Category = Settings.MovieCategory;
2016-05-03 07:58:28 +00:00
item.Title = torrent.Name;
item.DownloadClientInfo = DownloadClientItemClientInfo.FromDownloadClient(this, false);
2016-05-03 07:58:28 +00:00
item.OutputPath = GetOutputPath(outputPath, torrent);
item.TotalSize = torrent.TotalSize;
item.RemainingSize = torrent.LeftUntilDone;
item.SeedRatio = torrent.DownloadedEver <= 0 ? 0 :
2019-12-22 22:08:53 +00:00
(double)torrent.UploadedEver / torrent.DownloadedEver;
2016-05-03 07:58:28 +00:00
if (torrent.Eta >= 0)
{
try
{
item.RemainingTime = TimeSpan.FromSeconds(torrent.Eta);
}
catch (OverflowException)
{
item.RemainingTime = TimeSpan.FromMilliseconds(torrent.Eta);
}
2016-05-03 07:58:28 +00:00
}
if (!torrent.ErrorString.IsNullOrWhiteSpace())
{
item.Status = DownloadItemStatus.Warning;
item.Message = torrent.ErrorString;
}
else if (torrent.LeftUntilDone == 0 && (torrent.Status == TransmissionTorrentStatus.Stopped ||
torrent.Status == TransmissionTorrentStatus.Seeding ||
torrent.Status == TransmissionTorrentStatus.SeedingWait))
2016-05-03 07:58:28 +00:00
{
item.Status = DownloadItemStatus.Completed;
}
else if (torrent.IsFinished && torrent.Status != TransmissionTorrentStatus.Check &&
torrent.Status != TransmissionTorrentStatus.CheckWait)
{
item.Status = DownloadItemStatus.Completed;
}
else if (torrent.Status == TransmissionTorrentStatus.Queued)
{
item.Status = DownloadItemStatus.Queued;
}
else
{
item.Status = DownloadItemStatus.Downloading;
}
2019-07-10 02:05:32 +00:00
item.CanBeRemoved = HasReachedSeedLimit(torrent, item.SeedRatio, configFunc);
item.CanMoveFiles = item.CanBeRemoved && torrent.Status == TransmissionTorrentStatus.Stopped;
2016-05-03 07:58:28 +00:00
items.Add(item);
}
return items;
}
2019-07-10 02:05:32 +00:00
protected bool HasReachedSeedLimit(TransmissionTorrent torrent, double? ratio, Lazy<TransmissionConfig> config)
{
var isStopped = torrent.Status == TransmissionTorrentStatus.Stopped;
var isSeeding = torrent.Status == TransmissionTorrentStatus.Seeding;
2019-12-22 21:24:11 +00:00
2019-07-10 02:05:32 +00:00
if (torrent.SeedRatioMode == 1)
{
if (isStopped && ratio.HasValue && ratio >= torrent.SeedRatioLimit)
{
return true;
}
}
else if (torrent.SeedRatioMode == 0)
{
if (isStopped && config.Value.SeedRatioLimited && ratio >= config.Value.SeedRatioLimit)
{
return true;
}
}
// Transmission doesn't support SeedTimeLimit, use/abuse seed idle limit, but only if it was set per-torrent.
if (torrent.SeedIdleMode == 1)
{
if ((isStopped || isSeeding) && torrent.SecondsSeeding > torrent.SeedIdleLimit * 60)
{
return true;
}
}
else if (torrent.SeedIdleMode == 0)
{
// The global idle limit is a real idle limit, if it's configured then 'Stopped' is enough.
if (isStopped && config.Value.IdleSeedingLimitEnabled)
{
return true;
}
}
return false;
}
public override void RemoveItem(DownloadClientItem item, bool deleteData)
2016-05-03 07:58:28 +00:00
{
_proxy.RemoveTorrent(item.DownloadId.ToLower(), deleteData, Settings);
2016-05-03 07:58:28 +00:00
}
public override DownloadClientInfo GetStatus()
2016-05-03 07:58:28 +00:00
{
string destDir;
if (Settings.MovieDirectory.IsNotNullOrWhiteSpace())
{
destDir = Settings.MovieDirectory;
}
else
2016-05-03 07:58:28 +00:00
{
var config = _proxy.GetConfig(Settings);
destDir = config.DownloadDir;
if (Settings.MovieCategory.IsNotNullOrWhiteSpace())
{
destDir = string.Format("{0}/{1}", destDir, Settings.MovieCategory);
}
2016-05-03 07:58:28 +00:00
}
return new DownloadClientInfo
2016-05-03 07:58:28 +00:00
{
IsLocalhost = Settings.Host == "127.0.0.1" || Settings.Host == "localhost",
OutputRootFolders = new List<OsPath> { _remotePathMappingService.RemapRemoteToLocal(Settings.Host, new OsPath(destDir)) }
};
}
2017-01-10 22:37:23 +00:00
protected override string AddFromMagnetLink(RemoteMovie remoteMovie, string hash, string magnetLink)
{
_proxy.AddTorrentFromUrl(magnetLink, GetDownloadDirectory(), Settings);
_proxy.SetTorrentSeedingConfiguration(hash, remoteMovie.SeedConfiguration, Settings);
2022-03-20 15:55:47 +00:00
var isRecentMovie = remoteMovie.Movie.MovieMetadata.Value.IsRecentMovie;
2019-12-22 22:08:53 +00:00
if ((isRecentMovie && Settings.RecentMoviePriority == (int)TransmissionPriority.First) ||
(!isRecentMovie && Settings.OlderMoviePriority == (int)TransmissionPriority.First))
{
_proxy.MoveTorrentToTopInQueue(hash, Settings);
}
2017-01-10 22:37:23 +00:00
return hash;
}
protected override string AddFromTorrentFile(RemoteMovie remoteMovie, string hash, string filename, byte[] fileContent)
{
_proxy.AddTorrentFromData(fileContent, GetDownloadDirectory(), Settings);
_proxy.SetTorrentSeedingConfiguration(hash, remoteMovie.SeedConfiguration, Settings);
2022-03-20 15:55:47 +00:00
var isRecentMovie = remoteMovie.Movie.MovieMetadata.Value.IsRecentMovie;
2019-12-22 22:08:53 +00:00
if ((isRecentMovie && Settings.RecentMoviePriority == (int)TransmissionPriority.First) ||
(!isRecentMovie && Settings.OlderMoviePriority == (int)TransmissionPriority.First))
{
_proxy.MoveTorrentToTopInQueue(hash, Settings);
}
2016-05-03 07:58:28 +00:00
return hash;
}
protected override void Test(List<ValidationFailure> failures)
{
failures.AddIfNotNull(TestConnection());
2019-12-22 22:08:53 +00:00
if (failures.HasErrors())
{
return;
}
2016-05-03 07:58:28 +00:00
failures.AddIfNotNull(TestGetTorrents());
}
protected virtual OsPath GetOutputPath(OsPath outputPath, TransmissionTorrent torrent)
{
return outputPath + torrent.Name.Replace(":", "_");
2016-05-03 07:58:28 +00:00
}
protected string GetDownloadDirectory()
{
2017-01-13 16:13:41 +00:00
if (Settings.MovieDirectory.IsNotNullOrWhiteSpace())
2016-05-03 07:58:28 +00:00
{
2017-01-13 16:13:41 +00:00
return Settings.MovieDirectory;
2016-05-03 07:58:28 +00:00
}
2019-12-22 22:08:53 +00:00
if (!Settings.MovieCategory.IsNotNullOrWhiteSpace())
{
return null;
}
var config = _proxy.GetConfig(Settings);
2019-07-10 02:05:32 +00:00
var destDir = config.DownloadDir;
return $"{destDir.TrimEnd('/')}/{Settings.MovieCategory}";
2016-05-03 07:58:28 +00:00
}
protected ValidationFailure TestConnection()
{
try
{
return ValidateVersion();
}
catch (DownloadClientAuthenticationException ex)
{
_logger.Error(ex, ex.Message);
return new NzbDroneValidationFailure("Username", "Authentication failure")
{
DetailedDescription = string.Format("Please verify your username and password. Also verify if the host running Radarr isn't blocked from accessing {0} by WhiteList limitations in the {0} configuration.", Name)
2016-05-03 07:58:28 +00:00
};
}
catch (DownloadClientUnavailableException ex)
2016-05-03 07:58:28 +00:00
{
_logger.Error(ex, ex.Message);
return new NzbDroneValidationFailure("Host", "Unable to connect to Transmission")
{
DetailedDescription = ex.Message
};
2016-05-03 07:58:28 +00:00
}
catch (Exception ex)
{
_logger.Error(ex, "Failed to test");
2016-05-03 07:58:28 +00:00
return new NzbDroneValidationFailure(string.Empty, "Unknown exception: " + ex.Message);
}
}
protected abstract ValidationFailure ValidateVersion();
private ValidationFailure TestGetTorrents()
{
try
{
_proxy.GetTorrents(Settings);
}
catch (Exception ex)
{
_logger.Error(ex, "Failed to get torrents");
2016-05-03 07:58:28 +00:00
return new NzbDroneValidationFailure(string.Empty, "Failed to get the list of torrents: " + ex.Message);
}
return null;
}
}
}