diff --git a/list_torrents.py b/list_torrents.py index 63fcf2b..96870e5 100755 --- a/list_torrents.py +++ b/list_torrents.py @@ -15,10 +15,18 @@ Options: -h, --help show this help message and exit """ +import qbittorrentapi from docopt import docopt -from qbittorrent import Client from tabulate import tabulate +try: + with qbittorrentapi.Client( + host="localhost", port=8080, username="", password="" + ) as qbt_client: + qbt_client.auth_log_in() +except qbittorrentapi.LoginFailed as e: + print(e) + # convert byte units def human_bytes(input_bytes: int) -> str: @@ -44,35 +52,40 @@ def human_bytes(input_bytes: int) -> str: 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) + with qbittorrentapi.Client( + host="localhost", port=8080, username="", password="" + ) as qbt_client: + sorted_torrents = sorted( + qbt_client.torrents_info(), key=lambda d: d.ratio, reverse=True + ) + for torrent in sorted_torrents: + row = [] + row.append(torrent.name) + row.append(human_bytes(torrent.total_size)) + row.append(torrent.trackers_count) + row.append(torrent.ratio) + row.append(human_bytes(torrent.uploaded)) + table.append(row) - print(tabulate(table, headers, tablefmt="grid")) + 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}") + with qbittorrentapi.Client( + host="localhost", port=8080, username="", password="" + ) as qbt_client: + sorted_torrents = sorted( + qbt_client.torrents_info(), key=lambda d: d.ratio, reverse=True + ) + for torrent in sorted_torrents: + name = torrent.name + size = human_bytes(torrent.total_size) + trackers = torrent.trackers_count + ratio = torrent.ratio + uploaded = human_bytes(torrent.uploaded) + print(f"{name} {size} {trackers} {ratio} {uploaded}") if __name__ == "__main__":