Fixed: Setting seed criteria while torrent is still being loaded by qbittorrent

Taloth fixed, gitextension dropped the cherry pick link
pull/6060/head
nitsua 4 years ago committed by Qstick
parent ec8d1c4ae6
commit f3fb67a62e

@ -8,6 +8,7 @@ using NUnit.Framework;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients;
using NzbDrone.Core.Download.Clients.QBittorrent; using NzbDrone.Core.Download.Clients.QBittorrent;
using NzbDrone.Core.Exceptions; using NzbDrone.Core.Exceptions;
using NzbDrone.Core.MediaFiles.TorrentInfo; using NzbDrone.Core.MediaFiles.TorrentInfo;
@ -71,14 +72,14 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.QBittorrentTests
protected void GivenFailedDownload() protected void GivenFailedDownload()
{ {
Mocker.GetMock<IQBittorrentProxy>() Mocker.GetMock<IQBittorrentProxy>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<QBittorrentSettings>())) .Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()))
.Throws<InvalidOperationException>(); .Throws<InvalidOperationException>();
} }
protected void GivenSuccessfulDownload() protected void GivenSuccessfulDownload()
{ {
Mocker.GetMock<IQBittorrentProxy>() Mocker.GetMock<IQBittorrentProxy>()
.Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<QBittorrentSettings>())) .Setup(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()))
.Callback(() => .Callback(() =>
{ {
var torrent = new QBittorrentTorrent var torrent = new QBittorrentTorrent
@ -488,7 +489,7 @@ namespace NzbDrone.Core.Test.Download.DownloadClientTests.QBittorrentTests
Assert.DoesNotThrow(() => Subject.Download(remoteMovie)); Assert.DoesNotThrow(() => Subject.Download(remoteMovie));
Mocker.GetMock<IQBittorrentProxy>() Mocker.GetMock<IQBittorrentProxy>()
.Verify(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<QBittorrentSettings>()), Times.Once()); .Verify(s => s.AddTorrentFromUrl(It.IsAny<string>(), It.IsAny<TorrentSeedConfiguration>(), It.IsAny<QBittorrentSettings>()), Times.Once());
} }
[Test] [Test]

@ -45,6 +45,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
} }
private IQBittorrentProxy Proxy => _proxySelector.GetProxy(Settings); private IQBittorrentProxy Proxy => _proxySelector.GetProxy(Settings);
private Version ProxyApiVersion => _proxySelector.GetApiVersion(Settings);
public override void MarkItemAsImported(DownloadClientItem downloadClientItem) 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"); throw new NotSupportedException("Magnet Links without trackers not supported if DHT is disabled");
} }
Proxy.AddTorrentFromUrl(magnetLink, Settings); var setShareLimits = remoteMovie.SeedConfiguration != null && (remoteMovie.SeedConfiguration.Ratio.HasValue || remoteMovie.SeedConfiguration.SeedTime.HasValue);
var addHasSetShareLimits = setShareLimits && ProxyApiVersion >= new Version(2, 8, 1);
var isRecentMovie = remoteMovie.Movie.IsRecentMovie; var isRecentMovie = remoteMovie.Movie.IsRecentMovie;
var moveToTop = (isRecentMovie && Settings.RecentMoviePriority == (int)QBittorrentPriority.First) || (!isRecentMovie && Settings.OlderMoviePriority == (int)QBittorrentPriority.First);
var forceStart = (QBittorrentState)Settings.InitialState == QBittorrentState.ForceStart;
Proxy.AddTorrentFromUrl(magnetLink, addHasSetShareLimits && setShareLimits ? remoteMovie.SeedConfiguration : null, Settings);
if ((isRecentMovie && Settings.RecentMoviePriority == (int)QBittorrentPriority.First) || if ((!addHasSetShareLimits && setShareLimits) || moveToTop || forceStart)
(!isRecentMovie && Settings.OlderMoviePriority == (int)QBittorrentPriority.First))
{ {
Proxy.MoveTorrentToTopInQueue(hash.ToLower(), Settings); if (!WaitForTorrent(hash))
} {
return hash;
}
SetInitialState(hash.ToLower()); if (!addHasSetShareLimits && setShareLimits)
{
Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.SeedConfiguration, Settings);
}
if (remoteMovie.SeedConfiguration != null && (remoteMovie.SeedConfiguration.Ratio.HasValue || remoteMovie.SeedConfiguration.SeedTime.HasValue)) if (moveToTop)
{ {
Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.SeedConfiguration, Settings); 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; return hash;
@ -92,31 +121,76 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
protected override string AddFromTorrentFile(RemoteMovie remoteMovie, string hash, string filename, byte[] fileContent) protected override string AddFromTorrentFile(RemoteMovie remoteMovie, string hash, string filename, byte[] fileContent)
{ {
Proxy.AddTorrentFromFile(filename, fileContent, Settings); var setShareLimits = remoteMovie.SeedConfiguration != null && (remoteMovie.SeedConfiguration.Ratio.HasValue || remoteMovie.SeedConfiguration.SeedTime.HasValue);
var addHasSetShareLimits = setShareLimits && ProxyApiVersion >= new Version(2, 8, 1);
var isRecentMovie = remoteMovie.Movie.IsRecentMovie;
var moveToTop = (isRecentMovie && Settings.RecentMoviePriority == (int)QBittorrentPriority.First) || (!isRecentMovie && Settings.OlderMoviePriority == (int)QBittorrentPriority.First);
var forceStart = (QBittorrentState)Settings.InitialState == QBittorrentState.ForceStart;
try Proxy.AddTorrentFromFile(filename, fileContent, addHasSetShareLimits ? remoteMovie.SeedConfiguration : null, Settings);
if ((!addHasSetShareLimits && setShareLimits) || moveToTop || forceStart)
{ {
var isRecentMovie = remoteMovie.Movie.IsRecentMovie; if (!WaitForTorrent(hash))
{
return hash;
}
if ((isRecentMovie && Settings.RecentMoviePriority == (int)QBittorrentPriority.First) || if (!addHasSetShareLimits && setShareLimits)
(!isRecentMovie && Settings.OlderMoviePriority == (int)QBittorrentPriority.First))
{ {
Proxy.MoveTorrentToTopInQueue(hash.ToLower(), Settings); Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.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 (remoteMovie.SeedConfiguration != null && (remoteMovie.SeedConfiguration.Ratio.HasValue || remoteMovie.SeedConfiguration.SeedTime.HasValue)) while (count != 0)
{ {
Proxy.SetTorrentSeedingConfiguration(hash.ToLower(), remoteMovie.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"; public override string Name => "qBittorrent";
@ -488,29 +562,6 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
return null; 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) protected TimeSpan? GetRemainingTime(QBittorrentTorrent torrent)
{ {
if (torrent.Eta < 0 || torrent.Eta > 365 * 24 * 3600) if (torrent.Eta < 0 || torrent.Eta > 365 * 24 * 3600)

@ -18,8 +18,8 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings); QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings);
List<QBittorrentTorrentFile> GetTorrentFiles(string hash, QBittorrentSettings settings); List<QBittorrentTorrentFile> GetTorrentFiles(string hash, QBittorrentSettings settings);
void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings); void AddTorrentFromUrl(string torrentUrl, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings);
void AddTorrentFromFile(string fileName, byte[] fileContent, QBittorrentSettings settings); void AddTorrentFromFile(string fileName, byte[] fileContent, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings);
void RemoveTorrent(string hash, bool removeData, QBittorrentSettings settings); void RemoveTorrent(string hash, bool removeData, QBittorrentSettings settings);
void SetTorrentLabel(string hash, string label, QBittorrentSettings settings); void SetTorrentLabel(string hash, string label, QBittorrentSettings settings);
@ -35,12 +35,13 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
public interface IQBittorrentProxySelector public interface IQBittorrentProxySelector
{ {
IQBittorrentProxy GetProxy(QBittorrentSettings settings, bool force = false); IQBittorrentProxy GetProxy(QBittorrentSettings settings, bool force = false);
Version GetApiVersion(QBittorrentSettings settings, bool force = false);
} }
public class QBittorrentProxySelector : IQBittorrentProxySelector public class QBittorrentProxySelector : IQBittorrentProxySelector
{ {
private readonly IHttpClient _httpClient; private readonly IHttpClient _httpClient;
private readonly ICached<IQBittorrentProxy> _proxyCache; private readonly ICached<Tuple<IQBittorrentProxy, Version>> _proxyCache;
private readonly Logger _logger; private readonly Logger _logger;
private readonly IQBittorrentProxy _proxyV1; private readonly IQBittorrentProxy _proxyV1;
@ -53,7 +54,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
Logger logger) Logger logger)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_proxyCache = cacheManager.GetCache<IQBittorrentProxy>(GetType()); _proxyCache = cacheManager.GetCache<Tuple<IQBittorrentProxy, Version>>(GetType());
_logger = logger; _logger = logger;
_proxyV1 = proxyV1; _proxyV1 = proxyV1;
@ -61,6 +62,16 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
} }
public IQBittorrentProxy GetProxy(QBittorrentSettings settings, bool force) public IQBittorrentProxy GetProxy(QBittorrentSettings settings, bool force)
{
return GetProxyCache(settings, force).Item1;
}
public Version GetApiVersion(QBittorrentSettings settings, bool force)
{
return GetProxyCache(settings, force).Item2;
}
private Tuple<IQBittorrentProxy, Version> GetProxyCache(QBittorrentSettings settings, bool force)
{ {
var proxyKey = $"{settings.Host}_{settings.Port}"; var proxyKey = $"{settings.Host}_{settings.Port}";
@ -72,18 +83,18 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
return _proxyCache.Get(proxyKey, () => FetchProxy(settings), TimeSpan.FromMinutes(10.0)); return _proxyCache.Get(proxyKey, () => FetchProxy(settings), TimeSpan.FromMinutes(10.0));
} }
private IQBittorrentProxy FetchProxy(QBittorrentSettings settings) private Tuple<IQBittorrentProxy, Version> FetchProxy(QBittorrentSettings settings)
{ {
if (_proxyV2.IsApiSupported(settings)) if (_proxyV2.IsApiSupported(settings))
{ {
_logger.Trace("Using qbitTorrent API v2"); _logger.Trace("Using qbitTorrent API v2");
return _proxyV2; return Tuple.Create(_proxyV2, _proxyV2.GetApiVersion(settings));
} }
if (_proxyV1.IsApiSupported(settings)) if (_proxyV1.IsApiSupported(settings))
{ {
_logger.Trace("Using qbitTorrent API v1"); _logger.Trace("Using qbitTorrent API v1");
return _proxyV1; return Tuple.Create(_proxyV1, _proxyV1.GetApiVersion(settings));
} }
throw new DownloadClientException("Unable to determine qBittorrent API version"); throw new DownloadClientException("Unable to determine qBittorrent API version");

@ -113,7 +113,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
return response; return response;
} }
public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings) public void AddTorrentFromUrl(string torrentUrl, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
{ {
var request = BuildRequest(settings).Resource("/command/download") var request = BuildRequest(settings).Resource("/command/download")
.Post() .Post()
@ -124,7 +124,12 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
request.AddFormParameter("category", settings.MovieCategory); request.AddFormParameter("category", settings.MovieCategory);
} }
if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause) // Note: ForceStart is handled by separate api call
if ((QBittorrentState)settings.InitialState == QBittorrentState.Start)
{
request.AddFormParameter("paused", false);
}
else if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause)
{ {
request.AddFormParameter("paused", true); request.AddFormParameter("paused", true);
} }
@ -138,7 +143,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
} }
} }
public void AddTorrentFromFile(string fileName, byte[] fileContent, QBittorrentSettings settings) public void AddTorrentFromFile(string fileName, byte[] fileContent, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
{ {
var request = BuildRequest(settings).Resource("/command/upload") var request = BuildRequest(settings).Resource("/command/upload")
.Post() .Post()
@ -149,9 +154,14 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
request.AddFormParameter("category", settings.MovieCategory); request.AddFormParameter("category", settings.MovieCategory);
} }
if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause) // Note: ForceStart is handled by separate api call
if ((QBittorrentState)settings.InitialState == QBittorrentState.Start)
{ {
request.AddFormParameter("paused", "true"); request.AddFormParameter("paused", false);
}
else if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause)
{
request.AddFormParameter("paused", true);
} }
var result = ProcessRequest(request, settings); var result = ProcessRequest(request, settings);

