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