diff options
author | morpheus65535 <[email protected]> | 2022-02-05 10:09:29 -0500 |
---|---|---|
committer | morpheus65535 <[email protected]> | 2022-02-05 10:09:29 -0500 |
commit | 6988d7c7ad64b2bf6b52d7728f288a8f19bbb78c (patch) | |
tree | 76fe6e41ba782445faf7c2c38299b9dd9742be73 | |
parent | 54b8080a2576b2357c9679164532a5098cf014e7 (diff) | |
download | bazarr-6988d7c7ad64b2bf6b52d7728f288a8f19bbb78c.tar.gz bazarr-6988d7c7ad64b2bf6b52d7728f288a8f19bbb78c.zip |
Improved sync with Sonarr/Radarr exceptions management when trying to write to database. #1699
-rw-r--r-- | bazarr/get_episodes.py | 93 | ||||
-rw-r--r-- | bazarr/get_movies.py | 98 | ||||
-rw-r--r-- | bazarr/get_series.py | 83 |
3 files changed, 180 insertions, 94 deletions
diff --git a/bazarr/get_episodes.py b/bazarr/get_episodes.py index e79a6f7c2..a4e8720a5 100644 --- a/bazarr/get_episodes.py +++ b/bazarr/get_episodes.py @@ -3,7 +3,7 @@ import os import requests import logging -from peewee import DoesNotExist +from peewee import DoesNotExist, IntegrityError from database import TableEpisodes, TableShows from config import settings, url_sonarr @@ -88,13 +88,20 @@ def sync_episodes(series_id=None, send_event=True): removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr)) for removed_episode in removed_episodes: - episode_to_delete = TableEpisodes.select(TableEpisodes.sonarrSeriesId, TableEpisodes.sonarrEpisodeId)\ + episode_to_delete = TableEpisodes.select(TableEpisodes.path, + TableEpisodes.sonarrSeriesId, + TableEpisodes.sonarrEpisodeId)\ .where(TableEpisodes.sonarrEpisodeId == removed_episode)\ .dicts()\ .get() - TableEpisodes.delete().where(TableEpisodes.sonarrEpisodeId == removed_episode).execute() - if send_event: - event_stream(type='episode', action='delete', payload=episode_to_delete['sonarrEpisodeId']) + try: + TableEpisodes.delete().where(TableEpisodes.sonarrEpisodeId == removed_episode).execute() + except Exception as e: + logging.error(f"BAZARR cannot delete episode {episode_to_delete['path']} because of {e}") + continue + else: + if send_event: + event_stream(type='episode', action='delete', payload=episode_to_delete['sonarrEpisodeId']) # Update existing episodes in DB episode_in_db_list = [] @@ -120,24 +127,34 @@ def sync_episodes(series_id=None, send_event=True): episodes_to_update_list = [i for i in episodes_to_update if i not in episode_in_db_list] for updated_episode in episodes_to_update_list: - TableEpisodes.update(updated_episode).where(TableEpisodes.sonarrEpisodeId == - updated_episode['sonarrEpisodeId']).execute() - altered_episodes.append([updated_episode['sonarrEpisodeId'], - updated_episode['path'], - updated_episode['sonarrSeriesId']]) + try: + TableEpisodes.update(updated_episode).where(TableEpisodes.sonarrEpisodeId == + updated_episode['sonarrEpisodeId']).execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot update episode {updated_episode['path']} because of {e}") + continue + else: + altered_episodes.append([updated_episode['sonarrEpisodeId'], + updated_episode['path'], + updated_episode['sonarrSeriesId']]) # Insert new episodes in DB for added_episode in episodes_to_add: - result = TableEpisodes.insert(added_episode).on_conflict(action='IGNORE').execute() - if result > 0: - altered_episodes.append([added_episode['sonarrEpisodeId'], - added_episode['path'], - added_episode['monitored']]) - if send_event: - event_stream(type='episode', payload=added_episode['sonarrEpisodeId']) + try: + result = TableEpisodes.insert(added_episode).on_conflict(action='IGNORE').execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert episode {added_episode['path']} because of {e}") + continue else: - logging.debug('BAZARR unable to insert this episode into the database:{}'.format( - path_mappings.path_replace(added_episode['path']))) + if result > 0: + altered_episodes.append([added_episode['sonarrEpisodeId'], + added_episode['path'], + added_episode['monitored']]) + if send_event: + event_stream(type='episode', payload=added_episode['sonarrEpisodeId']) + else: + logging.debug('BAZARR unable to insert this episode into the database:{}'.format( + path_mappings.path_replace(added_episode['path']))) # Store subtitles for added or modified episodes for i, altered_episode in enumerate(altered_episodes, 1): @@ -185,25 +202,37 @@ def sync_one_episode(episode_id): # Remove episode from DB if not episode and existing_episode: - TableEpisodes.delete().where(TableEpisodes.sonarrEpisodeId == episode_id).execute() - event_stream(type='episode', action='delete', payload=int(episode_id)) - logging.debug('BAZARR deleted this episode from the database:{}'.format(path_mappings.path_replace( - existing_episode['path']))) - return + try: + TableEpisodes.delete().where(TableEpisodes.sonarrEpisodeId == episode_id).execute() + except Exception as e: + logging.error(f"BAZARR cannot delete episode {existing_episode['path']} because of {e}") + else: + event_stream(type='episode', action='delete', payload=int(episode_id)) + logging.debug('BAZARR deleted this episode from the database:{}'.format(path_mappings.path_replace( + existing_episode['path']))) + return # Update existing episodes in DB elif episode and existing_episode: - TableEpisodes.update(episode).where(TableEpisodes.sonarrEpisodeId == episode_id).execute() - event_stream(type='episode', action='update', payload=int(episode_id)) - logging.debug('BAZARR updated this episode into the database:{}'.format(path_mappings.path_replace( - episode['path']))) + try: + TableEpisodes.update(episode).where(TableEpisodes.sonarrEpisodeId == episode_id).execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot update episode {episode['path']} because of {e}") + else: + event_stream(type='episode', action='update', payload=int(episode_id)) + logging.debug('BAZARR updated this episode into the database:{}'.format(path_mappings.path_replace( + episode['path']))) # Insert new episodes in DB elif episode and not existing_episode: - TableEpisodes.insert(episode).on_conflict(action='IGNORE').execute() - event_stream(type='episode', action='update', payload=int(episode_id)) - logging.debug('BAZARR inserted this episode into the database:{}'.format(path_mappings.path_replace( - episode['path']))) + try: + TableEpisodes.insert(episode).on_conflict(action='IGNORE').execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert episode {episode['path']} because of {e}") + else: + event_stream(type='episode', action='update', payload=int(episode_id)) + logging.debug('BAZARR inserted this episode into the database:{}'.format(path_mappings.path_replace( + episode['path']))) # Storing existing subtitles logging.debug('BAZARR storing subtitles for this episode: {}'.format(path_mappings.path_replace( diff --git a/bazarr/get_movies.py b/bazarr/get_movies.py index 01e589aad..d369c1f3d 100644 --- a/bazarr/get_movies.py +++ b/bazarr/get_movies.py @@ -3,7 +3,7 @@ import os import requests import logging -from peewee import DoesNotExist +from peewee import DoesNotExist, IntegrityError from config import settings, url_radarr from helper import path_mappings @@ -93,7 +93,11 @@ def update_movies(send_event=True): removed_movies = list(set(current_movies_db_list) - set(current_movies_radarr)) for removed_movie in removed_movies: - TableMovies.delete().where(TableMovies.tmdbId == removed_movie).execute() + try: + TableMovies.delete().where(TableMovies.tmdbId == removed_movie).execute() + except Exception as e: + logging.error(f"BAZARR cannot remove movie tmdbId {removed_movie} because of {e}") + continue # Update movies in DB movies_in_db_list = [] @@ -125,25 +129,35 @@ def update_movies(send_event=True): movies_to_update_list = [i for i in movies_to_update if i not in movies_in_db_list] for updated_movie in movies_to_update_list: - TableMovies.update(updated_movie).where(TableMovies.tmdbId == updated_movie['tmdbId']).execute() - altered_movies.append([updated_movie['tmdbId'], - updated_movie['path'], - updated_movie['radarrId'], - updated_movie['monitored']]) + try: + TableMovies.update(updated_movie).where(TableMovies.tmdbId == updated_movie['tmdbId']).execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot update movie {updated_movie['path']} because of {e}") + continue + else: + altered_movies.append([updated_movie['tmdbId'], + updated_movie['path'], + updated_movie['radarrId'], + updated_movie['monitored']]) # Insert new movies in DB for added_movie in movies_to_add: - result = TableMovies.insert(added_movie).on_conflict(action='IGNORE').execute() - if result > 0: - altered_movies.append([added_movie['tmdbId'], - added_movie['path'], - added_movie['radarrId'], - added_movie['monitored']]) - if send_event: - event_stream(type='movie', action='update', payload=int(added_movie['radarrId'])) + try: + result = TableMovies.insert(added_movie).on_conflict(action='IGNORE').execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert movie {added_movie['path']} because of {e}") + continue else: - logging.debug('BAZARR unable to insert this movie into the database:', - path_mappings.path_replace_movie(added_movie['path'])) + if result > 0: + altered_movies.append([added_movie['tmdbId'], + added_movie['path'], + added_movie['radarrId'], + added_movie['monitored']]) + if send_event: + event_stream(type='movie', action='update', payload=int(added_movie['radarrId'])) + else: + logging.debug('BAZARR unable to insert this movie into the database:', + path_mappings.path_replace_movie(added_movie['path'])) # Store subtitles for added or modified movies for i, altered_movie in enumerate(altered_movies, 1): @@ -167,10 +181,14 @@ def update_one_movie(movie_id, action): # Remove movie from DB if action == 'deleted': if existing_movie: - TableMovies.delete().where(TableMovies.radarrId == movie_id).execute() - event_stream(type='movie', action='delete', payload=int(movie_id)) - logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie( - existing_movie['path']))) + try: + TableMovies.delete().where(TableMovies.radarrId == movie_id).execute() + except Exception as e: + logging.error(f"BAZARR cannot delete movie {existing_movie['path']} because of {e}") + else: + event_stream(type='movie', action='delete', payload=int(movie_id)) + logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie( + existing_movie['path']))) return movie_default_enabled = settings.general.getboolean('movie_default_enabled') @@ -209,25 +227,37 @@ def update_one_movie(movie_id, action): # Remove movie from DB if not movie and existing_movie: - TableMovies.delete().where(TableMovies.radarrId == movie_id).execute() - event_stream(type='movie', action='delete', payload=int(movie_id)) - logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie( - existing_movie['path']))) - return + try: + TableMovies.delete().where(TableMovies.radarrId == movie_id).execute() + except Exception as e: + logging.error(f"BAZARR cannot insert episode {existing_movie['path']} because of {e}") + else: + event_stream(type='movie', action='delete', payload=int(movie_id)) + logging.debug('BAZARR deleted this movie from the database:{}'.format(path_mappings.path_replace_movie( + existing_movie['path']))) + return # Update existing movie in DB elif movie and existing_movie: - TableMovies.update(movie).where(TableMovies.radarrId == movie['radarrId']).execute() - event_stream(type='movie', action='update', payload=int(movie_id)) - logging.debug('BAZARR updated this movie into the database:{}'.format(path_mappings.path_replace_movie( - movie['path']))) + try: + TableMovies.update(movie).where(TableMovies.radarrId == movie['radarrId']).execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert episode {movie['path']} because of {e}") + else: + event_stream(type='movie', action='update', payload=int(movie_id)) + logging.debug('BAZARR updated this movie into the database:{}'.format(path_mappings.path_replace_movie( + movie['path']))) # Insert new movie in DB elif movie and not existing_movie: - TableMovies.insert(movie).on_conflict(action='IGNORE').execute() - event_stream(type='movie', action='update', payload=int(movie_id)) - logging.debug('BAZARR inserted this movie into the database:{}'.format(path_mappings.path_replace_movie( - movie['path']))) + try: + TableMovies.insert(movie).on_conflict(action='IGNORE').execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert movie {movie['path']} because of {e}") + else: + event_stream(type='movie', action='update', payload=int(movie_id)) + logging.debug('BAZARR inserted this movie into the database:{}'.format(path_mappings.path_replace_movie( + movie['path']))) # Storing existing subtitles logging.debug('BAZARR storing subtitles for this movie: {}'.format(path_mappings.path_replace_movie( diff --git a/bazarr/get_series.py b/bazarr/get_series.py index 1dc1070ff..13dd566ea 100644 --- a/bazarr/get_series.py +++ b/bazarr/get_series.py @@ -3,7 +3,7 @@ import os import requests import logging -from peewee import DoesNotExist +from peewee import DoesNotExist, IntegrityError from config import settings, url_sonarr from list_subtitles import list_missing_subtitles @@ -76,9 +76,14 @@ def update_series(send_event=True): removed_series = list(set(current_shows_db_list) - set(current_shows_sonarr)) for series in removed_series: - TableShows.delete().where(TableShows.sonarrSeriesId == series).execute() - if send_event: - event_stream(type='series', action='delete', payload=series) + try: + TableShows.delete().where(TableShows.sonarrSeriesId == series).execute() + except Exception as e: + logging.error(f"BAZARR cannot delete series with sonarrSeriesId {series} because of {e}") + continue + else: + if send_event: + event_stream(type='series', action='delete', payload=series) # Update existing series in DB series_in_db_list = [] @@ -103,22 +108,32 @@ def update_series(send_event=True): series_to_update_list = [i for i in series_to_update if i not in series_in_db_list] for updated_series in series_to_update_list: - TableShows.update(updated_series).where(TableShows.sonarrSeriesId == - updated_series['sonarrSeriesId']).execute() - if send_event: - event_stream(type='series', payload=updated_series['sonarrSeriesId']) + try: + TableShows.update(updated_series).where(TableShows.sonarrSeriesId == + updated_series['sonarrSeriesId']).execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot update series {updated_series['path']} because of {e}") + continue + else: + if send_event: + event_stream(type='series', payload=updated_series['sonarrSeriesId']) # Insert new series in DB for added_series in series_to_add: - result = TableShows.insert(added_series).on_conflict(action='IGNORE').execute() - if result: - list_missing_subtitles(no=added_series['sonarrSeriesId']) + try: + result = TableShows.insert(added_series).on_conflict(action='IGNORE').execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert series {added_series['path']} because of {e}") + continue else: - logging.debug('BAZARR unable to insert this series into the database:', - path_mappings.path_replace(added_series['path'])) + if result: + list_missing_subtitles(no=added_series['sonarrSeriesId']) + else: + logging.debug('BAZARR unable to insert this series into the database:', + path_mappings.path_replace(added_series['path'])) - if send_event: - event_stream(type='series', action='update', payload=added_series['sonarrSeriesId']) + if send_event: + event_stream(type='series', action='update', payload=added_series['sonarrSeriesId']) logging.debug('BAZARR All series synced from Sonarr into database.') @@ -137,10 +152,14 @@ def update_one_series(series_id, action): # Delete series from DB if action == 'deleted' and existing_series: - TableShows.delete().where(TableShows.sonarrSeriesId == int(series_id)).execute() - TableEpisodes.delete().where(TableEpisodes.sonarrSeriesId == int(series_id)).execute() - event_stream(type='series', action='delete', payload=int(series_id)) - return + try: + TableShows.delete().where(TableShows.sonarrSeriesId == int(series_id)).execute() + except Exception as e: + logging.error(f"BAZARR cannot delete series with sonarrSeriesId {series_id} because of {e}") + else: + TableEpisodes.delete().where(TableEpisodes.sonarrSeriesId == int(series_id)).execute() + event_stream(type='series', action='delete', payload=int(series_id)) + return serie_default_enabled = settings.general.getboolean('serie_default_enabled') @@ -178,18 +197,26 @@ def update_one_series(series_id, action): # Update existing series in DB if action == 'updated' and existing_series: - TableShows.update(series).where(TableShows.sonarrSeriesId == series['sonarrSeriesId']).execute() - sync_episodes(series_id=int(series_id), send_event=True) - event_stream(type='series', action='update', payload=int(series_id)) - logging.debug('BAZARR updated this series into the database:{}'.format(path_mappings.path_replace( - series['path']))) + try: + TableShows.update(series).where(TableShows.sonarrSeriesId == series['sonarrSeriesId']).execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot update series {series['path']} because of {e}") + else: + sync_episodes(series_id=int(series_id), send_event=True) + event_stream(type='series', action='update', payload=int(series_id)) + logging.debug('BAZARR updated this series into the database:{}'.format(path_mappings.path_replace( + series['path']))) # Insert new series in DB elif action == 'updated' and not existing_series: - TableShows.insert(series).on_conflict(action='IGNORE').execute() - event_stream(type='series', action='update', payload=int(series_id)) - logging.debug('BAZARR inserted this series into the database:{}'.format(path_mappings.path_replace( - series['path']))) + try: + TableShows.insert(series).on_conflict(action='IGNORE').execute() + except IntegrityError as e: + logging.error(f"BAZARR cannot insert series {series['path']} because of {e}") + else: + event_stream(type='series', action='update', payload=int(series_id)) + logging.debug('BAZARR inserted this series into the database:{}'.format(path_mappings.path_replace( + series['path']))) def get_profile_list(): |