admin-scripts/bin/qbt_sum_size
2024-07-27 21:01:18 -05:00

65 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
"""qbt-sum-size
Usage:
qbt-sum-size (HOSTNAME) (USERNAME) (PASSWORD)
qbt-sum-size -h
Examples:
qbt-sum-size "http://localhost:8080" "admin" "adminadmin"
qbt-sum-size "https://cat.seedhost.eu/lol/qbittorrent" "lol" "supersecretpassword"
Options:
-h, --help show this help message and exit
"""
from docopt import docopt
from qbittorrent import Client
# 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__":
args = docopt(__doc__)
# Initialize client and login
qb = Client(args["HOSTNAME"])
qb.login(username=args["USERNAME"], password=args["PASSWORD"])
# get total_completed_bytes
completed_torrent_sizes = []
for torrent in qb.torrents():
if torrent["state"] == "queuedUP" or torrent["state"] == "pausedUP": # type: ignore
completed_torrent_sizes.append(torrent["total_size"]) # type: ignore
total_completed_bytes = sum(completed_torrent_sizes)
# get total_added_bytes
total_added_bytes = sum([torrent["total_size"] for torrent in torrents]) # type: ignore
# print the results
print(f"\nTotal completed size: {human_bytes(total_completed_bytes)}")
print(f"Total added size: {human_bytes(total_added_bytes)}\n")