From d861cee4d2a171a5219bba672317f22777df3519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Sat, 13 Apr 2019 23:00:53 -0400 Subject: [PATCH 1/3] Search for subtitles when episodes and movies are added to Bazarr if there's no more than 5 items added (to avoid longer initial scan). #308 --- bazarr/get_episodes.py | 19 +++++++++++++------ bazarr/get_movies.py | 17 ++++++++++++----- bazarr/get_subtitle.py | 39 ++++++++++++++++++++++++++++++++++++++- bazarr/main.py | 4 ++-- 4 files changed, 65 insertions(+), 14 deletions(-) diff --git a/bazarr/get_episodes.py b/bazarr/get_episodes.py index 49ddc0a4c..9493c5d9e 100644 --- a/bazarr/get_episodes.py +++ b/bazarr/get_episodes.py @@ -11,6 +11,7 @@ from config import settings, url_sonarr from helper import path_replace from list_subtitles import list_missing_subtitles, store_subtitles, series_full_scan_subtitles, \ movies_full_scan_subtitles +from get_subtitle import episode_download_subtitles def update_all_episodes(): @@ -37,7 +38,7 @@ def sync_episodes(): c = db.cursor() # Get current episodes id in DB - current_episodes_db = c.execute('SELECT sonarrEpisodeId, path FROM table_episodes').fetchall() + current_episodes_db = c.execute('SELECT sonarrEpisodeId, path, sonarrSeriesId FROM table_episodes').fetchall() current_episodes_db_list = [x[0] for x in current_episodes_db] current_episodes_sonarr = [] @@ -137,7 +138,7 @@ def sync_episodes(): db.commit() # Get episodes list after INSERT and UPDATE - episodes_now_in_db = c.execute('SELECT sonarrEpisodeId, path FROM table_episodes').fetchall() + episodes_now_in_db = c.execute('SELECT sonarrEpisodeId, path, sonarrSeriesId FROM table_episodes').fetchall() # Close database connection c.close() @@ -146,12 +147,18 @@ def sync_episodes(): 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])) + list_missing_subtitles(altered_episode[2]) logging.debug('BAZARR All episodes synced from Sonarr into database.') - - list_missing_subtitles() - logging.debug('BAZARR All missing subtitles updated in 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.") + notifications.write(msg='Episodes sync from Sonarr ended.', queue='get_episodes') diff --git a/bazarr/get_movies.py b/bazarr/get_movies.py index 858142a70..484630da7 100644 --- a/bazarr/get_movies.py +++ b/bazarr/get_movies.py @@ -10,6 +10,7 @@ from get_args import args from config import settings, url_radarr from helper import path_replace_movie from list_subtitles import store_subtitles_movie, list_missing_subtitles_movies +from get_subtitle import movies_download_subtitles def update_movies(): @@ -42,7 +43,7 @@ def update_movies(): # Get current movies in DB db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() - current_movies_db = c.execute('SELECT tmdbId, path FROM table_movies').fetchall() + current_movies_db = c.execute('SELECT tmdbId, path, radarrId FROM table_movies').fetchall() db.close() current_movies_db_list = [x[0] for x in current_movies_db] @@ -175,7 +176,7 @@ def update_movies(): db.commit() # Get movies list after INSERT and UPDATE - movies_now_in_db = c.execute('SELECT tmdbId, path FROM table_movies').fetchall() + movies_now_in_db = c.execute('SELECT tmdbId, path, radarrId FROM table_movies').fetchall() # Close database connection db.close() @@ -184,12 +185,18 @@ def update_movies(): 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])) + list_missing_subtitles_movies(altered_movie[2]) + + # 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.") + for altered_movie in altered_movies: + movies_download_subtitles(altered_movie[2]) + 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.') - list_missing_subtitles_movies() - logging.debug('BAZARR All movie missing subtitles updated in database.') - notifications.write(msg="Update movies list from Radarr is ended.", queue='get_movies') diff --git a/bazarr/get_subtitle.py b/bazarr/get_subtitle.py index 1e617e020..d59b90b67 100644 --- a/bazarr/get_subtitle.py +++ b/bazarr/get_subtitle.py @@ -469,6 +469,43 @@ def series_download_subtitles(no): notifications.write(msg='Searching completed. Please reload the page.', type='success', duration='permanent', button='refresh', queue='get_subtitle') +def episode_download_subtitles(no): + if settings.sonarr.getboolean('only_monitored'): + monitored_only_query_string = ' AND monitored = "True"' + else: + monitored_only_query_string = "" + + conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) + c_db = conn_db.cursor() + episodes_details = c_db.execute( + 'SELECT table_episodes.path, table_episodes.missing_subtitles, table_episodes.sonarrEpisodeId, table_episodes.scene_name, table_shows.hearing_impaired, table_shows.title, table_shows.sonarrSeriesId FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.sonarrEpisodeId = ?' + monitored_only_query_string, (no,)).fetchall() + c_db.close() + + providers_list = get_providers() + providers_auth = get_providers_auth() + + for episode in episodes_details: + for language in ast.literal_eval(episode[1]): + if language is not None: + notifications.write(msg='Searching for ' + str( + language_from_alpha2(language)) + ' subtitles for this episode: ' + path_replace(episode[0]), + queue='get_subtitle') + result = download_subtitle(path_replace(episode[0]), str(alpha3_from_alpha2(language)), + episode[4], providers_list, providers_auth, str(episode[3]), + episode[5], 'series') + if result is not None: + message = result[0] + path = result[1] + language_code = result[2] + provider = result[3] + score = result[4] + store_subtitles(path_replace(episode[0])) + history_log(1, episode[6], episode[2], message, path, language_code, provider, score) + send_notifications(episode[6], episode[2], message) + + list_missing_subtitles(episode[6]) + + def movies_download_subtitles(no): conn_db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c_db = conn_db.cursor() @@ -476,7 +513,7 @@ def movies_download_subtitles(no): "SELECT path, missing_subtitles, radarrId, sceneName, hearing_impaired, title FROM table_movies WHERE radarrId = ?", (no,)).fetchone() c_db.close() - + providers_list = get_providers() providers_auth = get_providers_auth() diff --git a/bazarr/main.py b/bazarr/main.py index a8c1e8e26..2234bb988 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -1762,7 +1762,7 @@ def remove_subtitles_movie(): list_missing_subtitles_movies(radarrId) -@route(base_url + 'get_subtitle', method='POST') +@route(base_url + 'INNtle', method='POST') @custom_auth_basic(check_credentials) def get_subtitle(): authorize() @@ -1956,7 +1956,7 @@ def api_wanted(): db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) c = db.cursor() data = c.execute( - "SELECT table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_episodes.missing_subtitles FROM table_episodes INNER JOIN table_shows on table_shows.sonarrSeriesId = table_episodes.sonarrSeriesId WHERE table_episodes.missing_subtitles != '[]' ORDER BY table_episodes._rowid_ DESC LIMIT 10").fetchall() + "SELECT table_shows.title, table_episodes.season || 'x' || table_episodes.episode, table_episodes.title, table_episodes.missing_subtitles FROM table_episodes prin WHERE table_episodes.missing_subtitles != '[]' ORDER BY table_episodes._rowid_ DESC LIMIT 10").fetchall() c.close() return dict(subtitles=data) From be5e7d9169949c9649c00840ee768a42478f48f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Sat, 13 Apr 2019 23:02:24 -0400 Subject: [PATCH 2/3] Version Bump. --- bazarr/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazarr/main.py b/bazarr/main.py index 2234bb988..534ce135b 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -1,6 +1,6 @@ # coding=utf-8 -bazarr_version = '0.7.3' +bazarr_version = '0.7.4' import gc import sys From e24354ce9fdfde8f538b7a2d055caf4f110c9773 Mon Sep 17 00:00:00 2001 From: Halali Date: Sun, 14 Apr 2019 07:33:56 +0200 Subject: [PATCH 3/3] Fix typo from last commit --- bazarr/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bazarr/main.py b/bazarr/main.py index 534ce135b..08a647715 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -1762,7 +1762,7 @@ def remove_subtitles_movie(): list_missing_subtitles_movies(radarrId) -@route(base_url + 'INNtle', method='POST') +@route(base_url + 'get_subtitle', method='POST') @custom_auth_basic(check_credentials) def get_subtitle(): authorize()