4

How can I write a batch/script file to query qBittorrent to find out whether or not it is downloading something?

Depending on the outcome, I would like to execute further commands.

4

Below are two pieces of code (one for Windows and one for Linux) which use the qBittorrent v4.1 (or later) API to return a message depending on whether or not something is downloading.

Windows

@echo off

rem Remove existing cookie jar
del /F %temp%\cookies.txt 2>nul

rem Login to qBittorrent
curl -s -b %temp%\cookies.txt -c %temp%\cookies.txt --header "Referer: http://localhost:8080" --data "username=admin&password=secret" http://localhost:8080/api/v2/auth/login >nul

rem Get list of downloading torrents
curl -s -b %temp%\cookies.txt -c %temp%\cookies.txt --header "Referer: http://localhost:8080" "http://localhost:8080/api/v2/torrents/info" | jq | findstr """state""" | findstr /R "stalledDL downloading metaDL" >nul

if errorlevel 0 (
    echo Something downloading
) else (
    echo Nothing downloading
)

rem Remove used cookie jar
del /F %temp%\cookies.txt 2>nul

Linux

#!/bin/bash

# Remove existing cookie jar
rm -f ~/.cookies.txt

# Login to qbittorrent
curl -s -b ~/.cookies.txt -c ~/.cookies.txt --header "Referer: http://localhost:8080" --data "username=admin&password=secret" http://localhost:8080/api/v2/auth/login >/dev/null 2>&1

# Get list of downloading torrents
if [[ $(curl -s -b ~/.cookies.txt -c ~/.cookies.txt --header "Referer: http://localhost:8080" "http://localhost:8080/api/v2/torrents/info" | jq . | grep "state" | egrep "(stalledDL|downloading|metaDL)") ]]; then
        echo "Something downloading"
else
        echo "Nothing downloading"
fi

# Remove used cookie jar
rm -f ~/.cookies.txt

Some points to note about both versions

  • You need to enable the web UI within qBittorrent and set a username and password.
  • The code has the username admin with the password secret, you'll want to change those
  • The code connects to localhost:8080, you'll need to change all instances of this to the correct machine name/IP and port number
  • We create a cookie jar (called cookies.txt) for holding the SID. This is given to us after a successful login and must be sent when executing further commands. Once we've finished with it, the cookie jar is deleted.
  • There's no error handling, if something fails it'll probably (and possibly incorrectly) report "Something downloading"
  • This code considers the states of stalledDL, downloading and metaDL to be downloading. You can change this by looking at the various states documented here.
  • jq takes the JSON ouput from the qBittorrent API and formats it in a way which is easier to read/parse.

Some points specific to the Windows version

  • If you aren't running Windows 10 then you'll need to install curl
  • You'll need to download jq, rename it to just jq.exe and place it in the same folder as this script - personally I put it in C:\Windows\System32 so it works from any folder
  • jq is essential because the output from qBittorrent can consist of extremely long lines, which findstr cannot handle (and throws an error).

Some points specific to the Linux version

  • The code used curl, which is usually available by default. If not, then on Debian you can use sudo apt-get install curl
  • You'll need to install jq for this to work and it's available in most repositories. On Debian this is sudo apt-get install jq
  • If you feel inclined jq isn't technically needed and the egrep can be modified to filter both the state and value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.