1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

Fixed: Detecting if qbittorrent seeding time limit has been reached

This commit is contained in:
Taloth Saldono 2019-03-23 22:40:03 +01:00
parent e52fcf843c
commit e2b91e5dc4
6 changed files with 153 additions and 87 deletions

View File

@ -99,15 +99,18 @@ protected void GivenHighPriority()
Subject.Definition.Settings.As<QBittorrentSettings>().RecentTvPriority = (int)QBittorrentPriority.First; Subject.Definition.Settings.As<QBittorrentSettings>().RecentTvPriority = (int)QBittorrentPriority.First;
} }
protected void GivenMaxRatio(float maxRatio, bool removeOnMaxRatio = true) protected void GivenGlobalSeedLimits(float maxRatio, int maxSeedingTime = -1, bool removeOnMaxRatio = false)
{ {
Mocker.GetMock<IQBittorrentProxy>() Mocker.GetMock<IQBittorrentProxy>()
.Setup(s => s.GetConfig(It.IsAny<QBittorrentSettings>())) .Setup(s => s.GetConfig(It.IsAny<QBittorrentSettings>()))
.Returns(new QBittorrentPreferences .Returns(new QBittorrentPreferences
{ {
RemoveOnMaxRatio = removeOnMaxRatio, RemoveOnMaxRatio = removeOnMaxRatio,
MaxRatio = maxRatio MaxRatio = maxRatio,
}); MaxRatioEnabled = maxRatio >= 0,
MaxSeedingTime = maxSeedingTime,
MaxSeedingTimeEnabled = maxSeedingTime >= 0
});
} }
protected virtual void GivenTorrents(List<QBittorrentTorrent> torrents) protected virtual void GivenTorrents(List<QBittorrentTorrent> torrents)
@ -371,7 +374,7 @@ public void Download_should_handle_http_redirect_to_torrent()
[Test] [Test]
public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio_not_reached() public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio_not_reached()
{ {
GivenMaxRatio(1.0f); GivenGlobalSeedLimits(1.0f);
var torrent = new QBittorrentTorrent var torrent = new QBittorrentTorrent
{ {
@ -392,11 +395,11 @@ public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio
item.CanMoveFiles.Should().BeFalse(); item.CanMoveFiles.Should().BeFalse();
} }
[Test] protected virtual QBittorrentTorrent GivenCompletedTorrent(
public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio_reached_and_not_paused() string state = "pausedUP",
float ratio = 0.1f, float ratioLimit = -2,
int seedingTime = 1, int seedingTimeLimit = -2)
{ {
GivenMaxRatio(1.0f);
var torrent = new QBittorrentTorrent var torrent = new QBittorrentTorrent
{ {
Hash = "HASH", Hash = "HASH",
@ -404,12 +407,32 @@ public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio
Size = 1000, Size = 1000,
Progress = 1.0, Progress = 1.0,
Eta = 8640000, Eta = 8640000,
State = "uploading", State = state,
Label = "", Label = "",
SavePath = "", SavePath = "",
Ratio = 1.0f Ratio = ratio,
RatioLimit = ratioLimit,
SeedingTimeLimit = seedingTimeLimit
}; };
GivenTorrents(new List<QBittorrentTorrent> { torrent });
GivenTorrents(new List<QBittorrentTorrent>() { torrent });
Mocker.GetMock<IQBittorrentProxy>()
.Setup(s => s.GetTorrentProperties("HASH", It.IsAny<QBittorrentSettings>()))
.Returns(new QBittorrentTorrentProperties
{
Hash = "HASH",
SeedingTime = seedingTime
});
return torrent;
}
[Test]
public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio_reached_and_not_paused()
{
GivenGlobalSeedLimits(1.0f);
GivenCompletedTorrent("uploading", ratio: 1.0f);
var item = Subject.GetItems().Single(); var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeFalse(); item.CanBeRemoved.Should().BeFalse();
@ -419,21 +442,8 @@ public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio
[Test] [Test]
public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio_is_not_set() public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio_is_not_set()
{ {
GivenMaxRatio(1.0f, false); GivenGlobalSeedLimits(-1);
GivenCompletedTorrent("pausedUP", ratio: 1.0f);
var torrent = new QBittorrentTorrent
{
Hash = "HASH",
Name = _title,
Size = 1000,
Progress = 1.0,
Eta = 8640000,
State = "uploading",
Label = "",
SavePath = "",
Ratio = 1.0f
};
GivenTorrents(new List<QBittorrentTorrent> { torrent });
var item = Subject.GetItems().Single(); var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeFalse(); item.CanBeRemoved.Should().BeFalse();
@ -443,21 +453,8 @@ public void should_not_be_removable_and_should_not_allow_move_files_if_max_ratio
[Test] [Test]
public void should_be_removable_and_should_allow_move_files_if_max_ratio_reached_and_paused() public void should_be_removable_and_should_allow_move_files_if_max_ratio_reached_and_paused()
{ {
GivenMaxRatio(1.0f); GivenGlobalSeedLimits(1.0f);
GivenCompletedTorrent("pausedUP", ratio: 1.0f);
var torrent = new QBittorrentTorrent
{
Hash = "HASH",
Name = _title,
Size = 1000,
Progress = 1.0,
Eta = 8640000,
State = "pausedUP",
Label = "",
SavePath = "",
Ratio = 1.0f
};
GivenTorrents(new List<QBittorrentTorrent> { torrent });
var item = Subject.GetItems().Single(); var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeTrue(); item.CanBeRemoved.Should().BeTrue();
@ -467,22 +464,8 @@ public void should_be_removable_and_should_allow_move_files_if_max_ratio_reached
[Test] [Test]
public void should_be_removable_and_should_allow_move_files_if_overridden_max_ratio_reached_and_paused() public void should_be_removable_and_should_allow_move_files_if_overridden_max_ratio_reached_and_paused()
{ {
GivenMaxRatio(2.0f); GivenGlobalSeedLimits(2.0f);
GivenCompletedTorrent("pausedUP", ratio: 1.0f, ratioLimit: 0.8f);
var torrent = new QBittorrentTorrent
{
Hash = "HASH",
Name = _title,
Size = 1000,
Progress = 1.0,
Eta = 8640000,
State = "pausedUP",
Label = "",
SavePath = "",
Ratio = 1.0f,
RatioLimit = 0.8f
};
GivenTorrents(new List<QBittorrentTorrent> { torrent });
var item = Subject.GetItems().Single(); var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeTrue(); item.CanBeRemoved.Should().BeTrue();
@ -492,33 +475,75 @@ public void should_be_removable_and_should_allow_move_files_if_overridden_max_ra
[Test] [Test]
public void should_not_be_removable_if_overridden_max_ratio_not_reached_and_paused() public void should_not_be_removable_if_overridden_max_ratio_not_reached_and_paused()
{ {
GivenMaxRatio(0.2f); GivenGlobalSeedLimits(0.2f);
GivenCompletedTorrent("pausedUP", ratio: 0.5f, ratioLimit: 0.8f);
var torrent = new QBittorrentTorrent var item = Subject.GetItems().Single();
{ item.CanBeRemoved.Should().BeFalse();
Hash = "HASH", item.CanMoveFiles.Should().BeFalse();
Name = _title, }
Size = 1000,
Progress = 1.0,
Eta = 8640000, [Test]
State = "pausedUP", public void should_not_be_removable_and_should_not_allow_move_files_if_max_seedingtime_reached_and_not_paused()
Label = "", {
SavePath = "", GivenGlobalSeedLimits(-1, 20);
Ratio = 0.5f, GivenCompletedTorrent("uploading", ratio: 2.0f, seedingTime: 30);
RatioLimit = 0.8f
};
GivenTorrents(new List<QBittorrentTorrent> { torrent });
var item = Subject.GetItems().Single(); var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeFalse(); item.CanBeRemoved.Should().BeFalse();
item.CanMoveFiles.Should().BeFalse(); item.CanMoveFiles.Should().BeFalse();
} }
[Test]
public void should_be_removable_and_should_allow_move_files_if_max_seedingtime_reached_and_paused()
{
GivenGlobalSeedLimits(-1, 20);
GivenCompletedTorrent("pausedUP", ratio: 2.0f, seedingTime: 20);
var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeTrue();
item.CanMoveFiles.Should().BeTrue();
}
[Test]
public void should_be_removable_and_should_allow_move_files_if_overridden_max_seedingtime_reached_and_paused()
{
GivenGlobalSeedLimits(-1, 40);
GivenCompletedTorrent("pausedUP", ratio: 2.0f, seedingTime: 20, seedingTimeLimit: 10);
var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeTrue();
item.CanMoveFiles.Should().BeTrue();
}
[Test]
public void should_not_be_removable_if_overridden_max_seedingtime_not_reached_and_paused()
{
GivenGlobalSeedLimits(-1, 20);
GivenCompletedTorrent("pausedUP", ratio: 2.0f, seedingTime: 30, seedingTimeLimit: 40);
var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeFalse();
item.CanMoveFiles.Should().BeFalse();
}
[Test]
public void should_be_removable_and_should_allow_move_files_if_max_seedingtime_reached_but_ratio_not_and_paused()
{
GivenGlobalSeedLimits(2.0f, 20);
GivenCompletedTorrent("pausedUP", ratio: 1.0f, seedingTime: 30);
var item = Subject.GetItems().Single();
item.CanBeRemoved.Should().BeTrue();
item.CanMoveFiles.Should().BeTrue();
}
[Test] [Test]
public void should_get_category_from_the_category_if_set() public void should_get_category_from_the_category_if_set()
{ {
const string category = "tv-sonarr"; const string category = "tv-sonarr";
GivenMaxRatio(1.0f); GivenGlobalSeedLimits(1.0f);
var torrent = new QBittorrentTorrent var torrent = new QBittorrentTorrent
{ {
@ -543,7 +568,7 @@ public void should_get_category_from_the_category_if_set()
public void should_get_category_from_the_label_if_the_category_is_not_available() public void should_get_category_from_the_label_if_the_category_is_not_available()
{ {
const string category = "tv-sonarr"; const string category = "tv-sonarr";
GivenMaxRatio(1.0f); GivenGlobalSeedLimits(1.0f);
var torrent = new QBittorrentTorrent var torrent = new QBittorrentTorrent
{ {

View File

@ -134,7 +134,6 @@ public override IEnumerable<DownloadClientItem> GetItems()
// Removal also requires the torrent to be paused, in case a higher max ratio was set on the torrent itself (which is not exposed by the api). // Removal also requires the torrent to be paused, in case a higher max ratio was set on the torrent itself (which is not exposed by the api).
item.CanMoveFiles = item.CanBeRemoved = (torrent.State == "pausedUP" && HasReachedSeedLimit(torrent, config)); item.CanMoveFiles = item.CanBeRemoved = (torrent.State == "pausedUP" && HasReachedSeedLimit(torrent, config));
if (!item.OutputPath.IsEmpty && item.OutputPath.FileName != torrent.Name) if (!item.OutputPath.IsEmpty && item.OutputPath.FileName != torrent.Name)
{ {
item.OutputPath += torrent.Name; item.OutputPath += torrent.Name;
@ -387,23 +386,40 @@ protected bool HasReachedSeedLimit(QBittorrentTorrent torrent, QBittorrentPrefer
{ {
if (torrent.RatioLimit >= 0) if (torrent.RatioLimit >= 0)
{ {
if (torrent.Ratio < torrent.RatioLimit) return false; if (torrent.Ratio >= torrent.RatioLimit) return true;
} }
else if (torrent.RatioLimit == -2 && config.MaxRatioEnabled) else if (torrent.RatioLimit == -2 && config.MaxRatioEnabled)
{ {
if (torrent.Ratio < config.MaxRatio) return false; if (torrent.Ratio >= config.MaxRatio) return true;
} }
if (torrent.SeedingTimeLimit >= 0) if (torrent.SeedingTimeLimit >= 0)
{ {
if (torrent.SeedingTime < torrent.SeedingTimeLimit) return false; if (!torrent.SeedingTime.HasValue)
{
FetchTorrentDetails(torrent);
}
if (torrent.SeedingTime >= torrent.SeedingTimeLimit) return true;
} }
else if (torrent.RatioLimit == -2 && config.MaxSeedingTimeEnabled) else if (torrent.SeedingTimeLimit == -2 && config.MaxSeedingTimeEnabled)
{ {
if (torrent.SeedingTime < config.MaxSeedingTime) return false; if (!torrent.SeedingTime.HasValue)
{
FetchTorrentDetails(torrent);
}
if (torrent.SeedingTime >= config.MaxSeedingTime) return true;
} }
return true; return false;
}
protected void FetchTorrentDetails(QBittorrentTorrent torrent)
{
var torrentProperties = Proxy.GetTorrentProperties(torrent.Hash, Settings);
torrent.SeedingTime = torrentProperties.SeedingTime;
} }
} }
} }

View File

@ -16,6 +16,7 @@ public interface IQBittorrentProxy
string GetVersion(QBittorrentSettings settings); string GetVersion(QBittorrentSettings settings);
QBittorrentPreferences GetConfig(QBittorrentSettings settings); QBittorrentPreferences GetConfig(QBittorrentSettings settings);
List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings); List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings);
QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings);
void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings); void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings);
void AddTorrentFromFile(string fileName, Byte[] fileContent, QBittorrentSettings settings); void AddTorrentFromFile(string fileName, Byte[] fileContent, QBittorrentSettings settings);

View File

@ -95,6 +95,14 @@ public List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings)
return response; return response;
} }
public QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings)
{
var request = BuildRequest(settings).Resource($"/query/propertiesGeneral/{hash}");
var response = ProcessRequest<QBittorrentTorrentProperties>(request, settings);
return response;
}
public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings) public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings)
{ {
var request = BuildRequest(settings).Resource("/command/download") var request = BuildRequest(settings).Resource("/command/download")

View File

@ -93,6 +93,15 @@ public List<QBittorrentTorrent> GetTorrents(QBittorrentSettings settings)
return response; return response;
} }
public QBittorrentTorrentProperties GetTorrentProperties(string hash, QBittorrentSettings settings)
{
var request = BuildRequest(settings).Resource("/api/v2/torrents/properties")
.AddQueryParam("hash", hash);
var response = ProcessRequest<QBittorrentTorrentProperties>(request, settings);
return response;
}
public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings) public void AddTorrentFromUrl(string torrentUrl, QBittorrentSettings settings)
{ {
var request = BuildRequest(settings).Resource("/api/v2/torrents/add") var request = BuildRequest(settings).Resource("/api/v2/torrents/add")

View File

@ -30,10 +30,17 @@ public class QBittorrentTorrent
public float RatioLimit { get; set; } = -2; public float RatioLimit { get; set; } = -2;
[JsonProperty(PropertyName = "seeding_time")] [JsonProperty(PropertyName = "seeding_time")]
public long SeedingTime { get; set; } // Torrent seeding time public long? SeedingTime { get; set; } // Torrent seeding time (not provided by the list api)
[JsonProperty(PropertyName = "seeding_time_limit")] // Per torrent seeding time limit (-2 = use global, -1 = unlimited) [JsonProperty(PropertyName = "seeding_time_limit")] // Per torrent seeding time limit (-2 = use global, -1 = unlimited)
public long SeedingTimeLimit { get; set; } = -2; public long SeedingTimeLimit { get; set; } = -2;
}
public class QBittorrentTorrentProperties
{
public string Hash { get; set; } // Torrent hash
[JsonProperty(PropertyName = "seeding_time")]
public long SeedingTime { get; set; } // Torrent seeding time
} }
} }