由于你使用的是 Debian 且有 root 权限,按照以下步骤操作:
1. 安装 Python 环境和库
pip3 install qbittorrent-api --break-system-packages
# 更新并安装 python3 和 pip
apt update && apt install -y python3-pip
# 安装 qBittorrent 官方 API 库
pip3 install qbittorrent-api
2. 创建并编辑脚本
我们对之前的脚本做一点增强优化,增加对 stalledDL(停滞下载)的判定:
mkdir /home/scripts && vim /home/scripts/qbt_cleaner.py
粘贴以下内容(请根据你的 qbt 修改账号密码):
import time
from qbittorrentapi import Client
# --- 配置区域 ---
QBT_HOST = '127.0.0.1'
QBT_PORT = 8080 # 你的 qbt 端口
QBT_USER = 'admin'
QBT_PASS = 'adminadmin'
# --- 黑名单格式定义 ---
BAD_EXTENSIONS = ('.iso', '.vob', '.ifo', '.bup','m2ts')
# ----------------
def clean_qbt():
qbt_client = Client(host=QBT_HOST, port=QBT_PORT, username=QBT_USER, password=QBT_PASS)
try:
qbt_client.auth_log_in()
torrents = qbt_client.torrents_info()
for torrent in torrents:
# 1. 逻辑:Tracker 报错清理 (针对你提到的死种)
trackers = qbt_client.torrents_trackers(torrent.hash)
all_failed = all(t.status == 4 for t in trackers if t.url.startswith(('http', 'udp')))
if all_failed and len(trackers) > 0:
print(f"[清理] Tracker 故障: {torrent.name}")
qbt_client.torrents_delete(delete_files=True, torrent_hashes=torrent.hash)
continue
# 2. 逻辑:识别原盘或 ISO 格式 (针对你截图的问题)
# 获取种子内部文件列表
files = qbt_client.torrents_files(torrent.hash)
is_bad_format = any(f.name.lower().endswith(BAD_EXTENSIONS) for f in files)
if is_bad_format:
print(f"[清理] 发现原盘/ISO 格式,正在剔除: {torrent.name}")
qbt_client.torrents_delete(delete_files=True, torrent_hashes=torrent.hash)
continue
# 3. 逻辑:0 速度停滞清理 (stalledDL)
if torrent.state == 'stalledDL' and torrent.dlspeed == 0:
print(f"[清理] 停滞无速度: {torrent.name}")
qbt_client.torrents_delete(delete_files=True, torrent_hashes=torrent.hash)
except Exception as e:
print(f"发生错误: {e}")
if __name__ == "__main__":
clean_qbt()
3. 测试运行
python3 /home/scripts/qbt_cleaner.py
查看输出,看它是否能识别并删除你 qbt 里那些卡住的任务。
4. 设置定时任务(每 10 分钟自动清理一次)
crontab -e
添加一行:
*/1 * * * * /usr/bin/python3 /home/scripts/qbt_cleaner.py >> /var/log/qbt_clean.log 2>&1
