Files
platform/gateway/integrations/qbittorrent.py
Yusuf Suleman 7cd81181ed Refactor gateway into modular architecture
Split 1878-line server.py into 15 focused modules:
- config.py: all env vars and constants
- database.py: schema, init, seed logic
- sessions.py: session/token CRUD
- proxy.py: proxy_request, SERVICE_MAP, resolve_service
- responses.py: ResponseMixin for handler helpers
- auth.py: login/logout/register handlers
- dashboard.py: dashboard, apps, connections, pinning
- command.py: AI command bar
- integrations/booklore.py: auth, books, cover, import
- integrations/kindle.py: send-to-kindle, file finder
- integrations/karakeep.py: save/delete bookmarks
- integrations/qbittorrent.py: download status
- integrations/image_proxy.py: external image proxy

server.py is now thin routing only (~344 lines).
All routes, methods, status codes, and responses preserved exactly.
Added PYTHONUNBUFFERED=1 to Dockerfile for live logging.
2026-03-29 00:14:46 -05:00

49 lines
1.9 KiB
Python

"""
Platform Gateway — qBittorrent integration (download status).
"""
import json
import os
import urllib.request
import urllib.parse
def handle_downloads_status(handler):
"""Get active downloads from qBittorrent."""
qbt_host = os.environ.get("QBITTORRENT_HOST", "192.168.1.42")
qbt_port = os.environ.get("QBITTORRENT_PORT", "8080")
qbt_user = os.environ.get("QBITTORRENT_USERNAME", "admin")
qbt_pass = os.environ.get("QBITTORRENT_PASSWORD", "")
base = f"http://{qbt_host}:{qbt_port}"
try:
# Login
login_data = urllib.parse.urlencode({"username": qbt_user, "password": qbt_pass}).encode()
req = urllib.request.Request(f"{base}/api/v2/auth/login", data=login_data)
with urllib.request.urlopen(req, timeout=5) as resp:
cookie = resp.headers.get("Set-Cookie", "").split(";")[0]
# Get torrents
req2 = urllib.request.Request(f"{base}/api/v2/torrents/info?filter=all&sort=added_on&reverse=true&limit=20",
headers={"Cookie": cookie})
with urllib.request.urlopen(req2, timeout=5) as resp2:
torrents_raw = json.loads(resp2.read())
torrents = []
for t in torrents_raw:
torrents.append({
"hash": t["hash"],
"name": t["name"],
"progress": round(t["progress"] * 100, 1),
"state": t["state"],
"size": t["total_size"],
"downloaded": t["downloaded"],
"dlSpeed": t["dlspeed"],
"upSpeed": t["upspeed"],
"eta": t.get("eta", 0),
"addedOn": t.get("added_on", 0),
"category": t.get("category", ""),
})
handler._send_json({"torrents": torrents, "total": len(torrents)})
except Exception as e:
handler._send_json({"torrents": [], "total": 0, "error": str(e)})