admin-scripts/python/qbt_sum_size.py

79 lines
2.1 KiB
Python
Raw Permalink Normal View History

2024-11-09 00:18:47 +01:00
# /// script
# dependencies = [
# "qbittorrent-api",
# "docopt",
# ]
# ///
2024-07-07 05:14:58 +02:00
2024-07-29 08:48:44 +02:00
"""qbt_sum_size.py
Description:
Get the total size of all added torrents and the total size of all completed
torrents from a qBittorrent instance.
2024-07-15 01:51:57 +02:00
Usage:
2024-07-29 08:48:44 +02:00
qbt_sum_size.py (HOSTNAME) (USERNAME) (PASSWORD)
qbt_sum_size.py -h
2024-07-15 01:51:57 +02:00
Examples:
2024-07-29 08:48:44 +02:00
qbt_sum_size.py "http://localhost:8080" "admin" "adminadmin"
qbt_sum_size.py "https://cat.seedhost.eu/lol/qbittorrent" "lol" "supersecretpassword"
2024-07-15 01:51:57 +02:00
Options:
-h, --help show this help message and exit
"""
2024-11-09 00:18:47 +01:00
import qbittorrentapi
2024-07-15 01:51:57 +02:00
from docopt import docopt
2024-07-07 05:14:58 +02:00
# convert byte units
def human_bytes(bites: int) -> str:
B = float(bites)
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 ""
if __name__ == "__main__":
2024-08-05 00:18:24 +02:00
args = docopt(__doc__) # type: ignore
2024-07-07 05:14:58 +02:00
2024-07-28 04:01:18 +02:00
completed_torrent_sizes = []
2024-11-09 00:18:47 +01:00
total_added_bytes = int()
2024-07-07 05:14:58 +02:00
2024-11-09 00:18:47 +01:00
with qbittorrentapi.Client(
host=args["HOSTNAME"], username=args["USERNAME"], password=args["PASSWORD"]
) as qbt_client:
try:
qbt_client.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(e)
for torrent in qbt_client.torrents_info():
if torrent.completion_on != 0:
completed_torrent_sizes.append(torrent.total_size)
2024-07-07 05:14:58 +02:00
2024-11-09 00:18:47 +01:00
total_added_bytes = sum(
[torrent.total_size for torrent in qbt_client.torrents_info()]
)
total_completed_bytes = sum(completed_torrent_sizes)
2024-07-07 05:14:58 +02:00
print(f"\nTotal completed size: {human_bytes(total_completed_bytes)}")
print(f"Total added size: {human_bytes(total_added_bytes)}\n")