jellyfin-monitor: cleanup + remove qbt auth

This commit is contained in:
Simon Gardling 2025-09-12 01:30:10 -04:00
parent 4729bd2cc4
commit f60c31578f
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -36,58 +36,12 @@ class JellyfinQBittorrentMonitor:
self.streaming_stop_delay = 60 self.streaming_stop_delay = 60
self.last_state_change = 0 self.last_state_change = 0
# Try to authenticate with qBittorrent
self.authenticate_qbittorrent()
def signal_handler(self, signum, frame): def signal_handler(self, signum, frame):
logger.info("Received shutdown signal, cleaning up...") logger.info("Received shutdown signal, cleaning up...")
self.running = False self.running = False
self.restore_normal_limits() self.restore_normal_limits()
sys.exit(0) sys.exit(0)
def authenticate_qbittorrent(self):
"""Try to authenticate with qBittorrent using empty credentials (for whitelist)"""
logger.info("Attempting to authenticate with qBittorrent...")
try:
test_response = self.session.get(
f"{self.qbittorrent_url}/api/v2/app/version", timeout=5
)
if test_response.status_code == 200:
logger.info("qBittorrent accessible without explicit login - subnet whitelist working")
return True
except Exception as e:
logger.info(f"Version endpoint failed: {e}")
try:
# Try login with empty credentials (should work with subnet whitelist)
login_data = {"username": "", "password": ""}
headers = {
"Referer": self.qbittorrent_url,
"Content-Type": "application/x-www-form-urlencoded",
}
response = self.session.post(
f"{self.qbittorrent_url}/login",
data=login_data,
headers=headers,
timeout=10,
)
if response.status_code == 200 and ("Ok." in response.text or response.text.strip() == "Ok."):
logger.info("Successfully authenticated with qBittorrent")
return True
elif "Fails." in response.text:
logger.warning("qBittorrent login failed - authentication may be required")
else:
logger.warning(f"Login failed: HTTP {response.status_code}")
except Exception as e:
logger.error(f"Could not authenticate with qBittorrent: {e}")
return False
def check_jellyfin_sessions(self): def check_jellyfin_sessions(self):
"""Check if anyone is actively streaming from Jellyfin""" """Check if anyone is actively streaming from Jellyfin"""
try: try:
@ -130,7 +84,9 @@ class JellyfinQBittorrentMonitor:
if response.status_code == 200: if response.status_code == 200:
return response.text.strip() == "1" return response.text.strip() == "1"
else: else:
logger.warning(f"SpeedLimitsMode endpoint returned HTTP {response.status_code}") logger.warning(
f"SpeedLimitsMode endpoint returned HTTP {response.status_code}"
)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
logger.error(f"SpeedLimitsMode endpoint failed: {e}") logger.error(f"SpeedLimitsMode endpoint failed: {e}")
@ -150,17 +106,21 @@ class JellyfinQBittorrentMonitor:
except Exception as e: except Exception as e:
logger.error(f"Transfer info fallback failed: {e}") logger.error(f"Transfer info fallback failed: {e}")
logger.warning("Could not determine qBittorrent alternate limits status, using tracked state") logger.warning(
"Could not determine qBittorrent alternate limits status, using tracked state"
)
return self.throttle_active return self.throttle_active
def toggle_qbittorrent_limits(self, enable_throttle): def toggle_qbittorrent_limits(self, enable_throttle):
"""Toggle qBittorrent alternate speed limits""" """Toggle qBittorrent alternate speed limits"""
try: try:
current_throttle = self.check_qbittorrent_alternate_limits() current_throttle = self.check_qbittorrent_alternate_limits()
if current_throttle == enable_throttle: if current_throttle == enable_throttle:
action = "enabled" if enable_throttle else "disabled" action = "enabled" if enable_throttle else "disabled"
logger.info(f"Alternate speed limits already {action}, no action needed") logger.info(
f"Alternate speed limits already {action}, no action needed"
)
return return
response = self.session.post( response = self.session.post(
@ -168,17 +128,19 @@ class JellyfinQBittorrentMonitor:
timeout=10, timeout=10,
) )
response.raise_for_status() response.raise_for_status()
self.throttle_active = enable_throttle self.throttle_active = enable_throttle
# Verify the change took effect # Verify the change took effect
new_state = self.check_qbittorrent_alternate_limits() new_state = self.check_qbittorrent_alternate_limits()
if new_state == enable_throttle: if new_state == enable_throttle:
action = "enabled" if enable_throttle else "disabled" action = "enabled" if enable_throttle else "disabled"
logger.info(f"✓ Successfully {action} alternate speed limits") logger.info(f"✓ Successfully {action} alternate speed limits")
else: else:
logger.warning(f"Toggle may have failed: expected {enable_throttle}, got {new_state}") logger.warning(
f"Toggle may have failed: expected {enable_throttle}, got {new_state}"
)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
action = "enable" if enable_throttle else "disable" action = "enable" if enable_throttle else "disable"
logger.error(f"Failed to {action} alternate speed limits: {e}") logger.error(f"Failed to {action} alternate speed limits: {e}")
@ -207,7 +169,9 @@ class JellyfinQBittorrentMonitor:
return True return True
else: else:
remaining = self.streaming_start_delay - time_since_change remaining = self.streaming_start_delay - time_since_change
logger.info(f"Streaming started - waiting {remaining:.1f}s before enabling throttling") logger.info(
f"Streaming started - waiting {remaining:.1f}s before enabling throttling"
)
# Stop throttling (streaming stopped) # Stop throttling (streaming stopped)
elif not new_streaming_state and self.last_streaming_state: elif not new_streaming_state and self.last_streaming_state:
@ -216,7 +180,9 @@ class JellyfinQBittorrentMonitor:
return True return True
else: else:
remaining = self.streaming_stop_delay - time_since_change remaining = self.streaming_stop_delay - time_since_change
logger.info(f"Streaming stopped - waiting {remaining:.1f}s before disabling throttling") logger.info(
f"Streaming stopped - waiting {remaining:.1f}s before disabling throttling"
)
return False return False