From 06f290e99af49cf5c96844807d45285697e20029 Mon Sep 17 00:00:00 2001 From: Lucas Vivo Date: Mon, 16 Nov 2026 14:44:36 +0100 Subject: [PATCH] style(torrent_scripts): ruff changes --- torrent/torrent_configure_indexers.py | 50 ++++++++++++--------------- torrent/torrent_qbittorrent_api.py | 5 +-- 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/torrent/torrent_configure_indexers.py b/torrent/torrent_configure_indexers.py index 1b5eea0..1e0190a 100755 --- a/torrent/torrent_configure_indexers.py +++ b/torrent/torrent_configure_indexers.py @@ -3,7 +3,6 @@ import argparse import logging import re -import time import requests from requests.adapters import HTTPAdapter @@ -87,7 +86,6 @@ adapter = HTTPAdapter(max_retries=retry_strategy) session.mount("http://", adapter) session.mount("https://", adapter) - def get_api_version(base_url, api_key): r = requests.get( f"{base_url}/api", @@ -99,7 +97,6 @@ def get_api_version(base_url, api_key): def api_get(base_url, api_key, endpoint, retries=3): - last_error = None for attempt in range(1, retries + 1): try: @@ -113,22 +110,17 @@ def api_get(base_url, api_key, endpoint, retries=3): return r.json() except requests.HTTPError as e: - logger.info("Status:", e.response.status_code) - logger.info("Response:", e.response.text) + logger.info(f"Status: {e.response.status_code}") + logger.info(f"Response: {e.response.text}") raise except Exception as e: - last_error = e logger.info(f"GET failed ({attempt}/{retries}): {e}") + raise - if attempt < retries: - time.sleep(2**attempt) - - raise last_error - + # if attempt < retries: + # time.sleep(2**attempt) def api_put(base_url, api_key, endpoint, payload, retries=3): - last_error = None - for attempt in range(1, retries + 1): try: logger.trace(f"Put to endpoint: {endpoint}") @@ -144,31 +136,31 @@ def api_put(base_url, api_key, endpoint, payload, retries=3): r.raise_for_status() return r.json() if r.content else None except requests.HTTPError as e: - logger.info("Status:", e.response.status_code) - logger.info("Response:", e.response.text) + logger.info(f"Status: {e.response.status_code}") + logger.info(f"Response: {e.response.text}") raise except Exception as e: - last_error = e logger.info(f"PUT failed ({attempt}/{retries}): {e}") + raise - if attempt < retries: - time.sleep(2**attempt) - - raise last_error +# if attempt < retries: +# time.sleep(2**attempt) -def get_download_client_ids(base_url, endpoint, api_key): +def get_download_client_ids(base_url, endpoint, api_key) -> tuple[int,int]: clients = api_get(base_url, api_key, endpoint) - private_id = None - public_id = None - + private_id = -1 + public_id = -1 + if not clients: + return private_id, public_id + for client in clients: if client["name"] == PRIVATE_CLIENT: - private_id = client["id"] + private_id = int(client["id"]) if client["name"] == PUBLIC_CLIENT: - public_id = client["id"] + public_id = int(client["id"]) return private_id, public_id @@ -202,6 +194,8 @@ def process_instance(instance): logger.info(f"{PUBLIC_CLIENT} id: {public_id}") indexers = api_get(url, api_key, f"/api/{api_version}/indexer") + if not indexers: + return for indexer in indexers: idx_id = indexer["id"] idx_name = indexer["name"] @@ -238,7 +232,8 @@ def process_instance(instance): logger.info(f"SET {idx_name} -> {target_name}") except Exception as e: - logger.info(f"FAIL {idx_name}: {e}") + logger.error(f"FAIL {idx_name}: {e}") + raise def main(): @@ -253,6 +248,5 @@ def main(): for instance in INSTANCES: process_instance(instance) - if __name__ == "__main__": main() diff --git a/torrent/torrent_qbittorrent_api.py b/torrent/torrent_qbittorrent_api.py index 4c7ec7f..0845191 100755 --- a/torrent/torrent_qbittorrent_api.py +++ b/torrent/torrent_qbittorrent_api.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse + import requests API_KEY_PRIVATE = "qbt_Fw6R39WuJVY7rdhv2IXIc5n8Yfhc" @@ -72,13 +73,11 @@ def force_start_by_tracker(instance, trackers): print(f"{name} -> force_start={force_start} (matched {tracker})") break - def format_duration(seconds): hours = seconds / 3600 days = seconds // 86400 return f"{days}d ({hours:.1f}h)" - def show_torrents(instance, sort_by="name", reverse=True): sort_keys = { "name": lambda t: t["name"].lower(), @@ -109,14 +108,12 @@ def show_torrents(instance, sort_by="name", reverse=True): for name, ratio, seed_time in rows: print(f"{name:<{name_w}} Ratio: {ratio:>{ratio_w}} Seed Time: {seed_time}") - class Qbittorrent: def __init__(self, ip_adress, name, api_key) -> None: self.ip_adress = ip_adress self.name = name self.api_key = api_key - if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument(