Update plex_client.py
This commit is contained in:
parent
e06f0ea372
commit
a73d89bc9e
1 changed files with 14 additions and 2 deletions
|
|
@ -80,6 +80,8 @@ class PlexClient:
|
||||||
self.music_library: Optional[MusicSection] = None
|
self.music_library: Optional[MusicSection] = None
|
||||||
self._connection_attempted = False
|
self._connection_attempted = False
|
||||||
self._is_connecting = False
|
self._is_connecting = False
|
||||||
|
self._last_connection_check = 0 # Cache connection checks
|
||||||
|
self._connection_check_interval = 30 # Check every 30 seconds max
|
||||||
|
|
||||||
def ensure_connection(self) -> bool:
|
def ensure_connection(self) -> bool:
|
||||||
"""Ensure connection to Plex server with lazy initialization."""
|
"""Ensure connection to Plex server with lazy initialization."""
|
||||||
|
|
@ -159,11 +161,21 @@ class PlexClient:
|
||||||
logger.error(f"Error finding music library: {e}")
|
logger.error(f"Error finding music library: {e}")
|
||||||
|
|
||||||
def is_connected(self) -> bool:
|
def is_connected(self) -> bool:
|
||||||
"""Check if connected to Plex server with lazy initialization."""
|
"""Check if connected to Plex server with cached connection checks."""
|
||||||
if not self._connection_attempted:
|
import time
|
||||||
|
|
||||||
|
current_time = time.time()
|
||||||
|
|
||||||
|
# Only check connection if enough time has passed or never attempted
|
||||||
|
if (not self._connection_attempted or
|
||||||
|
current_time - self._last_connection_check > self._connection_check_interval):
|
||||||
|
|
||||||
|
self._last_connection_check = current_time
|
||||||
|
|
||||||
# Try to connect on first call, but don't block if already connecting
|
# Try to connect on first call, but don't block if already connecting
|
||||||
if not self._is_connecting:
|
if not self._is_connecting:
|
||||||
self.ensure_connection()
|
self.ensure_connection()
|
||||||
|
|
||||||
return self.server is not None and self.music_library is not None
|
return self.server is not None and self.music_library is not None
|
||||||
|
|
||||||
def get_all_playlists(self) -> List[PlexPlaylistInfo]:
|
def get_all_playlists(self) -> List[PlexPlaylistInfo]:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue