import requests import time HOST = "http://127.0.0.1:8085" USERNAME = "admin" PASSWORD = "123456" INTERVAL = 10 # seconds session = requests.Session() def try_login(): try: login_resp = session.post(f"{HOST}/api/v2/auth/login", data={ "username": USERNAME, "password": PASSWORD }, timeout=5) if login_resp.text != "Ok.": print("Login failed, response was:", login_resp.text) return False print("Logged in to qBittorrent WebUI. Monitoring torrents...") return True except requests.exceptions.RequestException as e: print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Could not connect to host: {e}") return False # Try login until successful while not try_login(): time.sleep(INTERVAL) # Main loop while True: try: # Fetch all torrents torrents_resp = session.get(f"{HOST}/api/v2/torrents/info", timeout=5) torrents = torrents_resp.json() # Find torrents needing sequential download hashes_to_seq = [t["hash"] for t in torrents if not t["seq_dl"]] if hashes_to_seq: # Enable sequential download session.post(f"{HOST}/api/v2/torrents/toggleSequentialDownload", data={ "hashes": "|".join(hashes_to_seq) }) # Enable first/last piece priority session.post(f"{HOST}/api/v2/torrents/toggleFirstLastPiecePrio", data={ "hashes": "|".join(hashes_to_seq) }) count = len(hashes_to_seq) word = "torrent" if count == 1 else "torrents" print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Updated {count} {word}: sequential + first/last piece enabled.") else: total = len(torrents) word = "torrent" if total == 1 else "torrents" print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] All torrents already configured.") except requests.exceptions.RequestException as e: print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Connection error: {e}") except Exception as e: print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Unexpected error: {e}") time.sleep(INTERVAL)