Skip to content

Commit

Permalink
feat(limiter): automagically determine host
Browse files Browse the repository at this point in the history
  • Loading branch information
buroa committed Oct 9, 2024
1 parent 5548e2d commit f97f35d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
31 changes: 25 additions & 6 deletions qbtools/commands/limiter.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)..."
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion qbtools/commands/reannounce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
)
Expand Down
1 change: 1 addition & 0 deletions qbtools/qbtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
qbittorrent-api==2024.9.67
tldextract==5.1.2
pyyaml==6.0.2
httpx==0.27.2

0 comments on commit f97f35d

Please sign in to comment.