Skip to content

Commit

Permalink
Support stop endpoint instead of Pause in QBT 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Webreaper committed Nov 2, 2024
1 parent fd9d3ff commit 7d3cdfa
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,6 @@ MigrationBackup/
*.db
*.zip
.DS_Store
.idea/.idea.QbtManager/.idea/encodings.xml
.idea/.idea.QbtManager/.idea/indexLayout.xml
.idea/.idea.QbtManager/.idea/vcs.xml
13 changes: 13 additions & 0 deletions .idea/.idea.QbtManager/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions QbtManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public static void Main(string[] args)

if (service.SignIn())
{
service.GetQBTVersion();

Utils.Log("Getting Seeding Task list and mapping trackers...");
var tasks = service.GetTasks()
.OrderBy(x => x.name)
Expand Down
84 changes: 78 additions & 6 deletions QbtManager/qbtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class qbtService
{
private readonly RestClient client;
private readonly QBittorrentSettings settings;
private Version? qbtVersion;

public class Tracker
{
Expand Down Expand Up @@ -102,6 +103,21 @@ public bool SignIn()
return false;
}

public void GetQBTVersion()
{
// Dont use ?filter=completed here - we'll filter ourselves.
var versionStr = MakeRestRequest("/app/version", null);

if (!string.IsNullOrEmpty(versionStr))
{
if( versionStr.StartsWith("v") )
versionStr = versionStr.Substring(1);

qbtVersion = new Version(versionStr);
Utils.Log( $"QBT Version is: {qbtVersion}");
}
}

/// <summary>
/// Get the list of torrents
/// </summary>
Expand Down Expand Up @@ -164,10 +180,18 @@ public bool DownloadTorrent(string torrentUrl, string category)
/// <returns></returns>
public bool PauseTask(string[] taskIds)
{
// Handle the fact that pause => stop in QBT v5
var command = qbtVersion != null && qbtVersion.Major < 5 ? "pause" : "stop";
var parms = new Dictionary<string, string>();

parms["hashes"] = string.Join("|", taskIds);
return ExecuteCommand("/torrents/pause", parms);
foreach (var chunk in taskIds.Chunk(30))
{
parms["hashes"] = string.Join("|", chunk);
if (!ExecuteCommand($"/torrents/{command}", parms))
return false;
}

return true;
}

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


/// <summary>
/// Generic REST method handler.
/// </summary>
Expand All @@ -261,13 +286,60 @@ public bool ExecuteCommand(string requestMethod, IDictionary<string, string> par
/// <param name="parms"></param>
/// <param name="method"></param>
/// <returns></returns>
public T MakeRestRequest<T>(string requestMethod, IDictionary<string, string> parms, Method method = Method.Get) where T : new()
public string MakeRestRequest(string requestMethod, IDictionary<string, string>? parms)
{
var request = new RestRequest(requestMethod, method );
var request = new RestRequest(requestMethod, Method.Get );

foreach (var kvp in parms)
request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
if (parms != null)
{
foreach (var kvp in parms)
request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
}

try
{
var queryResult = client.Execute<string>(request);

if (queryResult != null)
{
if (queryResult.StatusCode != HttpStatusCode.OK)
{
Utils.Log("Error: {0} - {1}", queryResult.StatusCode, queryResult.Content);
}
else
{
return queryResult.Content;
}
}
else
Utils.Log("No valid queryResult.");
}
catch (Exception ex)
{
Utils.Log("Exception: {0}: {1}", ex.Message, ex);
}

return string.Empty;
}

/// <summary>
/// Generic REST method handler.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="requestMethod"></param>
/// <param name="parms"></param>
/// <param name="method"></param>
/// <returns></returns>
public T MakeRestRequest<T>(string requestMethod, IDictionary<string, string>? parms, Method method = Method.Get)
{
var request = new RestRequest(requestMethod, method );

if (parms != null)
{
foreach (var kvp in parms)
request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
}

try
{
var queryResult = client.Execute<T>(request);
Expand Down

0 comments on commit 7d3cdfa

Please sign in to comment.