From 8aabd1466ec2f81cb902f2e8a70a383a17a704af Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 24 Oct 2025 13:04:31 -0400 Subject: [PATCH] jellyfin-qbittorrent-monitor: cleanup --- services/jellyfin-qbittorrent-monitor.nix | 2 +- services/jellyfin-qbittorrent-monitor.py | 14 +++----------- tests/jellyfin-qbittorrent-monitor.nix | 16 ++++++++-------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/services/jellyfin-qbittorrent-monitor.nix b/services/jellyfin-qbittorrent-monitor.nix index a660b11..430cc15 100644 --- a/services/jellyfin-qbittorrent-monitor.nix +++ b/services/jellyfin-qbittorrent-monitor.nix @@ -45,4 +45,4 @@ CHECK_INTERVAL = "30"; }; }; -} \ No newline at end of file +} diff --git a/services/jellyfin-qbittorrent-monitor.py b/services/jellyfin-qbittorrent-monitor.py index 6d19c61..b749e27 100644 --- a/services/jellyfin-qbittorrent-monitor.py +++ b/services/jellyfin-qbittorrent-monitor.py @@ -67,9 +67,7 @@ class JellyfinQBittorrentMonitor: def check_jellyfin_sessions(self) -> list[str]: """Check if anyone is actively streaming from Jellyfin (external networks only)""" try: - headers = {} - if self.jellyfin_api_key: - headers["X-Emby-Token"] = self.jellyfin_api_key + headers = {"X-Emby-Token": self.jellyfin_api_key} if self.jellyfin_api_key else {} response = requests.get( f"{self.jellyfin_url}/Sessions", headers=headers, timeout=10 @@ -83,20 +81,14 @@ class JellyfinQBittorrentMonitor: if ( "NowPlayingItem" in session and not session.get("PlayState", {}).get("IsPaused", True) + and not self.is_local_ip(session.get("RemoteEndPoint", "")) ): - # Check if session is from external network - remote_endpoint = session.get("RemoteEndPoint", "") - if remote_endpoint and self.is_local_ip(remote_endpoint): - logger.debug(f"Skipping local session from {remote_endpoint}") - continue - item = session["NowPlayingItem"] # Only count video streams (Movies, Episodes, etc.) item_type = item.get("Type", "").lower() if item_type in ["movie", "episode", "video"]: user = session.get("UserName", "Unknown") - client_info = f" (from {remote_endpoint})" if remote_endpoint else "" - active_streams.append(f"{user}: {item.get('Name', 'Unknown')}{client_info}") + active_streams.append(f"{user}: {item.get('Name', 'Unknown')}") return active_streams diff --git a/tests/jellyfin-qbittorrent-monitor.nix b/tests/jellyfin-qbittorrent-monitor.nix index c2c09d1..aba0634 100644 --- a/tests/jellyfin-qbittorrent-monitor.nix +++ b/tests/jellyfin-qbittorrent-monitor.nix @@ -243,9 +243,9 @@ pkgs.testers.runNixOSTest { systemd-run --unit=jellyfin-qbittorrent-monitor-test \\ --setenv=JELLYFIN_URL=http://localhost:8096 \\ --setenv=QBITTORRENT_URL=http://localhost:8080 \\ - --setenv=CHECK_INTERVAL=2 \\ - --setenv=STREAMING_START_DELAY=2 \\ - --setenv=STREAMING_STOP_DELAY=3 \\ + --setenv=CHECK_INTERVAL=1 \\ + --setenv=STREAMING_START_DELAY=1 \\ + --setenv=STREAMING_STOP_DELAY=1 \\ {python_path} {monitor_path} """) @@ -265,7 +265,7 @@ pkgs.testers.runNixOSTest { # Set streaming state set_jellyfin_state(streaming=True, paused=is_paused, local=is_local, media_type=media_type) - time.sleep(6) # Wait for monitor to detect and apply changes + time.sleep(1.5) # Wait for monitor to detect and apply changes throttling_active = get_throttling_state() @@ -286,23 +286,23 @@ pkgs.testers.runNixOSTest { assert not throttling_active, f"Expected no throttling for {media_type}, {'paused' if is_paused else 'playing'}, {'local' if is_local else 'external'}" set_jellyfin_state(streaming=False) - time.sleep(7) # Wait for stop delay + time.sleep(1.5) # Wait for stop delay assert not get_throttling_state(), "No streaming should disable throttling" # Start with throttling-enabled state set_jellyfin_state(streaming=True, paused=False, local=False, media_type="Movie") - time.sleep(6) + time.sleep(1.5) assert get_throttling_state(), "Should enable throttling for external Movie" # Switch to paused (should disable throttling) set_jellyfin_state(streaming=True, paused=True, local=False, media_type="Movie") - time.sleep(6) + time.sleep(1.5) assert not get_throttling_state(), "Should disable throttling when paused" # Switch back to playing (should re-enable throttling) set_jellyfin_state(streaming=True, paused=False, local=False, media_type="Movie") - time.sleep(6) + time.sleep(1.5) assert get_throttling_state(), "Should re-enable throttling when unpaused" ''; }