如何检测 qBittorrent 是否正在下载?

Ric*_*ard 4 windows linux batch shell-script qbittorrent

如何编写批处理/脚本文件来查询qBittorrent以查明它是否正在下载某些内容?

根据结果​​,我想执行进一步的命令。

Ric*_*ard 5

下面是两段代码(一段适用于 Windows,一段适用于 Linux),它们使用qBittorrent v4.1(或更高版本)API根据是否正在下载内容返回一条消息。

视窗

@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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

关于两个版本的一些注意事项

  • 您需要在 qBittorrent 中启用 Web UI 并设置用户名和密码。
  • 该代码包含用户名admin和密码secret,您需要更改这些
  • 代码连接到localhost:8080,您需要将其所有实例更改为正确的计算机名称/IP 和端口号
  • 我们创建一个 cookie jar(称为cookies.txt)来保存 SID。这是在成功登录后提供给我们的,并且在执行进一步命令时必须发送。一旦我们完成它,cookie jar 就会被删除。
  • 没有错误处理,如果出现故障,它可能(并且可能错误地)报告“正在下载某些内容”
  • stalledDL此代码考虑、downloading和的状态metaDL为正在下载。您可以通过查看此处记录的各种状态来更改此设置。
  • jq从 qBittorrent API 获取 JSON 输出,并以更易于阅读/解析的方式对其进行格式化。

Windows 版本特有的一些要点

  • 如果您没有运行 Windows 10,那么您需要安装curl
  • 您需要下载jq,将其重命名为 justjq.exe并将其放在与此脚本相同的文件夹中 - 我个人将其放入,C:\Windows\System32以便它可以在任何文件夹中使用
  • jq这是至关重要的,因为 qBittorrent 的输出可能包含极长的行,findstr无法处理(并引发错误)。

Linux 版本特有的一些要点

  • 该代码使用了curl,通常默认情况下可用。如果没有,那么在 Debian 上你可以使用sudo apt-get install curl
  • 您需要安装jq它才能工作,并且它在大多数存储库中都可用。在 Debian 上这是sudo apt-get install jq
  • 如果您觉得jq技术上不需要,egrep可以修改以过滤状态和值