2024-11-09 00:18:47 +01:00
|
|
|
# /// script
|
|
|
|
# dependencies = [
|
|
|
|
# "qbittorrent-api",
|
|
|
|
# "docopt",
|
|
|
|
# ]
|
|
|
|
# ///
|
2024-10-20 19:34:45 +02:00
|
|
|
|
|
|
|
"""list_torrents.py
|
|
|
|
|
|
|
|
Description:
|
|
|
|
Fetch a list of torrents from a qBittorrent instance running on localhost.
|
|
|
|
The qBittorrent instance must be configured to allow login on localhost
|
|
|
|
without authentication. The output is formatted into a plaintext table.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
list_torrents.py
|
|
|
|
list_torrents.py -h
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h, --help show this help message and exit
|
|
|
|
"""
|
|
|
|
|
2024-11-09 00:18:47 +01:00
|
|
|
import qbittorrentapi
|
2024-10-20 19:34:45 +02:00
|
|
|
from docopt import docopt
|
|
|
|
|
|
|
|
|
|
|
|
# convert byte units
|
|
|
|
def human_bytes(input_bytes: int) -> str:
|
|
|
|
B = float(input_bytes)
|
|
|
|
KiB = float(1024)
|
|
|
|
MiB = float(KiB**2)
|
|
|
|
GiB = float(KiB**3)
|
|
|
|
TiB = float(KiB**4)
|
|
|
|
|
|
|
|
match B:
|
|
|
|
case B if B < KiB:
|
|
|
|
return "{0} {1}".format(B, "bytes" if 0 == B > 1 else "byte")
|
|
|
|
case B if KiB <= B <= MiB:
|
|
|
|
return "{0:.2f} KiB".format(B / KiB)
|
|
|
|
case B if MiB <= B <= GiB:
|
|
|
|
return "{0:.2f} MiB".format(B / MiB)
|
|
|
|
case B if GiB <= B <= TiB:
|
|
|
|
return "{0:.2f} GiB".format(B / GiB)
|
|
|
|
case B if TiB <= B:
|
|
|
|
return "{0:.2f} TiB".format(B / TiB)
|
|
|
|
case _:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2024-10-25 18:56:21 +02:00
|
|
|
def print_ssv():
|
2024-11-09 00:18:47 +01:00
|
|
|
with qbittorrentapi.Client(
|
|
|
|
host="localhost", port=8080, username="", password=""
|
|
|
|
) as qbt_client:
|
|
|
|
try:
|
|
|
|
qbt_client.auth_log_in()
|
|
|
|
except qbittorrentapi.LoginFailed as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
sorted_torrents = sorted(
|
|
|
|
qbt_client.torrents_info(), key=lambda d: d.ratio, reverse=True
|
|
|
|
)
|
|
|
|
print("Name Size # of Trackers Ratio Uploaded")
|
|
|
|
for torrent in sorted_torrents:
|
|
|
|
name = torrent.name
|
|
|
|
size = human_bytes(torrent.total_size)
|
|
|
|
trackers = torrent.trackers_count
|
|
|
|
ratio = torrent.ratio
|
|
|
|
uploaded = human_bytes(torrent.uploaded)
|
|
|
|
print(f"{name} {size} {trackers} {ratio} {uploaded}")
|
2024-10-25 18:56:21 +02:00
|
|
|
|
|
|
|
|
2024-10-20 19:34:45 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
args = docopt(__doc__) # type: ignore
|
2024-10-25 18:56:21 +02:00
|
|
|
print_ssv()
|