Skip to content

Commit 7d3cdfa

Browse files
committed
Support stop endpoint instead of Pause in QBT 5.0
1 parent fd9d3ff commit 7d3cdfa

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,6 @@ MigrationBackup/
371371
*.db
372372
*.zip
373373
.DS_Store
374+
.idea/.idea.QbtManager/.idea/encodings.xml
375+
.idea/.idea.QbtManager/.idea/indexLayout.xml
376+
.idea/.idea.QbtManager/.idea/vcs.xml

.idea/.idea.QbtManager/.idea/.gitignore

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

QbtManager/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ public static void Main(string[] args)
105105

106106
if (service.SignIn())
107107
{
108+
service.GetQBTVersion();
109+
108110
Utils.Log("Getting Seeding Task list and mapping trackers...");
109111
var tasks = service.GetTasks()
110112
.OrderBy(x => x.name)

QbtManager/qbtService.cs

+78-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class qbtService
1818
{
1919
private readonly RestClient client;
2020
private readonly QBittorrentSettings settings;
21+
private Version? qbtVersion;
2122

2223
public class Tracker
2324
{
@@ -102,6 +103,21 @@ public bool SignIn()
102103
return false;
103104
}
104105

106+
public void GetQBTVersion()
107+
{
108+
// Dont use ?filter=completed here - we'll filter ourselves.
109+
var versionStr = MakeRestRequest("/app/version", null);
110+
111+
if (!string.IsNullOrEmpty(versionStr))
112+
{
113+
if( versionStr.StartsWith("v") )
114+
versionStr = versionStr.Substring(1);
115+
116+
qbtVersion = new Version(versionStr);
117+
Utils.Log( $"QBT Version is: {qbtVersion}");
118+
}
119+
}
120+
105121
/// <summary>
106122
/// Get the list of torrents
107123
/// </summary>
@@ -164,10 +180,18 @@ public bool DownloadTorrent(string torrentUrl, string category)
164180
/// <returns></returns>
165181
public bool PauseTask(string[] taskIds)
166182
{
183+
// Handle the fact that pause => stop in QBT v5
184+
var command = qbtVersion != null && qbtVersion.Major < 5 ? "pause" : "stop";
167185
var parms = new Dictionary<string, string>();
168186

169-
parms["hashes"] = string.Join("|", taskIds);
170-
return ExecuteCommand("/torrents/pause", parms);
187+
foreach (var chunk in taskIds.Chunk(30))
188+
{
189+
parms["hashes"] = string.Join("|", chunk);
190+
if (!ExecuteCommand($"/torrents/{command}", parms))
191+
return false;
192+
}
193+
194+
return true;
171195
}
172196

173197
/// <summary>
@@ -253,6 +277,7 @@ public bool ExecuteCommand(string requestMethod, IDictionary<string, string> par
253277
return queryResult.StatusCode == HttpStatusCode.OK;
254278
}
255279

280+
256281
/// <summary>
257282
/// Generic REST method handler.
258283
/// </summary>
@@ -261,13 +286,60 @@ public bool ExecuteCommand(string requestMethod, IDictionary<string, string> par
261286
/// <param name="parms"></param>
262287
/// <param name="method"></param>
263288
/// <returns></returns>
264-
public T MakeRestRequest<T>(string requestMethod, IDictionary<string, string> parms, Method method = Method.Get) where T : new()
289+
public string MakeRestRequest(string requestMethod, IDictionary<string, string>? parms)
265290
{
266-
var request = new RestRequest(requestMethod, method );
291+
var request = new RestRequest(requestMethod, Method.Get );
267292

268-
foreach (var kvp in parms)
269-
request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
293+
if (parms != null)
294+
{
295+
foreach (var kvp in parms)
296+
request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
297+
}
298+
299+
try
300+
{
301+
var queryResult = client.Execute<string>(request);
302+
303+
if (queryResult != null)
304+
{
305+
if (queryResult.StatusCode != HttpStatusCode.OK)
306+
{
307+
Utils.Log("Error: {0} - {1}", queryResult.StatusCode, queryResult.Content);
308+
}
309+
else
310+
{
311+
return queryResult.Content;
312+
}
313+
}
314+
else
315+
Utils.Log("No valid queryResult.");
316+
}
317+
catch (Exception ex)
318+
{
319+
Utils.Log("Exception: {0}: {1}", ex.Message, ex);
320+
}
321+
322+
return string.Empty;
323+
}
324+
325+
/// <summary>
326+
/// Generic REST method handler.
327+
/// </summary>
328+
/// <typeparam name="T"></typeparam>
329+
/// <param name="requestMethod"></param>
330+
/// <param name="parms"></param>
331+
/// <param name="method"></param>
332+
/// <returns></returns>
333+
public T MakeRestRequest<T>(string requestMethod, IDictionary<string, string>? parms, Method method = Method.Get)
334+
{
335+
var request = new RestRequest(requestMethod, method );
270336

337+
if (parms != null)
338+
{
339+
foreach (var kvp in parms)
340+
request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
341+
}
342+
271343
try
272344
{
273345
var queryResult = client.Execute<T>(request);

0 commit comments

Comments
 (0)