qBittorrent helpers
Install the package and its dependencies in your virtual environment:
uv sync --project .
Use the Qbittorrent class:
from torrent_api import Qbittorrent
instance = Qbittorrent("localhost:8080", "example", "your-api-key")
# Either limit may be omitted. Seed time is measured in seconds.
removed = instance.remove_torrents(seed_time=7 * 86400, ratio=2.0)
instance.show_and_copy_torrent_contents("exact torrent name or hash")
instance.show_torrents(id=True) # Include torrent hashes in the listing.
remove_torrents removes torrents exceeding either limit and deletes their
downloaded files by default. Pass delete_files=False to retain the files. It
returns the matching torrent records after a successful deletion.
show_and_copy_torrent_contents prints files whose progress meets the threshold
(default: 1.0). Pass copy_to with a destination directory to also copy files
that are 100% complete there, preserving their torrent-relative paths. Names
must match exactly; use the hash if multiple torrents have the same name.
For container paths, pass path_mappings={"/downloads": "/host/downloads"}
to Qbittorrent to translate save paths when copying. Unmapped paths are used
as-is. Copies require the qBittorrent host to be local to the process.
CLI and debugging
Set QBITTORRENT_PRIVATE_API_KEY and QBITTORRENT_PUBLIC_API_KEY in the
environment, then run torrent-api or python -m torrent_api.cli. To load
them from the local .env file in a POSIX shell, run:
set -a
. ./.env
set +a
torrent-api
The CLI uses the existing private and public instance addresses and supports
--log-level (TRACE, DEBUG, INFO, WARNING, ERROR, or CRITICAL; default INFO),
--sort, --asc, --force-start, --show-id, --id (torrent name or hash
for contents), --progress-threshold, and --copy-to (a destination directory
for completed files). Add --dry-run to log planned copies without writing
files. The VS Code launch configuration
uses the workspace main.py and loads .env automatically. Keep this file
private; it is ignored by Git.
The CLI uses the qBittorrent host mount paths from the repository's Docker
Compose configuration; update those mappings if the mounts change.
At INFO level, startup logs include the working directory and the directory
containing the entry point.
Tests
Run python run_tests.py to discover and run all unittests. In VS Code, use
the Torrent API: Run tests launch configuration or unittest discovery when
this directory is opened as the workspace and the environment with the
installed package is selected.
Lint and build
Install the optional tools and build a single-file executable:
uv sync --project . --extra build --extra lint
uv run --project . ruff check src tests main.py run_tests.py
uv run --project . python -m PyInstaller --noconfirm torrent_api.spec
./dist/torrent_api --help
The VS Code build task runs the same PyInstaller command using .venv. Builds
are specific to the OS and architecture on which they run. The Gitea workflow
tests and lints on pushes and pull requests; on a successful push to main,
it builds the executable and installs it to ~/tools/torrent_api on the same
self-hosted runners as ffmpeg_handling.