2026-11-22 16:16:14 +01:00
|
|
|
# qBittorrent helpers
|
|
|
|
|
|
|
|
|
|
Install the package and its dependencies in your virtual environment:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
python -m pip install -e .
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Use the `Qbittorrent` class:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
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.get_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.
|
|
|
|
|
`get_torrent_contents` prints files whose progress meets the threshold
|
2026-11-22 16:48:35 +01:00
|
|
|
(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.
|
2026-11-22 16:16:14 +01:00
|
|
|
|
|
|
|
|
## 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:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
set -a
|
|
|
|
|
. ./.env
|
|
|
|
|
set +a
|
|
|
|
|
torrent-api
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
The CLI uses the existing private and public instance addresses and supports
|
|
|
|
|
`--sort`, `--asc`, `--force-start`, `--show-id`, `--id` (torrent name or hash
|
2026-11-22 16:48:35 +01:00
|
|
|
for contents), `--progress-threshold`, and `--copy-to` (a destination directory
|
|
|
|
|
for completed files). The VS Code launch configuration
|
2026-11-22 16:16:14 +01:00
|
|
|
uses the workspace `main.py` and loads `.env` automatically. Keep this file
|
|
|
|
|
private; it is ignored by Git.
|
|
|
|
|
|
|
|
|
|
## 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:
|
|
|
|
|
|
|
|
|
|
```sh
|
|
|
|
|
python -m pip install -e '.[build,lint]'
|
|
|
|
|
ruff check src tests main.py run_tests.py
|
|
|
|
|
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`.
|