From ad4203fb8e8183366918e2d86f72db0a7b07c080 Mon Sep 17 00:00:00 2001 From: morpheus65535 <5130500+morpheus65535@users.noreply.github.com> Date: Tue, 10 Jul 2018 22:31:58 -0400 Subject: [PATCH] Better exception handling and logging when calling Sonarr and Radarr --- bazarr.py | 6 +- get_episodes.py | 35 ++++++++---- get_movies.py | 145 ++++++++++++++++++++++++++--------------------- get_series.py | 148 ++++++++++++++++++++++++++++++------------------ 4 files changed, 200 insertions(+), 134 deletions(-) diff --git a/bazarr.py b/bazarr.py index f85796d71..5eaa3db6c 100644 --- a/bazarr.py +++ b/bazarr.py @@ -130,7 +130,7 @@ def image_proxy(url): apikey = get_sonarr_settings()[2] url_image = url_sonarr_short + '/' + url + '?apikey=' + apikey try: - img_pil = Image.open(BytesIO(requests.get(url_sonarr_short + '/api' + url_image.split(url_sonarr)[1]).content)) + img_pil = Image.open(BytesIO(requests.get(url_sonarr_short + '/api' + url_image.split(url_sonarr)[1], timeout=15).content)) except: return None else: @@ -148,10 +148,10 @@ def image_proxy_movies(url): apikey = get_radarr_settings()[2] try: url_image = (url_radarr_short + '/' + url + '?apikey=' + apikey).replace('/fanart.jpg', '/banner.jpg') - img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1]).content)) + img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1], timeout=15).content)) except: url_image = url_radarr_short + '/' + url + '?apikey=' + apikey - img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1]).content)) + img_pil = Image.open(BytesIO(requests.get(url_radarr_short + '/api' + url_image.split(url_radarr)[1], timeout=15).content)) img_buffer = BytesIO() img_pil.tobytes() diff --git a/get_episodes.py b/get_episodes.py index 059d8d8a2..eb2cbcad3 100644 --- a/get_episodes.py +++ b/get_episodes.py @@ -36,18 +36,29 @@ def sync_episodes(): for seriesId in seriesIdList: # Get episodes data for a series from Sonarr url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr - r = requests.get(url_sonarr_api_episode) - for episode in r.json(): - if 'hasFile' in episode: - if episode['hasFile'] is True: - if 'episodeFile' in episode: - if episode['episodeFile']['size'] > 20480: - # Add shows in Sonarr to current shows list - if 'sceneName' in episode['episodeFile']: - sceneName = episode['episodeFile']['sceneName'] - else: - sceneName = None - current_episodes_sonarr.append((episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName)) + try: + r = requests.get(url_sonarr_api_episode, timeout=15) + r.raise_for_status() + except requests.exceptions.HTTPError as errh: + logging.exception("Error trying to get episodes from Sonarr. Http error.") + except requests.exceptions.ConnectionError as errc: + logging.exception("Error trying to get episodes from Sonarr. Connection Error.") + except requests.exceptions.Timeout as errt: + logging.exception("Error trying to get episodes from Sonarr. Timeout Error.") + except requests.exceptions.RequestException as err: + logging.exception("Error trying to get episodes from Sonarr.") + else: + for episode in r.json(): + if 'hasFile' in episode: + if episode['hasFile'] is True: + if 'episodeFile' in episode: + if episode['episodeFile']['size'] > 20480: + # Add shows in Sonarr to current shows list + if 'sceneName' in episode['episodeFile']: + sceneName = episode['episodeFile']['sceneName'] + else: + sceneName = None + current_episodes_sonarr.append((episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName)) added_episodes = list(set(current_episodes_sonarr) - set(current_episodes_db)) removed_episodes = list(set(current_episodes_db) - set(current_episodes_sonarr)) diff --git a/get_movies.py b/get_movies.py index a187cf156..419992f97 100644 --- a/get_movies.py +++ b/get_movies.py @@ -1,6 +1,7 @@ import os import sqlite3 import requests +import logging from get_general_settings import * from list_subtitles import * @@ -25,69 +26,79 @@ def update_movies(): # Get movies data from radarr url_radarr_api_movies = url_radarr + "/api/movie?apikey=" + apikey_radarr - r = requests.get(url_radarr_api_movies) - - # Get current movies in DB - current_movies_db = c.execute('SELECT tmdbId FROM table_movies').fetchall() - current_movies_db_list = [x[0] for x in current_movies_db] - current_movies_radarr = [] - - for movie in r.json(): - if movie['hasFile'] is True: - try: - overview = unicode(movie['overview']) - except: - overview = "" - try: - poster_big = movie['images'][0]['url'] - poster = os.path.splitext(poster_big)[0] + '-500' + os.path.splitext(poster_big)[1] - except: - poster = "" - try: - fanart = movie['images'][1]['url'] - except: - fanart = "" - - if 'movieFile' in movie: - if 'sceneName' in movie['movieFile']: - sceneName = movie['movieFile']['sceneName'] + try: + r = requests.get(url_radarr_api_movies, timeout=15) + r.raise_for_status() + except requests.exceptions.HTTPError as errh: + logging.exception("Error trying to get movies from Radarr. Http error.") + except requests.exceptions.ConnectionError as errc: + logging.exception("Error trying to get movies from Radarr. Connection Error.") + except requests.exceptions.Timeout as errt: + logging.exception("Error trying to get movies from Radarr. Timeout Error.") + except requests.exceptions.RequestException as err: + logging.exception("Error trying to get movies from Radarr.") + else: + # Get current movies in DB + current_movies_db = c.execute('SELECT tmdbId FROM table_movies').fetchall() + current_movies_db_list = [x[0] for x in current_movies_db] + current_movies_radarr = [] + + for movie in r.json(): + if movie['hasFile'] is True: + try: + overview = unicode(movie['overview']) + except: + overview = "" + try: + poster_big = movie['images'][0]['url'] + poster = os.path.splitext(poster_big)[0] + '-500' + os.path.splitext(poster_big)[1] + except: + poster = "" + try: + fanart = movie['images'][1]['url'] + except: + fanart = "" + + if 'movieFile' in movie: + if 'sceneName' in movie['movieFile']: + sceneName = movie['movieFile']['sceneName'] + else: + sceneName = None else: sceneName = None - else: - sceneName = None - - # Add movies in radarr to current movies list - current_movies_radarr.append(unicode(movie['tmdbId'])) - - # Detect file separator - if movie['path'][0] == "/": - separator = "/" - else: - separator = "\\" - - # Update or insert movies list in database table - try: - if movie_default_enabled == 'True': - c.execute('''INSERT INTO table_movies(title, path, tmdbId, languages,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName) VALUES (?,?,?,?,?, ?, ?, ?, ?, ?, ?)''', (movie["title"], movie["path"] + separator + movie['movieFile']['relativePath'], movie["tmdbId"], movie_default_language, movie_default_hi, movie["id"], overview, poster, fanart, profile_id_to_language(movie['qualityProfileId']), sceneName)) - else: - c.execute('''INSERT INTO table_movies(title, path, tmdbId, languages,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?),(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?, ?)''', (movie["title"], movie["path"] + separator + movie['movieFile']['relativePath'], movie["tmdbId"], movie["tmdbId"], movie["tmdbId"], movie["id"], overview, poster, fanart, profile_id_to_language(movie['qualityProfileId']), sceneName)) - except: - c.execute('''UPDATE table_movies SET title = ?, path = ?, tmdbId = ?, radarrId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ?, sceneName = ? WHERE tmdbid = ?''', (movie["title"],movie["path"] + separator + movie['movieFile']['relativePath'],movie["tmdbId"],movie["id"],overview,poster,fanart,profile_id_to_language(movie['qualityProfileId']),sceneName,movie["tmdbId"])) - - # Commit changes to database table - db.commit() - # Delete movies not in radarr anymore - added_movies = list(set(current_movies_radarr) - set(current_movies_db_list)) - removed_movies = list(set(current_movies_db_list) - set(current_movies_radarr)) + # Add movies in radarr to current movies list + current_movies_radarr.append(unicode(movie['tmdbId'])) - for removed_movie in removed_movies: - c.execute('DELETE FROM table_movies WHERE radarrId = ?', (removed_movie,)) - db.commit() + # Detect file separator + if movie['path'][0] == "/": + separator = "/" + else: + separator = "\\" + + # Update or insert movies list in database table + try: + if movie_default_enabled == 'True': + c.execute('''INSERT INTO table_movies(title, path, tmdbId, languages,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName) VALUES (?,?,?,?,?, ?, ?, ?, ?, ?, ?)''', (movie["title"], movie["path"] + separator + movie['movieFile']['relativePath'], movie["tmdbId"], movie_default_language, movie_default_hi, movie["id"], overview, poster, fanart, profile_id_to_language(movie['qualityProfileId']), sceneName)) + else: + c.execute('''INSERT INTO table_movies(title, path, tmdbId, languages,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?),(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?, ?)''', (movie["title"], movie["path"] + separator + movie['movieFile']['relativePath'], movie["tmdbId"], movie["tmdbId"], movie["tmdbId"], movie["id"], overview, poster, fanart, profile_id_to_language(movie['qualityProfileId']), sceneName)) + except: + c.execute('''UPDATE table_movies SET title = ?, path = ?, tmdbId = ?, radarrId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ?, sceneName = ? WHERE tmdbid = ?''', (movie["title"],movie["path"] + separator + movie['movieFile']['relativePath'],movie["tmdbId"],movie["id"],overview,poster,fanart,profile_id_to_language(movie['qualityProfileId']),sceneName,movie["tmdbId"])) + + # Commit changes to database table + db.commit() + + # Delete movies not in radarr anymore + added_movies = list(set(current_movies_radarr) - set(current_movies_db_list)) + removed_movies = list(set(current_movies_db_list) - set(current_movies_radarr)) + + for removed_movie in removed_movies: + c.execute('DELETE FROM table_movies WHERE radarrId = ?', (removed_movie,)) + db.commit() - for added_movie in added_movies: - added_path = c.execute('SELECT path FROM table_movies WHERE tmdbId = ?', (added_movie,)).fetchone() - store_subtitles_movie(path_replace_movie(added_path[0])) + for added_movie in added_movies: + added_path = c.execute('SELECT path FROM table_movies WHERE tmdbId = ?', (added_movie,)).fetchone() + store_subtitles_movie(path_replace_movie(added_path[0])) # Close database connection db.close() @@ -101,14 +112,22 @@ def get_profile_list(): apikey_radarr = get_radarr_settings()[2] # Get profiles data from radarr - url_radarr_api_movies = url_radarr + "/api/profile?apikey=" + apikey_radarr - profiles_json = requests.get(url_radarr_api_movies) global profiles_list profiles_list = [] - # Parsing data returned from radarr - for profile in profiles_json.json(): - profiles_list.append([profile['id'], profile['language'].capitalize()]) + url_radarr_api_movies = url_radarr + "/api/profile?apikey=" + apikey_radarr + try: + profiles_json = requests.get(url_radarr_api_movies, timeout=15) + except requests.exceptions.ConnectionError as errc: + logging.exception("Error trying to get profiles from Sonarr. Connection Error.") + except requests.exceptions.Timeout as errt: + logging.exception("Error trying to get profiles from Sonarr. Timeout Error.") + except requests.exceptions.RequestException as err: + logging.exception("Error trying to get profiles from Sonarr.") + else: + # Parsing data returned from radarr + for profile in profiles_json.json(): + profiles_list.append([profile['id'], profile['language'].capitalize()]) def profile_id_to_language(id): for profile in profiles_list: diff --git a/get_series.py b/get_series.py index 8d7cd1805..b57798be9 100644 --- a/get_series.py +++ b/get_series.py @@ -1,6 +1,7 @@ import os import sqlite3 import requests +import logging from get_general_settings import * from list_subtitles import * @@ -25,50 +26,60 @@ def update_series(): # Get shows data from Sonarr url_sonarr_api_series = url_sonarr + "/api/series?apikey=" + apikey_sonarr - r = requests.get(url_sonarr_api_series) - - # Get current shows in DB - current_shows_db = c.execute('SELECT tvdbId FROM table_shows').fetchall() - current_shows_db_list = [x[0] for x in current_shows_db] - current_shows_sonarr = [] - - for show in r.json(): - try: - overview = unicode(show['overview']) - except: - overview = "" - try: - poster_big = show['images'][2]['url'].split('?')[0] - poster = os.path.splitext(poster_big)[0] + '-250' + os.path.splitext(poster_big)[1] - except: - poster = "" - try: - fanart = show['images'][0]['url'].split('?')[0] - except: - fanart = "" - - # Add shows in Sonarr to current shows list - current_shows_sonarr.append(show['tvdbId']) - - # Update or insert shows list in database table - try: - if serie_default_enabled == 'True': - c.execute('''INSERT INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (show["title"], show["path"], show["tvdbId"], serie_default_language, serie_default_hi, show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) - list_missing_subtitles(show["id"]) - else: - c.execute('''INSERT INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?,?,?,(SELECT languages FROM table_shows WHERE tvdbId = ?),(SELECT `hearing_impaired` FROM table_shows WHERE tvdbId = ?), ?, ?, ?, ?, ?, ?)''', (show["title"], show["path"], show["tvdbId"], show["tvdbId"], show["tvdbId"], show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) - except: - c.execute('''UPDATE table_shows SET title = ?, path = ?, tvdbId = ?, sonarrSeriesId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ? , sortTitle = ? WHERE tvdbid = ?''', (show["title"],show["path"],show["tvdbId"],show["id"],overview,poster,fanart,profile_id_to_language((show['qualityProfileId'] if sonarr_version == 2 else show['languageProfileId'])),show['sortTitle'],show["tvdbId"])) - - # Delete shows not in Sonarr anymore - deleted_items = [] - for item in current_shows_db_list: - if item not in current_shows_sonarr: - deleted_items.append(tuple([item])) - c.executemany('DELETE FROM table_shows WHERE tvdbId = ?',deleted_items) - - # Commit changes to database table - db.commit() + try: + r = requests.get(url_sonarr_api_series, timeout=15) + r.raise_for_status() + except requests.exceptions.HTTPError as errh: + logging.exception("Error trying to get series from Sonarr. Http error.") + except requests.exceptions.ConnectionError as errc: + logging.exception("Error trying to get series from Sonarr. Connection Error.") + except requests.exceptions.Timeout as errt: + logging.exception("Error trying to get series from Sonarr. Timeout Error.") + except requests.exceptions.RequestException as err: + logging.exception("Error trying to get series from Sonarr.") + else: + # Get current shows in DB + current_shows_db = c.execute('SELECT tvdbId FROM table_shows').fetchall() + current_shows_db_list = [x[0] for x in current_shows_db] + current_shows_sonarr = [] + + for show in r.json(): + try: + overview = unicode(show['overview']) + except: + overview = "" + try: + poster_big = show['images'][2]['url'].split('?')[0] + poster = os.path.splitext(poster_big)[0] + '-250' + os.path.splitext(poster_big)[1] + except: + poster = "" + try: + fanart = show['images'][0]['url'].split('?')[0] + except: + fanart = "" + + # Add shows in Sonarr to current shows list + current_shows_sonarr.append(show['tvdbId']) + + # Update or insert shows list in database table + try: + if serie_default_enabled == 'True': + c.execute('''INSERT INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (show["title"], show["path"], show["tvdbId"], serie_default_language, serie_default_hi, show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) + list_missing_subtitles(show["id"]) + else: + c.execute('''INSERT INTO table_shows(title, path, tvdbId, languages,`hearing_impaired`, sonarrSeriesId, overview, poster, fanart, `audio_language`, sortTitle) VALUES (?,?,?,(SELECT languages FROM table_shows WHERE tvdbId = ?),(SELECT `hearing_impaired` FROM table_shows WHERE tvdbId = ?), ?, ?, ?, ?, ?, ?)''', (show["title"], show["path"], show["tvdbId"], show["tvdbId"], show["tvdbId"], show["id"], overview, poster, fanart, profile_id_to_language(show['qualityProfileId']), show['sortTitle'])) + except: + c.execute('''UPDATE table_shows SET title = ?, path = ?, tvdbId = ?, sonarrSeriesId = ?, overview = ?, poster = ?, fanart = ?, `audio_language` = ? , sortTitle = ? WHERE tvdbid = ?''', (show["title"],show["path"],show["tvdbId"],show["id"],overview,poster,fanart,profile_id_to_language((show['qualityProfileId'] if sonarr_version == 2 else show['languageProfileId'])),show['sortTitle'],show["tvdbId"])) + + # Delete shows not in Sonarr anymore + deleted_items = [] + for item in current_shows_db_list: + if item not in current_shows_sonarr: + deleted_items.append(tuple([item])) + c.executemany('DELETE FROM table_shows WHERE tvdbId = ?',deleted_items) + + # Commit changes to database table + db.commit() # Close database connection db.close() @@ -80,23 +91,48 @@ def get_profile_list(): apikey_sonarr = get_sonarr_settings()[2] # Get profiles data from Sonarr + error = False + url_sonarr_api_series = url_sonarr + "/api/profile?apikey=" + apikey_sonarr - profiles_json = requests.get(url_sonarr_api_series) + try: + profiles_json = requests.get(url_sonarr_api_series, timeout=15) + except requests.exceptions.ConnectionError as errc: + error = True + logging.exception("Error trying to get profiles from Sonarr. Connection Error.") + except requests.exceptions.Timeout as errt: + error = True + logging.exception("Error trying to get profiles from Sonarr. Timeout Error.") + except requests.exceptions.RequestException as err: + error = True + logging.exception("Error trying to get profiles from Sonarr.") + url_sonarr_api_series_v3 = url_sonarr + "/api/v3/languageprofile?apikey=" + apikey_sonarr - profiles_json_v3 = requests.get(url_sonarr_api_series_v3) + try: + profiles_json_v3 = requests.get(url_sonarr_api_series_v3, timeout=15) + except requests.exceptions.ConnectionError as errc: + error = True + logging.exception("Error trying to get profiles from Sonarr. Connection Error.") + except requests.exceptions.Timeout as errt: + error = True + logging.exception("Error trying to get profiles from Sonarr. Timeout Error.") + except requests.exceptions.RequestException as err: + error = True + logging.exception("Error trying to get profiles from Sonarr.") + global profiles_list profiles_list = [] - # Parsing data returned from Sonarr - global sonarr_version - if type(profiles_json_v3.json()) != list: - sonarr_version = 2 - for profile in profiles_json.json(): - profiles_list.append([profile['id'], profile['language'].capitalize()]) - else: - sonarr_version = 3 - for profile in profiles_json_v3.json(): - profiles_list.append([profile['id'], profile['name'].capitalize()]) + if error is False: + # Parsing data returned from Sonarr + global sonarr_version + if type(profiles_json_v3.json()) != list: + sonarr_version = 2 + for profile in profiles_json.json(): + profiles_list.append([profile['id'], profile['language'].capitalize()]) + else: + sonarr_version = 3 + for profile in profiles_json_v3.json(): + profiles_list.append([profile['id'], profile['name'].capitalize()]) def profile_id_to_language(id): for profile in profiles_list: