自动删除 qBittorrent 下载完毕的任务

可能有更好更精简的实现方式,有脱裤子放屁的嫌疑,但符合我的日常使用习惯

准备

  • 一台有 crontab 的机器
    • Windows 也可以,把 bash 翻译成 PowerShell 然后扔进计划任务即可
  • https://github.com/fedarovich/qbittorrent-cli
    • 本来想自己糊一个只支持 Login、Add 和 Delete 操作的 qBittorrent WebAPI CLI Wrapper,结果发现了现成的
    • 使用 C# 实现,支持的操作很全面,跨平台,唯一的遗憾是没有提供单文件的 Release
  • qBittorrent 开启 WebUI

步骤

准备 qbt CLI

在 https://github.com/fedarovich/qbittorrent-cli/releases 下载对应平台的 二进制文件

如果不希望看到目录里杂乱的 dll 文件,也可以选择 clone 代码后自行编译,编译时需要添加参数:

  • -f net6
    • 指定dotnet 版本
  • -p:PublishSingleFile=true
    • 生成单文件
    • 即使是以单文件发布,Windows 版本也会自带两个 dll(Linux 则是一个 so 库文件, OSX 也带一个 dylib 库文件)
  • -r [win-x64/linux-x64/osx-x64/...] --sc
    • 指定对应平台

不需要对源码做任何更改即可成功编译,然后将 qbt 可执行文件添加到系统 PATH

也可以使用 Github Actions 自动编译,fork 之后添加 Actions 文件,写入以下内容

name: Publish

on:
  workflow_dispatch:
  push:
    branches: [ master ]
    paths-ignore:
      - '**/**.md'
      - '.github/**'

jobs:
  publish:
    name: Publish the app
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
    - name: Checkout source code
      uses: actions/[email protected]
    
    - name: Setup .NET
      uses: actions/[email protected]
      with:
        dotnet-version: 6.0.x
    
    - name: Publish
      run: |
        cd src/QBittorrent.CommandLineInterface
        dotnet restore
        dotnet publish -c release -f net6 -r win-x64 -o ~/bin/release/win-x64 -p:PublishSingleFile=true -p:DebugType=None -p:DebugSymbols=false --sc
        dotnet publish -c release -f net6 -r linux-x64 -o ~/bin/release/linux-x64 -p:PublishSingleFile=true -p:DebugType=None -p:DebugSymbols=false --sc
        dotnet publish -c release -f net6 -r osx-x64 -o ~/bin/release/osx-x64 -p:PublishSingleFile=true -p:DebugType=None -p:DebugSymbols=false --sc
      
    - name: Install Zip
      uses: montudor/[email protected]
      
    - name: Create Zip
      run: |
        zip -qq -r qbt-win-x64.zip win-x64/*
        zip -qq -r qbt-linux-x64.zip linux-x64/*
        zip -qq -r qbt-osx-x64.zip osx-x64/*
      working-directory: /home/runner/bin/release
      
    - name: Upload zip
      uses: actions/[email protected]
      with:
        name: My Artifact
        path: /home/runner/bin/release/*.zip
        if-no-files-found: warn
        
    - name: Create Release
      uses: "marvinpinto/[email protected]"
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        automatic_release_tag: "latest"
        prerelease: false
        files: /home/runner/bin/release/*.zip

获取到 qbt 后运行指令,配置URL、账户和密码

qbt settings set url [qBittorrent WebUI 的地址]
qbt settings set username [qBittorrent WebUI 的账户用户名]
qbt settings set password # 按照提示设置密码

配置完毕后会生成 settings.json,Windows 下位于 C:\Users\用户名\Appdata\Local.qbt,Linux 和 OSX 都在 ~/.qbt 下

自动删除脚本

需要安装 jq,Debian 可以通过执行

apt install jq

来安装

新建脚本 delDownloaded.sh,写入以下内容

#!/bin/bash

echo "- Start: $(date) -"

# allhashes=$(/path/to/qbt torrent list -F json | jq -r '.[] | .hash')
hashes=$(/path/to/qbt torrent list -F json | jq -r '.[] | select(.state == "uploading" or .state == "stalledUP") | .hash')

hashCnt=$(echo "$hashes" | wc -l)

if [ $hashCnt -gt 0 ]
then
        for hash in $hashes
        do
                echo Deleting $hash
                /path/to/qbt torrent delete $hash
        done
fi

echo "- End: $(date) -"

脚本会自动删除所有状态为  uploading 和 stalledUP 的任务

crontab

执行

crontab -e

添加新行

*/5 * * * * /usr/bin/bash /path/to/delDownloaded.sh > /dev/null 2>&1

每五分钟执行一次

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据