@ -119,7 +119,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
return response; return response;
} }
public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings) public void AddTorrentFromUrl(string torrentUrl, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
{ {
var request = BuildRequest(settings).Resource("/api/v2/torrents/add") var request = BuildRequest(settings).Resource("/api/v2/torrents/add")
.Post() .Post()
@ -129,11 +129,21 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
request.AddFormParameter("category", settings.MovieCategory); request.AddFormParameter("category", settings.MovieCategory);
} }
if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause) // Note: ForceStart is handled by separate api call
if ((QBittorrentState)settings.InitialState == QBittorrentState.Start)
{
request.AddFormParameter("paused", false);
}
else if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause)
{ {
request.AddFormParameter("paused", true); request.AddFormParameter("paused", true);
} }
if (seedConfiguration != null)
{
AddTorrentSeedingFormParameters(request, seedConfiguration, settings);
}
var result = ProcessRequest(request, settings); var result = ProcessRequest(request, settings);
// Note: Older qbit versions returned nothing, so we can't do != "Ok." here. // Note: Older qbit versions returned nothing, so we can't do != "Ok." here.
@ -143,7 +153,7 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
} }
} }
public void AddTorrentFromFile(string fileName, byte[] fileContent, QBittorrentSettings settings) public void AddTorrentFromFile(string fileName, byte[] fileContent, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
{ {
var request = BuildRequest(settings).Resource("/api/v2/torrents/add") var request = BuildRequest(settings).Resource("/api/v2/torrents/add")
.Post() .Post()
@ -154,9 +164,19 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
request.AddFormParameter("category", settings.MovieCategory); request.AddFormParameter("category", settings.MovieCategory);
} }
if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause) // Note: ForceStart is handled by separate api call
if ((QBittorrentState)settings.InitialState == QBittorrentState.Start)
{
request.AddFormParameter("paused", false);
}
else if ((QBittorrentState)settings.InitialState == QBittorrentState.Pause)
{
request.AddFormParameter("paused", true);
}
if (seedConfiguration != null)
{ {
request.AddFormParameter("paused", "true"); AddTorrentSeedingFormParameters(request, seedConfiguration, settings);
} }
var result = ProcessRequest(request, settings); var result = ProcessRequest(request, settings);
@ -205,16 +225,29 @@ namespace NzbDrone.Core.Download.Clients.QBittorrent
return Json.Deserialize<Dictionary<string, QBittorrentLabel>>(ProcessRequest(request, settings)); return Json.Deserialize<Dictionary<string, QBittorrentLabel>>(ProcessRequest(request, settings));
} }
public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings) private void AddTorrentSeedingFormParameters(HttpRequestBuilder request, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
{ {
var ratioLimit = seedConfiguration.Ratio.HasValue ? seedConfiguration.Ratio : -2; var ratioLimit = seedConfiguration.Ratio.HasValue ? seedConfiguration.Ratio : -2;
var seedingTimeLimit = seedConfiguration.SeedTime.HasValue ? (long)seedConfiguration.SeedTime.Value.TotalMinutes : -2; var seedingTimeLimit = seedConfiguration.SeedTime.HasValue ? (long)seedConfiguration.SeedTime.Value.TotalMinutes : -2;
if (ratioLimit != -2)
{
request.AddFormParameter("ratioLimit", ratioLimit);
}
if (seedingTimeLimit != -2)
{
request.AddFormParameter("seedingTimeLimit", seedingTimeLimit);
}
}
public void SetTorrentSeedingConfiguration(string hash, TorrentSeedConfiguration seedConfiguration, QBittorrentSettings settings)
{
var request = BuildRequest(settings).Resource("/api/v2/torrents/setShareLimits") var request = BuildRequest(settings).Resource("/api/v2/torrents/setShareLimits")
.Post() .Post()
.AddFormParameter("hashes", hash) .AddFormParameter("hashes", hash);
.AddFormParameter("ratioLimit", ratioLimit)
.AddFormParameter("seedingTimeLimit", seedingTimeLimit); AddTorrentSeedingFormParameters(request, seedConfiguration, settings);
try try
{ {

Loading…
Cancel
Save