From 863c848074d76c22ed9f1f29f3d465fba00e9513 Mon Sep 17 00:00:00 2001 From: Steven Kreitzer Date: Wed, 9 Oct 2024 12:29:16 -0500 Subject: [PATCH] feat(limiter): automagically determine host (#85) --- qbtools/commands/limiter.py | 31 +++++++++++++++++++++++++------ qbtools/commands/reannounce.py | 2 +- qbtools/qbtools.py | 1 + requirements.txt | 1 + 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/qbtools/commands/limiter.py b/qbtools/commands/limiter.py index 2f84c5a..b1cd838 100755 --- a/qbtools/commands/limiter.py +++ b/qbtools/commands/limiter.py @@ -1,14 +1,17 @@ import time -import requests +import httpx +from httpx import URL from qbtools import utils -from decimal import Decimal from typing import Optional, Tuple def __init__(app, logger): logger.info("Starting limiter process...") + app.sabnzbd_host = parse_sabnzbd_host(app) + logger.info(app.sabnzbd_host) + def process(): qbittorrent_queue, qbittorrent_current_limit = qbittorrent_data(app) sabnzbd_queue, sabnzbd_current_limit = sabnzbd_data(app) @@ -29,9 +32,7 @@ def process(): limit = int(app.max_line_speed_mbps * percentage) if qbittorrent_current_limit != limit: - app.client.transfer_set_download_limit( - limit * 1024 * 1024 - ) + app.client.transfer_set_download_limit(limit * 1024 * 1024) logger.info( f"qbittorrent download limit set to {limit} MB/s " f"(was {qbittorrent_current_limit} MB/s)..." @@ -62,6 +63,17 @@ def process(): time.sleep(app.interval) +def parse_sabnzbd_host(app) -> str: + url = app.sabnzbd_host + if not URL(url).host: + url = ( + f"http://{app.sabnzbd_host}:{app.sabnzbd_port}" + if app.sabnzbd_port + else f"http://{app.sabnzbd_host}" + ) + return url + + def qbittorrent_data(app) -> Tuple[int, int]: torrents = len(app.client.torrents.info(status_filter="downloading")) download_limit = app.client.transfer_download_limit() / 1024 / 1024 @@ -81,7 +93,7 @@ def sabnzbd_data(app) -> Tuple[int, int]: def handle_request( url: str, method: str = "GET", data: Optional[dict] = None ) -> Optional[dict]: - response = requests.post(url, data=data) if method == "POST" else requests.get(url) + response = httpx.request(method=method, url=url, data=data) response.raise_for_status() return response.json() if method == "GET" else None @@ -101,6 +113,13 @@ def add_arguments(command, subparser): help="The host of the SabNZBD server", required=True, ) + parser.add_argument( + "--sabnzbd-port", + action=utils.EnvDefault, + envvar="SABNZBD_PORT", + help="The port of the SabNZBD server", + required=False, + ) parser.add_argument( "--sabnzbd-apikey", action=utils.EnvDefault, diff --git a/qbtools/commands/reannounce.py b/qbtools/commands/reannounce.py index 57a61b9..991199a 100755 --- a/qbtools/commands/reannounce.py +++ b/qbtools/commands/reannounce.py @@ -35,7 +35,7 @@ def process_torrents(status): continue t.reannounce() - torrents_retries[t.hash] = torrent_retries + 1 + torrents_retries[t.hash] = torrent_retries = torrent_retries + 1 logger.info( f"Reannounced torrent {t.name} ({t.hash}) {torrent_retries}/{app.max_retries}", ) diff --git a/qbtools/qbtools.py b/qbtools/qbtools.py index 97af997..a06eb2b 100755 --- a/qbtools/qbtools.py +++ b/qbtools/qbtools.py @@ -110,6 +110,7 @@ def get_config(app): def main(): logging.getLogger("filelock").setLevel(logging.ERROR) # Suppress lock messages + logging.getLogger("httpx").setLevel(logging.ERROR) # Suppress httpx messages logging.basicConfig( stream=sys.stdout, level=logging.INFO, diff --git a/requirements.txt b/requirements.txt index 62648a5..b1ca946 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ qbittorrent-api==2024.9.67 tldextract==5.1.2 pyyaml==6.0.2 +httpx==0.27.2