Fix for episodes being removed from DB if connection to Sonarr fail.

This commit is contained in:
Louis Vézina 2019-09-11 20:01:48 -04:00
parent 94474e8eae
commit 90609b6433
3 changed files with 58 additions and 56 deletions

View File

@ -127,69 +127,69 @@ def sync_episodes():
'video_codec': videoCodec,
'audio_codec': audioCodec})
# Update existing episodes in DB
episode_in_db_list = []
episodes_in_db = TableEpisodes.select(
TableEpisodes.sonarr_series_id,
TableEpisodes.sonarr_episode_id,
TableEpisodes.title,
TableEpisodes.path,
TableEpisodes.season,
TableEpisodes.episode,
TableEpisodes.scene_name,
TableEpisodes.monitored,
TableEpisodes.format,
TableEpisodes.resolution,
TableEpisodes.video_codec,
TableEpisodes.audio_codec
).dicts()
# Update existing episodes in DB
episode_in_db_list = []
episodes_in_db = TableEpisodes.select(
TableEpisodes.sonarr_series_id,
TableEpisodes.sonarr_episode_id,
TableEpisodes.title,
TableEpisodes.path,
TableEpisodes.season,
TableEpisodes.episode,
TableEpisodes.scene_name,
TableEpisodes.monitored,
TableEpisodes.format,
TableEpisodes.resolution,
TableEpisodes.video_codec,
TableEpisodes.audio_codec
).dicts()
for item in episodes_in_db:
episode_in_db_list.append(item)
for item in episodes_in_db:
episode_in_db_list.append(item)
episodes_to_update_list = [i for i in episodes_to_update if i not in episode_in_db_list]
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.sonarr_episode_id == updated_episode['sonarr_episode_id']
).execute()
altered_episodes.append([updated_episode['sonarr_episode_id'],
updated_episode['path'],
updated_episode['sonarr_series_id']])
for updated_episode in episodes_to_update_list:
TableEpisodes.update(
updated_episode
).where(
TableEpisodes.sonarr_episode_id == updated_episode['sonarr_episode_id']
).execute()
altered_episodes.append([updated_episode['sonarr_episode_id'],
updated_episode['path'],
updated_episode['sonarr_series_id']])
# Insert new episodes in DB
for added_episode in episodes_to_add:
TableEpisodes.insert(
added_episode
).on_conflict_ignore().execute()
altered_episodes.append([added_episode['sonarr_episode_id'],
added_episode['path'],
added_episode['sonarr_series_id']])
# Insert new episodes in DB
for added_episode in episodes_to_add:
TableEpisodes.insert(
added_episode
).on_conflict_ignore().execute()
altered_episodes.append([added_episode['sonarr_episode_id'],
added_episode['path'],
added_episode['sonarr_series_id']])
# Remove old episodes from DB
removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr))
# Remove old episodes from DB
removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr))
for removed_episode in removed_episodes:
TableEpisodes.delete().where(
TableEpisodes.sonarr_episode_id == removed_episode
).execute()
for removed_episode in removed_episodes:
TableEpisodes.delete().where(
TableEpisodes.sonarr_episode_id == removed_episode
).execute()
# Store subtitles for added or modified episodes
for altered_episode in altered_episodes:
store_subtitles(path_replace(altered_episode[1]))
list_missing_subtitles(altered_episode[2])
# Store subtitles for added or modified episodes
for altered_episode in altered_episodes:
store_subtitles(path_replace(altered_episode[1]))
list_missing_subtitles(altered_episode[2])
logging.debug('BAZARR All episodes synced from Sonarr into database.')
logging.debug('BAZARR All episodes synced from Sonarr into database.')
# Search for desired subtitles if no more than 5 episodes have been added.
if len(altered_episodes) <= 5:
logging.debug("BAZARR No more than 5 episodes were added during this sync then we'll search for subtitles.")
for altered_episode in altered_episodes:
episode_download_subtitles(altered_episode[0])
else:
logging.debug("BAZARR More than 5 episodes were added during this sync then we wont search for subtitles right now.")
# Search for desired subtitles if no more than 5 episodes have been added.
if len(altered_episodes) <= 5:
logging.debug("BAZARR No more than 5 episodes were added during this sync then we'll search for subtitles.")
for altered_episode in altered_episodes:
episode_download_subtitles(altered_episode[0])
else:
logging.debug("BAZARR More than 5 episodes were added during this sync then we wont search for subtitles right now.")
def SonarrFormatAudioCodec(audioCodec):

View File

@ -261,6 +261,8 @@ def update_movies():
store_subtitles_movie(path_replace_movie(altered_movie[1]))
list_missing_subtitles_movies(altered_movie[2])
logging.debug('BAZARR All movies synced from Radarr into database.')
# Search for desired subtitles if no more than 5 movies have been added.
if len(altered_movies) <= 5:
logging.debug("BAZARR No more than 5 movies were added during this sync then we'll search for subtitles.")
@ -269,8 +271,6 @@ def update_movies():
else:
logging.debug("BAZARR More than 5 movies were added during this sync then we wont search for subtitles.")
logging.debug('BAZARR All movies synced from Radarr into database.')
def get_profile_list():
apikey_radarr = settings.radarr.apikey

View File

@ -161,6 +161,8 @@ def update_series():
TableShows.tvdb_id == series
).execute()
logging.debug('BAZARR All series synced from Sonarr into database.')
def get_profile_list():
apikey_sonarr = settings.sonarr.apikey