diff --git a/bazarr/get_episodes.py b/bazarr/get_episodes.py index 61cc1b0b6..43be7d7e7 100644 --- a/bazarr/get_episodes.py +++ b/bazarr/get_episodes.py @@ -35,7 +35,7 @@ def sync_episodes(): c = db.cursor() # Get current episodes id in DB - current_episodes_db = c.execute('SELECT sonarrEpisodeId FROM table_episodes').fetchall() + current_episodes_db = c.execute('SELECT sonarrEpisodeId, path FROM table_episodes').fetchall() current_episodes_db_list = [x[0] for x in current_episodes_db] current_episodes_sonarr = [] @@ -99,15 +99,16 @@ def sync_episodes(): c.execute('DELETE FROM table_episodes WHERE sonarrEpisodeId = ?', (removed_episode,)) db.commit() + # Get episodes list after INSERT and UPDATE + episodes_now_in_db = c.execute('SELECT sonarrEpisodeId, path FROM table_episodes').fetchall() + # Close database connection c.close() - # TODO: Commented until I find a way to make it store only episodes really updated. - #for updated_episode in episodes_to_update: - # store_subtitles(path_replace(updated_episode[1])) - - for added_episode in episodes_to_add: - store_subtitles(path_replace(added_episode[3])) + # Get only episodes added or modified and store subtitles for them + altered_episodes = set(episodes_now_in_db).difference(set(current_episodes_db)) + for altered_episode in altered_episodes: + store_subtitles(path_replace(altered_episode[1])) logging.debug('BAZARR All episodes synced from Sonarr into database.') diff --git a/bazarr/get_movies.py b/bazarr/get_movies.py index a9c5f57b1..0e12bfa47 100644 --- a/bazarr/get_movies.py +++ b/bazarr/get_movies.py @@ -41,7 +41,7 @@ def update_movies(): # Get current movies in DB db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - current_movies_db = c.execute('SELECT tmdbId FROM table_movies').fetchall() + current_movies_db = c.execute('SELECT tmdbId, path FROM table_movies').fetchall() db.close() current_movies_db_list = [x[0] for x in current_movies_db] @@ -105,23 +105,23 @@ def update_movies(): else: added_result = c.executemany('''INSERT OR IGNORE INTO table_movies(title, path, tmdbId, languages, subtitles,`hearing_impaired`, radarrId, overview, poster, fanart, `audio_language`, sceneName, monitored, sortTitle) VALUES (?,?,?,(SELECT languages FROM table_movies WHERE tmdbId = ?), '[]',(SELECT `hearing_impaired` FROM table_movies WHERE tmdbId = ?), ?, ?, ?, ?, ?, ?, ?, ?)''', movies_to_add) db.commit() - db.close() removed_movies = list(set(current_movies_db_list) - set(current_movies_radarr)) - db = sqlite3.connect(os.path.join(config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() for removed_movie in removed_movies: c.execute('DELETE FROM table_movies WHERE tmdbId = ?', (removed_movie,)) - db.commit() - db.close() + db.commit() - for added_movie in movies_to_add: - store_subtitles_movie(path_replace_movie(added_movie[1])) + # Get movies list after INSERT and UPDATE + movies_now_in_db = c.execute('SELECT tmdbId, path FROM table_movies').fetchall() + + # Close database connection + db.close() - # TODO: Commented until I find a way to make it store only episodes really updated. - # for updated_movie in movies_to_update: - # store_subtitles_movie(path_replace_movie(updated_movie[1])) + # Get only movies added or modified and store subtitles for them + altered_movies = set(movies_now_in_db).difference(set(current_movies_db)) + for altered_movie in altered_movies: + store_subtitles_movie(path_replace_movie(altered_movie[1])) logging.debug('BAZARR All movies synced from Radarr into database.')