From 837d71712e7c10103153eafee0c86418249e850f Mon Sep 17 00:00:00 2001 From: tyler breese Date: Mon, 6 Nov 2023 23:09:37 -0500 Subject: [PATCH] random fixes --- plexpy/__init__.py | 7 ++- plexpy/datafactory.py | 10 ++-- plexpy/pmsconnect.py | 2 +- plexpy/web_socket.py | 103 ++++++++++++++++++++---------------------- plexpy/webserve.py | 2 +- 5 files changed, 61 insertions(+), 63 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 1a1564ac..30d1b260 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -490,8 +490,8 @@ def initialize_scheduler(): schedule_job(activity_pinger.connect_server, 'Check for server response', hours=0, minutes=0, seconds=0) - # schedule_job(web_socket.send_ping, 'Websocket ping', - # hours=0, minutes=0, seconds=10 * bool(CONFIG.WEBSOCKET_MONITOR_PING_PONG)) + schedule_job(web_socket.send_ping, 'Websocket ping', + hours=0, minutes=0, seconds=10 * bool(CONFIG.WEBSOCKET_MONITOR_PING_PONG)) else: # Cancel all jobs @@ -2762,8 +2762,7 @@ def shutdown(restart=False, update=False, checkout=False, reset=False): webstart.stop() # Shutdown the websocket connection - if WEBSOCKET: - web_socket.shutdown() + web_socket.shutdown() if SCHED.running: SCHED.shutdown(wait=False) diff --git a/plexpy/datafactory.py b/plexpy/datafactory.py index 3136ea32..1fe33c4f 100644 --- a/plexpy/datafactory.py +++ b/plexpy/datafactory.py @@ -1143,7 +1143,9 @@ class DataFactory(object): if (parts[0] not in libraries): libraries[parts[0]] = [] libraries[parts[0]].append(parts[1]) - + if len(libraries) < 1: + logger.warn("Tautulli DataFactory :: Unable to execute database query for get_library_stats") + return None try: query = "SELECT ls.id, ls.section_id, ls.section_name, ls.section_type, ls.thumb AS library_thumb, " \ "ls.custom_thumb_url AS custom_thumb, ls.art AS library_art, ls.custom_art_url AS custom_art, " \ @@ -1153,17 +1155,17 @@ class DataFactory(object): "sh.rating_key, shm.grandparent_rating_key, shm.thumb, shm.grandparent_thumb, " \ "sh.user, sh.user_id, sh.player, " \ "shm.art, sh.media_type, shm.content_rating, shm.labels, shm.live, shm.guid, " \ - "MAX(sh.started) AS last_watch, ls.server_id as server_id " \ + "ls.server_id as server_id " \ "FROM library_sections AS ls " \ "LEFT OUTER JOIN session_history AS sh ON ls.section_id = sh.section_id AND ls.server_id = sh.server_id " \ - "LEFT OUTER JOIN session_history_metadata AS shm ON sh.id = shm.id AND sh.server_id = shm.server_id " + "LEFT OUTER JOIN session_history_metadata AS shm ON sh.id = shm.id AND sh.server_id = shm.server_id WHERE " query_parts = [] for library in libraries: query_parts.append("(ls.section_id IN (" + ",".join(libraries[library]) + ") AND ls.deleted_section = 0 AND ls.server_id = '" + library +"' )") query +=(" or ").join(query_parts) - query += "ORDER BY ls.section_type, ls.count DESC, ls.parent_count DESC, ls.child_count DESC " + query += " GROUP BY ls.section_id, ls.server_id ORDER BY ls.section_type, ls.count DESC, ls.parent_count DESC, ls.child_count DESC " result = monitor_db.select(query) except Exception as e: logger.warn("Tautulli DataFactory :: Unable to execute database query for get_library_stats: %s." % e) diff --git a/plexpy/pmsconnect.py b/plexpy/pmsconnect.py index 7d778026..54cf0cca 100644 --- a/plexpy/pmsconnect.py +++ b/plexpy/pmsconnect.py @@ -2373,7 +2373,7 @@ class PmsConnect(object): if session_id: logger.info("Tautulli Pmsconnect :: Terminating session %s (session_id %s)." % (session_key, session_id)) - response = self.get_sessions_terminate(session_id=session_id, reason=message, server_id=self.server_id) + response = self.get_sessions_terminate(session_id=session_id, reason=message) return response.ok else: msg = 'Missing session_id' diff --git a/plexpy/web_socket.py b/plexpy/web_socket.py index 1575219b..7b549eaf 100644 --- a/plexpy/web_socket.py +++ b/plexpy/web_socket.py @@ -55,13 +55,20 @@ pong_timer = None pong_count = 0 ws_list = [] - def isServerUp(): for ws in ws_list: if ws.WS_CONNECTED: return True return False +def shutdown(): + for ws in ws_list: + ws.shutdown() + +def send_ping(): + for ws in ws_list: + ws.send_ping() + def start_threads(): try: # Check for any existing sessions on start up @@ -80,15 +87,13 @@ def start_threads(): for owned_server in owned_servers: for server in plex_servers: if owned_server.server_id == server['pms_identifier']: - for connection in server['connections']: - if connection['local']: - wss=WebSocketServer(connection, owned_server.server_id) - ws_list.append(wss) - thread = threading.Thread(target=wss.run) - thread.daemon = True - thread.start() - break - break + wss=WebSocketServer(server['connections'], owned_server.server_id) + ws_list.append(wss) + thread = threading.Thread(target=wss.run) + thread.daemon = True + thread.start() + + continue class WebSocketServer(object): @@ -193,53 +198,45 @@ class WebSocketServer(object): def run(self): + # try local first + if self.server: + for connection in self.server: + if plexpy.CONFIG.PMS_SSL: + uri = connection['uri'].replace('https://', 'wss://') + '/:/websockets/notifications' + secure = 'secure ' + if plexpy.CONFIG.VERIFY_SSL_CERT: + sslopt = {'ca_certs': certifi.where()} + else: + sslopt = {'cert_reqs': ssl.CERT_NONE} + else: + uri = 'ws://%s:%s/:/websockets/notifications' % ( + connection['address'], + connection['port'] + ) + secure = '' + sslopt = None - if plexpy.CONFIG.PMS_SSL: - uri = "" - if self.server: - uri = self.server['uri'].replace('https://', 'wss://') + '/:/websockets/notifications' - else: - uri = plexpy.CONFIG.PMS_URL.replace('https://', 'wss://') + '/:/websockets/notifications' - secure = 'secure ' - if plexpy.CONFIG.VERIFY_SSL_CERT: - sslopt = {'ca_certs': certifi.where()} - else: - sslopt = {'cert_reqs': ssl.CERT_NONE} - else: - uri = "" - if self.server: - uri = 'ws://%s:%s/:/websockets/notifications' % ( - self.server['address'], - self.server['port'] - ) - else: - uri = 'ws://%s:%s/:/websockets/notifications' % ( - plexpy.CONFIG.PMS_IP, - plexpy.CONFIG.PMS_PORT - ) - secure = '' - sslopt = None + # Set authentication token (if one is available) + if plexpy.CONFIG.PMS_TOKEN: + header = {"X-Plex-Token": plexpy.CONFIG.PMS_TOKEN} + else: + header = None - # Set authentication token (if one is available) - if plexpy.CONFIG.PMS_TOKEN: - header = {"X-Plex-Token": plexpy.CONFIG.PMS_TOKEN} - else: - header = None + timeout = plexpy.CONFIG.PMS_TIMEOUT - timeout = plexpy.CONFIG.PMS_TIMEOUT + global ws_shutdown + ws_shutdown = False + reconnects = 0 - global ws_shutdown - ws_shutdown = False - reconnects = 0 - - # Try an open the websocket connection - logger.info("Tautulli WebSocket :: Opening %swebsocket." % secure) - try: - self.WEBSOCKET = create_connection(uri, timeout=timeout, header=header, sslopt=sslopt) - logger.info("Tautulli WebSocket :: Ready") - self.WS_CONNECTED = True - except (websocket.WebSocketException, IOError, Exception) as e: - logger.error("Tautulli WebSocket :: %s.", e) + # Try an open the websocket connection + logger.info("Tautulli WebSocket :: Opening %swebsocket." % secure) + try: + self.WEBSOCKET = create_connection(uri, timeout=timeout, header=header, sslopt=sslopt) + logger.info("Tautulli WebSocket :: Ready") + self.WS_CONNECTED = True + break + except (websocket.WebSocketException, IOError, Exception) as e: + logger.error("Tautulli WebSocket :: %s.", e) if self.WS_CONNECTED: self.on_connect() diff --git a/plexpy/webserve.py b/plexpy/webserve.py index 52e86344..b980fae6 100644 --- a/plexpy/webserve.py +++ b/plexpy/webserve.py @@ -3375,7 +3375,7 @@ class WebInterface(object): # Get new server URLs for SSL communications and get new server friendly name if server_changed: plextv.get_server_resources() - if plexpy.WS_CONNECTED: + if web_socket.isServerUp(): web_socket.reconnect() # If first run, start websocket