From af5873f37a8a63ae957a93d9c5e7f0696e853945 Mon Sep 17 00:00:00 2001 From: Jeffrey Serio Date: Tue, 5 Nov 2024 17:59:58 -0600 Subject: [PATCH] Use qbittorrentapi --- qbt_sum_size.py | 31 ++++++++++++++++++------------- scihub_knapsack.py | 20 +++++++++++++++----- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/qbt_sum_size.py b/qbt_sum_size.py index 2bb9ca1..39f1edf 100755 --- a/qbt_sum_size.py +++ b/qbt_sum_size.py @@ -18,8 +18,8 @@ Options: -h, --help show this help message and exit """ +import qbittorrentapi from docopt import docopt -from qbittorrent import Client # convert byte units @@ -48,21 +48,26 @@ def human_bytes(bites: int) -> str: if __name__ == "__main__": args = docopt(__doc__) # type: ignore - # 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["completion_on"] != 0: # type: ignore - completed_torrent_sizes.append(torrent["total_size"]) # type: ignore + total_added_bytes = int() + + 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) + + total_added_bytes = sum( + [torrent.total_size for torrent in qbt_client.torrents_info()] + ) total_completed_bytes = sum(completed_torrent_sizes) - # get total_added_bytes - total_added_bytes = sum([torrent["total_size"] for torrent in qb.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") diff --git a/scihub_knapsack.py b/scihub_knapsack.py index 2f7c41c..1c6858c 100755 --- a/scihub_knapsack.py +++ b/scihub_knapsack.py @@ -39,9 +39,9 @@ Options: import json +import qbittorrentapi import requests from docopt import docopt -from qbittorrent import Client def get_torrent_health_data() -> list[dict]: @@ -62,6 +62,8 @@ def convert_size_to_bytes(size: str) -> int: Example: 42G --> 45097156608 bytes """ + total_bytes = int() + if size.endswith("T"): total_bytes = int(size.split("T")[0]) * (1024**4) @@ -157,8 +159,14 @@ if __name__ == "__main__": dry_run = args["--dry-run"] # Initialize client and login - qb = Client(hostname) - qb.login(username=username, password=password) + qbt_client = qbittorrentapi.Client( + host=hostname, username=username, password=password + ) + + try: + qbt_client.auth_log_in() + except qbittorrentapi.LoginFailed as e: + print(e) # Fill the knapsack knapsack = fill_knapsack(max_seeders, knapsack_size, smaller) @@ -183,13 +191,15 @@ if __name__ == "__main__": for torrent in knapsack: if "gen.lib.rus.ec" in torrent["link"]: new_torrent = torrent["link"].replace("gen.lib.rus.ec", "libgen.is") - qb.download_from_link(new_torrent, category="scihub") + qbt_client.torrents_add(new_torrent, category="scihub") if "libgen.rs" in torrent["link"]: new_torrent = torrent["link"].replace("libgen.rs", "libgen.is") - qb.download_from_link(new_torrent, category="scihub") + qbt_client.torrents_add(new_torrent, category="scihub") # print(f"Added {torrent['name']}") + qbt_client.auth_log_out() + print("----------------") print(f"Count: {len(knapsack)} torrents") print(f"Total combined size: {get_knapsack_weight(knapsack)}")