From 97bdf0066e76b31c188699d7a94caeaa9b795199 Mon Sep 17 00:00:00 2001 From: morpheus65535 Date: Thu, 23 Nov 2023 17:33:08 -0500 Subject: [PATCH] Fixed an issue that could clear episodes history in case Sonarr API return something else than valid series or episodes. --- bazarr/radarr/sync/utils.py | 8 +++++++- bazarr/sonarr/sync/episodes.py | 2 ++ bazarr/sonarr/sync/series.py | 5 ++--- bazarr/sonarr/sync/utils.py | 32 +++++++++++++++++++++++++------- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bazarr/radarr/sync/utils.py b/bazarr/radarr/sync/utils.py index 834f74104..e91f34f44 100644 --- a/bazarr/radarr/sync/utils.py +++ b/bazarr/radarr/sync/utils.py @@ -85,5 +85,11 @@ def get_movies_from_radarr_api(apikey_radarr, radarr_id=None): except requests.exceptions.RequestException: logging.exception("BAZARR Error trying to get movies from Radarr.") return + except Exception as e: + logging.exception(f"Exception raised while getting movies from Radarr API: {e}") + return else: - return r.json() + if r.status_code == 200: + return r.json() + else: + return diff --git a/bazarr/sonarr/sync/episodes.py b/bazarr/sonarr/sync/episodes.py index 2b9f62981..7894d9061 100644 --- a/bazarr/sonarr/sync/episodes.py +++ b/bazarr/sonarr/sync/episodes.py @@ -78,6 +78,8 @@ def sync_episodes(series_id, send_event=True): episodes_to_update.append(parsed_episode) else: episodes_to_add.append(episodeParser(episode)) + else: + return # Remove old episodes from DB episodes_to_delete = list(set(current_episodes_id_db_list) - set(current_episodes_sonarr)) diff --git a/bazarr/sonarr/sync/series.py b/bazarr/sonarr/sync/series.py index 09ee8e43b..41eb4ee35 100644 --- a/bazarr/sonarr/sync/series.py +++ b/bazarr/sonarr/sync/series.py @@ -43,7 +43,7 @@ def update_series(send_event=True): tagsDict = get_tags() # Get shows data from Sonarr - series = get_series_from_sonarr_api(url=url_sonarr(), apikey_sonarr=apikey_sonarr) + series = get_series_from_sonarr_api(apikey_sonarr=apikey_sonarr) if not isinstance(series, list): return else: @@ -157,8 +157,7 @@ def update_one_series(series_id, action): # Get series data from sonarr api series = None - series_data = get_series_from_sonarr_api(url=url_sonarr(), apikey_sonarr=settings.sonarr.apikey, - sonarr_series_id=int(series_id)) + series_data = get_series_from_sonarr_api(apikey_sonarr=settings.sonarr.apikey, sonarr_series_id=int(series_id)) if not series_data: return diff --git a/bazarr/sonarr/sync/utils.py b/bazarr/sonarr/sync/utils.py index d90bcd1f7..6ace16e33 100644 --- a/bazarr/sonarr/sync/utils.py +++ b/bazarr/sonarr/sync/utils.py @@ -67,7 +67,7 @@ def get_tags(): return tagsDict.json() -def get_series_from_sonarr_api(url, apikey_sonarr, sonarr_series_id=None): +def get_series_from_sonarr_api(apikey_sonarr, sonarr_series_id=None): url_sonarr_api_series = (f"{url_api_sonarr()}series/{sonarr_series_id if sonarr_series_id else ''}?" f"apikey={apikey_sonarr}") try: @@ -87,12 +87,18 @@ def get_series_from_sonarr_api(url, apikey_sonarr, sonarr_series_id=None): except requests.exceptions.RequestException: logging.exception("BAZARR Error trying to get series from Sonarr.") return + except Exception as e: + logging.exception(f"Exception raised while getting series from Sonarr API: {e}") + return else: - result = r.json() - if isinstance(result, dict): - return [result] + if r.status_code == 200: + result = r.json() + if isinstance(result, dict): + return [result] + else: + return r.json() else: - return r.json() + return def get_episodes_from_sonarr_api(apikey_sonarr, series_id=None, episode_id=None): @@ -118,8 +124,14 @@ def get_episodes_from_sonarr_api(apikey_sonarr, series_id=None, episode_id=None) except requests.exceptions.RequestException: logging.exception("BAZARR Error trying to get episodes from Sonarr.") return + except Exception as e: + logging.exception(f"Exception raised while getting episodes from Sonarr API: {e}") + return else: - return r.json() + if r.status_code == 200: + return r.json() + else: + return def get_episodesFiles_from_sonarr_api(apikey_sonarr, series_id=None, episode_file_id=None): @@ -146,5 +158,11 @@ def get_episodesFiles_from_sonarr_api(apikey_sonarr, series_id=None, episode_fil except requests.exceptions.RequestException: logging.exception("BAZARR Error trying to get episodeFiles from Sonarr.") return + except Exception as e: + logging.exception(f"Exception raised while getting episodes from Sonarr API: {e}") + return else: - return r.json() + if r.status_code == 200: + return r.json() + else: + return