@@ -18,6 +18,7 @@ public class qbtService
18
18
{
19
19
private readonly RestClient client ;
20
20
private readonly QBittorrentSettings settings ;
21
+ private Version ? qbtVersion ;
21
22
22
23
public class Tracker
23
24
{
@@ -102,6 +103,21 @@ public bool SignIn()
102
103
return false ;
103
104
}
104
105
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
+
105
121
/// <summary>
106
122
/// Get the list of torrents
107
123
/// </summary>
@@ -164,10 +180,18 @@ public bool DownloadTorrent(string torrentUrl, string category)
164
180
/// <returns></returns>
165
181
public bool PauseTask ( string [ ] taskIds )
166
182
{
183
+ // Handle the fact that pause => stop in QBT v5
184
+ var command = qbtVersion != null && qbtVersion . Major < 5 ? "pause" : "stop" ;
167
185
var parms = new Dictionary < string , string > ( ) ;
168
186
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 ;
171
195
}
172
196
173
197
/// <summary>
@@ -253,6 +277,7 @@ public bool ExecuteCommand(string requestMethod, IDictionary<string, string> par
253
277
return queryResult . StatusCode == HttpStatusCode . OK ;
254
278
}
255
279
280
+
256
281
/// <summary>
257
282
/// Generic REST method handler.
258
283
/// </summary>
@@ -261,13 +286,60 @@ public bool ExecuteCommand(string requestMethod, IDictionary<string, string> par
261
286
/// <param name="parms"></param>
262
287
/// <param name="method"></param>
263
288
/// <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 )
265
290
{
266
- var request = new RestRequest ( requestMethod , method ) ;
291
+ var request = new RestRequest ( requestMethod , Method . Get ) ;
267
292
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 ) ;
270
336
337
+ if ( parms != null )
338
+ {
339
+ foreach ( var kvp in parms )
340
+ request . AddParameter ( kvp . Key , kvp . Value , ParameterType . GetOrPost ) ;
341
+ }
342
+
271
343
try
272
344
{
273
345
var queryResult = client . Execute < T > ( request ) ;
0 commit comments