diff --git a/bazarr/sonarr/sync/episodes.py b/bazarr/sonarr/sync/episodes.py index ed1679ff8..b6979ad30 100644 --- a/bazarr/sonarr/sync/episodes.py +++ b/bazarr/sonarr/sync/episodes.py @@ -145,6 +145,20 @@ def sync_episodes(series_id, send_event=True): if send_event: event_stream(type='episode', action='delete', payload=removed_episode) + # Insert new episodes in DB + if len(episodes_to_add): + for added_episode in episodes_to_add: + try: + database.execute(insert(TableEpisodes).values(added_episode)) + except IntegrityError as e: + logging.error(f"BAZARR cannot insert episodes because of {e}. We'll try to update it instead.") + episodes_to_update.append(added_episode) + else: + store_subtitles(added_episode['path'], path_mappings.path_replace(added_episode['path'])) + + if send_event: + event_stream(type='episode', payload=added_episode['sonarrEpisodeId']) + # Update existing episodes in DB if len(episodes_to_update): for updated_episode in episodes_to_update: @@ -160,19 +174,6 @@ def sync_episodes(series_id, send_event=True): if send_event: event_stream(type='episode', action='update', payload=updated_episode['sonarrEpisodeId']) - # Insert new episodes in DB - if len(episodes_to_add): - for added_episode in episodes_to_add: - try: - database.execute(insert(TableEpisodes).values(added_episode)) - except IntegrityError as e: - logging.error(f"BAZARR cannot insert episodes because of {e}") - else: - store_subtitles(added_episode['path'], path_mappings.path_replace(added_episode['path'])) - - if send_event: - event_stream(type='episode', payload=added_episode['sonarrEpisodeId']) - logging.debug(f'BAZARR All episodes from series ID {series_id} synced from Sonarr into database.')