Assorted shell and Python scripts
1#!/usr/bin/env -S uv run --script
2# /// script
3# dependencies = [
4# "qbittorrent-api",
5# "docopt",
6# ]
7# ///
8
9"""qbt_sum_size.py
10
11Description:
12Get the total size of all added torrents and the total size of all completed
13torrents from a qBittorrent instance.
14
15Usage:
16 qbt_sum_size.py (HOSTNAME) (USERNAME) (PASSWORD)
17 qbt_sum_size.py -h
18
19Examples:
20 qbt_sum_size.py "http://localhost:8080" "admin" "adminadmin"
21 qbt_sum_size.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "supersecretpassword"
22
23Options:
24 -h, --help show this help message and exit
25"""
26
27import qbittorrentapi
28from docopt import docopt
29
30
31# convert byte units
32def human_bytes(bites: int) -> str:
33 B = float(bites)
34 KiB = float(1024)
35 MiB = float(KiB**2)
36 GiB = float(KiB**3)
37 TiB = float(KiB**4)
38
39 match B:
40 case B if B < KiB:
41 return "{0} {1}".format(B, "bytes" if 0 == B > 1 else "byte")
42 case B if KiB <= B < MiB:
43 return "{0:.2f} KiB".format(B / KiB)
44 case B if MiB <= B < GiB:
45 return "{0:.2f} MiB".format(B / MiB)
46 case B if GiB <= B < TiB:
47 return "{0:.2f} GiB".format(B / GiB)
48 case B if TiB <= B:
49 return "{0:.2f} TiB".format(B / TiB)
50 case _:
51 return ""
52
53
54if __name__ == "__main__":
55 args = docopt(__doc__) # type: ignore
56
57 completed_torrent_sizes = []
58 total_added_bytes = int()
59
60 with qbittorrentapi.Client(
61 host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
62 ) as qbt_client:
63 try:
64 qbt_client.auth_log_in()
65 except qbittorrentapi.LoginFailed as e:
66 print(e)
67
68 for torrent in qbt_client.torrents_info():
69 if torrent.completion_on != 0:
70 completed_torrent_sizes.append(torrent.total_size)
71
72 total_added_bytes = sum(
73 [torrent.total_size for torrent in qbt_client.torrents_info()]
74 )
75
76 total_completed_bytes = sum(completed_torrent_sizes)
77
78 print(f"\nTotal completed size: {human_bytes(total_completed_bytes)}")
79 print(f"Total added size: {human_bytes(total_added_bytes)}\n")