From dc1fe1159073b906c8fc64a9e3fe153e9c0a9da1 Mon Sep 17 00:00:00 2001 From: Luigi311 Date: Wed, 8 Mar 2023 21:49:56 -0700 Subject: [PATCH] Check for response status 200 on jellyfin query --- src/jellyfin.py | 22 ++++++++++++---------- src/main.py | 37 +++++++++++++++++++++++++++---------- src/plex.py | 6 +++++- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/jellyfin.py b/src/jellyfin.py index 4034f7f..2b88a6a 100644 --- a/src/jellyfin.py +++ b/src/jellyfin.py @@ -38,17 +38,24 @@ class Jellyfin: async with session.get( self.baseurl + query, headers=headers ) as response: + if response.status != 200: + raise Exception( + f"Query failed with status {response.status} {response.reason}" + ) results = await response.json() elif query_type == "post": async with session.post( self.baseurl + query, headers=headers ) as response: + if response.status != 200: + raise Exception( + f"Query failed with status {response.status} {response.reason}" + ) results = await response.json() - if type(results) is str: - logger(f"Jellyfin: Query {query_type} {query} {results}", 2) - raise Exception(results) + if not isinstance(results, list) and not isinstance(results, dict): + raise Exception("Query result is not of type list or dict") # append identifiers to results if identifiers: @@ -57,7 +64,7 @@ class Jellyfin: return results except Exception as e: - logger(f"Jellyfin: Query failed {e}", 2) + logger(f"Jellyfin: Query {query_type} {query}\nResults {results}\n{e}", 2) raise Exception(e) async def get_users(self): @@ -393,12 +400,7 @@ class Jellyfin: # If there are multiple types in library raise error if types is None or len(types) < 1: - all_types = set( - [ - x["Type"] - for x in watched["Items"] - ] - ) + all_types = set([x["Type"] for x in watched["Items"]]) logger( f"Jellyfin: Skipping Library {library_title} found types: {types}, all types: {all_types}", 1, diff --git a/src/main.py b/src/main.py index 0b45ab9..8f281b2 100644 --- a/src/main.py +++ b/src/main.py @@ -18,7 +18,6 @@ load_dotenv(override=True) def setup_users( server_1, server_2, blacklist_users, whitelist_users, user_mapping=None ): - # generate list of users from server 1 and server 2 server_1_type = server_1[0] server_1_connection = server_1[1] @@ -266,27 +265,45 @@ def update_server_watched( def should_sync_server(server_1_type, server_2_type): sync_from_plex_to_jellyfin = str_to_bool( - os.getenv("SYNC_FROM_PLEX_TO_JELLYFIN", "True")) + os.getenv("SYNC_FROM_PLEX_TO_JELLYFIN", "True") + ) sync_from_jelly_to_plex = str_to_bool( - os.getenv("SYNC_FROM_JELLYFIN_TO_PLEX", "True")) - sync_from_plex_to_plex = str_to_bool( - os.getenv("SYNC_FROM_PLEX_TO_PLEX", "True")) + os.getenv("SYNC_FROM_JELLYFIN_TO_PLEX", "True") + ) + sync_from_plex_to_plex = str_to_bool(os.getenv("SYNC_FROM_PLEX_TO_PLEX", "True")) sync_from_jelly_to_jellyfin = str_to_bool( - os.getenv("SYNC_FROM_JELLYFIN_TO_JELLYFIN", "True")) + os.getenv("SYNC_FROM_JELLYFIN_TO_JELLYFIN", "True") + ) - if server_1_type == "plex" and server_2_type == "plex" and not sync_from_plex_to_plex: + if ( + server_1_type == "plex" + and server_2_type == "plex" + and not sync_from_plex_to_plex + ): logger("Sync between plex and plex is disabled", 1) return False - if server_1_type == "plex" and server_2_type == "jellyfin" and not sync_from_jelly_to_plex: + if ( + server_1_type == "plex" + and server_2_type == "jellyfin" + and not sync_from_jelly_to_plex + ): logger("Sync from jellyfin to plex disabled", 1) return False - if server_1_type == "jellyfin" and server_2_type == "jellyfin" and not sync_from_jelly_to_jellyfin: + if ( + server_1_type == "jellyfin" + and server_2_type == "jellyfin" + and not sync_from_jelly_to_jellyfin + ): logger("Sync between jellyfin and jellyfin is disabled", 1) return False - if server_1_type == "jellyfin" and server_2_type == "plex" and not sync_from_plex_to_jellyfin: + if ( + server_1_type == "jellyfin" + and server_2_type == "plex" + and not sync_from_plex_to_jellyfin + ): logger("Sync from plex to jellyfin is disabled", 1) return False diff --git a/src/plex.py b/src/plex.py index 29e8571..a688c3c 100644 --- a/src/plex.py +++ b/src/plex.py @@ -12,6 +12,7 @@ from src.functions import ( future_thread_executor, ) + # Bypass hostname validation for ssl. Taken from https://github.com/pkkid/python-plexapi/issues/143#issuecomment-775485186 class HostNameIgnoringAdapter(requests.adapters.HTTPAdapter): def init_poolmanager(self, connections, maxsize, block=..., **pool_kwargs): @@ -235,7 +236,10 @@ def update_user_watched(user, user_plex, library, videos, dryrun): ).group(1) # If episode provider source and episode provider id are in videos_episodes_ids exactly, then the episode is in the list - if episode_guid_source in videos_episodes_ids.keys(): + if ( + episode_guid_source + in videos_episodes_ids.keys() + ): if ( episode_guid_id in videos_episodes_ids[episode_guid_source]