mirror of
https://codeberg.org/hyperreal/admin-scripts
synced 2024-11-25 09:03:41 +01:00
81 lines
2.6 KiB
Python
Executable File
81 lines
2.6 KiB
Python
Executable File
#!/home/jas/virtualenvs/list_torrents/bin/python3
|
|
|
|
"""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
|
|
"""
|
|
|
|
from docopt import docopt
|
|
from qbittorrent import Client
|
|
from tabulate import tabulate
|
|
|
|
|
|
# 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 ""
|
|
|
|
|
|
def print_table():
|
|
qb = Client("http://localhost:8080/")
|
|
qb.login()
|
|
table = []
|
|
headers = ["Name", "Total Size", "Trackers Count", "Ratio", "Uploaded"]
|
|
sorted_torrents = sorted(qb.torrents(), key=lambda d: d["ratio"], reverse=True) # type: ignore
|
|
for torrent in sorted_torrents:
|
|
row = []
|
|
row.append(torrent["name"]) # type: ignore
|
|
row.append(human_bytes(int(torrent["total_size"]))) # type: ignore
|
|
row.append(torrent["trackers_count"]) # type: ignore
|
|
row.append(torrent["ratio"]) # type: ignore
|
|
row.append(human_bytes(int(torrent["uploaded"]))) # type: ignore
|
|
table.append(row)
|
|
|
|
print(tabulate(table, headers, tablefmt="grid"))
|
|
|
|
|
|
def print_ssv():
|
|
qb = Client("http://localhost:8080/")
|
|
qb.login()
|
|
sorted_torrents = sorted(qb.torrents(), key=lambda d: d["ratio"], reverse=True) # type: ignore
|
|
print("Name Size Trackers Ratio Uploaded")
|
|
for torrent in sorted_torrents:
|
|
name = torrent["name"] # type: ignore
|
|
size = human_bytes(int(torrent["total_size"])) # type: ignore
|
|
trackers = torrent["trackers_count"] # type: ignore
|
|
ratio = torrent["ratio"] # type: ignore
|
|
uploaded = human_bytes(int(torrent["uploaded"])) # type: ignore
|
|
print(f"{name} {size} {trackers} {ratio} {uploaded}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = docopt(__doc__) # type: ignore
|
|
print_ssv()
|