Major improvement to episodes synchronization

This commit is contained in:
morpheus65535 2018-09-24 22:22:27 -04:00
parent 0f2371423d
commit e6a44bcbd6

View file

@ -31,11 +31,19 @@ def sync_episodes():
c = db.cursor() c = db.cursor()
# Get current episodes id in DB # Get current episodes id in DB
current_episodes_db = c.execute('SELECT sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored FROM table_episodes').fetchall() current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall()
current_episodes_db_list = [x[0] for x in current_episodes_db]
current_episodes_sonarr = []
episodes_to_update = []
episodes_to_add = []
# Get sonarrId for each series from database # Get sonarrId for each series from database
current_episodes_sonarr = []
seriesIdList = c.execute("SELECT sonarrSeriesId FROM table_shows").fetchall() seriesIdList = c.execute("SELECT sonarrSeriesId FROM table_shows").fetchall()
# Close database connection
c.close()
for seriesId in seriesIdList: for seriesId in seriesIdList:
# Get episodes data for a series from Sonarr # Get episodes data for a series from Sonarr
url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr url_sonarr_api_episode = url_sonarr + "/api/episode?seriesId=" + str(seriesId[0]) + "&apikey=" + apikey_sonarr
@ -61,27 +69,41 @@ def sync_episodes():
sceneName = episode['episodeFile']['sceneName'] sceneName = episode['episodeFile']['sceneName']
else: else:
sceneName = None sceneName = None
current_episodes_sonarr.append((episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored']))))
added_episodes = list(set(current_episodes_sonarr) - set(current_episodes_db)) # Add episodes in sonarr to current episode list
removed_episodes = list(set(current_episodes_db) - set(current_episodes_sonarr)) current_episodes_sonarr.append(episode['id'])
for removed_episode in removed_episodes: if episode['id'] in current_episodes_db_list:
c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode[1],)) episodes_to_update.append((episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored'])), episode['id']))
else:
episodes_to_add.append((episode['seriesId'], episode['id'], episode['title'], episode['episodeFile']['path'], episode['seasonNumber'], episode['episodeNumber'], sceneName, str(bool(episode['monitored']))))
removed_episodes = list(set(current_episodes_db_list) - set(current_episodes_sonarr))
# Update or insert movies in DB
db = sqlite3.connect(os.path.join(config_dir, 'db/bazarr.db'), timeout=30)
c = db.cursor()
updated_result = c.executemany('''UPDATE table_episodes SET title = ?, path = ?, season = ?, episode = ?, scene_name = ?, monitored = ? WHERE sonarrEpisodeId = ?''', episodes_to_update)
db.commit()
try:
added_result = c.executemany('''INSERT INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', episodes_to_add)
except sqlite3.IntegrityError as e:
logging.exception("You're probably an early adopter of Bazarr and this is a known issue. Please open an issue on Github and we'll fix this.")
else:
db.commit() db.commit()
for added_episode in added_episodes: for removed_episode in removed_episodes:
try: c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode,))
c.execute('''INSERT INTO table_episodes(sonarrSeriesId, sonarrEpisodeId, title, path, season, episode, scene_name, monitored) VALUES (?, ?, ?, ?, ?, ?, ?, ?)''', added_episode) db.commit()
except sqlite3.IntegrityError as e:
logging.exception("You're probably an early adopter of Bazarr and this is a known issue. Please open an issue on Github and we'll fix this.")
else:
db.commit()
store_subtitles(path_replace(added_episode[3]))
# Close database connection # Close database connection
c.close() c.close()
for added_episode in episodes_to_add:
store_subtitles(path_replace(added_episode[3]))
logging.debug('All episodes synced from Sonarr into database.') logging.debug('All episodes synced from Sonarr into database.')
list_missing_subtitles() list_missing_subtitles()