diff --git a/bazarr/subtitles/download.py b/bazarr/subtitles/download.py index f0511714a..a5029089b 100644 --- a/bazarr/subtitles/download.py +++ b/bazarr/subtitles/download.py @@ -5,6 +5,7 @@ import os import sys import logging import subliminal +import ast from subzero.language import Language from subliminal_patch.core import save_subtitles @@ -12,6 +13,8 @@ from subliminal_patch.core_persistent import download_best_subtitles from subliminal_patch.score import ComputeScore from app.config import settings, get_array_from, get_scores +from app.database import TableEpisodes, TableMovies +from utilities.path_mappings import path_mappings from utilities.helper import get_target_folder, force_unicode from languages.get_languages import alpha3_from_alpha2 @@ -22,7 +25,7 @@ from .processing import process_subtitle @update_pools def generate_subtitles(path, languages, audio_language, sceneName, title, media_type, - forced_minimum_score=None, is_upgrade=False, profile_id=None): + forced_minimum_score=None, is_upgrade=False, profile_id=None, check_if_still_required=False): if not languages: return None @@ -49,58 +52,67 @@ def generate_subtitles(path, languages, audio_language, sceneName, title, media_ minimum_score_movie = settings.general.minimum_score_movie min_score, max_score, scores = _get_scores(media_type, minimum_score_movie, minimum_score) + subz_mods = get_array_from(settings.general.subzero_mods) + saved_any = False + if providers: if forced_minimum_score: min_score = int(forced_minimum_score) + 1 - downloaded_subtitles = download_best_subtitles(videos={video}, - languages=language_set, - pool_instance=pool, - min_score=int(min_score), - hearing_impaired=hi_required, - compute_score=ComputeScore(get_scores())) - else: - downloaded_subtitles = None - logging.info("BAZARR All providers are throttled") - return None - - subz_mods = get_array_from(settings.general.subzero_mods) - saved_any = False - if downloaded_subtitles: - for video, subtitles in downloaded_subtitles.items(): - if not subtitles: + for language in language_set: + # confirm if language is still missing or if cutoff has been reached + if check_if_still_required and language not in check_missing_languages(path, media_type): + # cutoff has been reached + logging.debug(f"BAZARR this language ({parse_language_object(language)}) is ignored because cutoff " + f"has been reached during this search.") continue - - subtitle_formats = set() - for s in subtitles: - s.mods = subz_mods - subtitle_formats.add(s.format) - - try: - fld = get_target_folder(path) - chmod = int(settings.general.chmod, 8) if not sys.platform.startswith( - 'win') and settings.general.getboolean('chmod_enabled') else None - saved_subtitles = save_subtitles(video.original_path, subtitles, - single=settings.general.getboolean('single_language'), - tags=None, # fixme - directory=fld, - chmod=chmod, - formats=tuple(subtitle_formats), - path_decoder=force_unicode - ) - except Exception as e: - logging.exception( - 'BAZARR Error saving Subtitles file to disk for this file:' + path + ': ' + repr(e)) - pass else: - saved_any = True - for subtitle in saved_subtitles: - processed_subtitle = process_subtitle(subtitle=subtitle, media_type=media_type, - audio_language=audio_language, is_upgrade=is_upgrade, - is_manual=False, path=path, max_score=max_score) - if not processed_subtitle: - logging.debug(f"BAZARR unable to process this subtitles: {subtitle}") + downloaded_subtitles = download_best_subtitles(videos={video}, + languages={language}, + pool_instance=pool, + min_score=int(min_score), + hearing_impaired=hi_required, + compute_score=ComputeScore(get_scores())) + + if downloaded_subtitles: + for video, subtitles in downloaded_subtitles.items(): + if not subtitles: continue - yield processed_subtitle + + subtitle_formats = set() + for s in subtitles: + s.mods = subz_mods + subtitle_formats.add(s.format) + + try: + fld = get_target_folder(path) + chmod = int(settings.general.chmod, 8) if not sys.platform.startswith( + 'win') and settings.general.getboolean('chmod_enabled') else None + saved_subtitles = save_subtitles(video.original_path, subtitles, + single=settings.general.getboolean('single_language'), + tags=None, # fixme + directory=fld, + chmod=chmod, + formats=tuple(subtitle_formats), + path_decoder=force_unicode + ) + except Exception as e: + logging.exception( + 'BAZARR Error saving Subtitles file to disk for this file:' + path + ': ' + repr(e)) + pass + else: + saved_any = True + for subtitle in saved_subtitles: + processed_subtitle = process_subtitle(subtitle=subtitle, media_type=media_type, + audio_language=audio_language, + is_upgrade=is_upgrade, is_manual=False, + path=path, max_score=max_score) + if not processed_subtitle: + logging.debug(f"BAZARR unable to process this subtitles: {subtitle}") + continue + yield processed_subtitle + else: + logging.info("BAZARR All providers are throttled") + return None if not saved_any: logging.debug('BAZARR No Subtitles were found for this file: ' + path) @@ -137,3 +149,38 @@ def _get_language_obj(languages): language_set.add(lang_obj) return language_set + + +def parse_language_object(language): + if isinstance(language, Language): + hi = ":hi" if language.hi else "" + forced = ":forced" if language.forced else "" + return language.basename + hi + forced + else: + return language + + +def check_missing_languages(path, media_type): + # confirm if language is still missing or if cutoff has been reached + if media_type == 'series': + confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \ + .where(TableEpisodes.path == path_mappings.path_replace_reverse(path)) \ + .dicts() \ + .get_or_none() + else: + confirmed_missing_subs = TableMovies.select(TableMovies.missing_subtitles) \ + .where(TableMovies.path == path_mappings.path_replace_movie_reverse(path)) \ + .dicts() \ + .get_or_none() + + if not confirmed_missing_subs: + return None + + languages = [] + for language in ast.literal_eval(confirmed_missing_subs['missing_subtitles']): + if language is not None: + hi_ = "True" if language.endswith(':hi') else "False" + forced_ = "True" if language.endswith(':forced') else "False" + languages.append((language.split(":")[0], hi_, forced_)) + + return _get_language_obj(languages=languages) diff --git a/bazarr/subtitles/mass_download/movies.py b/bazarr/subtitles/mass_download/movies.py index e0349ad7f..c77ab7c59 100644 --- a/bazarr/subtitles/mass_download/movies.py +++ b/bazarr/subtitles/mass_download/movies.py @@ -58,17 +58,6 @@ def movies_download_subtitles(no): hi_ = "True" if language.endswith(':hi') else "False" forced_ = "True" if language.endswith(':forced') else "False" languages.append((language.split(":")[0], hi_, forced_)) - - # confirm if language is still missing or if cutoff have been reached - confirmed_missing_subs = TableMovies.select(TableMovies.missing_subtitles) \ - .where(TableMovies.radarrId == movie['radarrId']) \ - .dicts() \ - .get_or_none() - if not confirmed_missing_subs: - continue - - if language not in ast.literal_eval(confirmed_missing_subs['missing_subtitles']): - continue else: logging.info("BAZARR All providers are throttled") break @@ -84,7 +73,8 @@ def movies_download_subtitles(no): audio_language, str(movie['sceneName']), movie['title'], - 'movie'): + 'movie', + check_if_still_required=True): if result: message = result[0] diff --git a/bazarr/subtitles/mass_download/series.py b/bazarr/subtitles/mass_download/series.py index 0b1491d98..cd67981ad 100644 --- a/bazarr/subtitles/mass_download/series.py +++ b/bazarr/subtitles/mass_download/series.py @@ -65,17 +65,6 @@ def series_download_subtitles(no): languages = [] for language in ast.literal_eval(episode['missing_subtitles']): - # confirm if language is still missing or if cutoff have been reached - confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \ - .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \ - .dicts() \ - .get_or_none() - if not confirmed_missing_subs: - continue - - if language not in ast.literal_eval(confirmed_missing_subs['missing_subtitles']): - continue - if language is not None: hi_ = "True" if language.endswith(':hi') else "False" forced_ = "True" if language.endswith(':forced') else "False" @@ -88,7 +77,9 @@ def series_download_subtitles(no): languages, audio_language, str(episode['scene_name']), - episode['title'], 'series'): + episode['title'], + 'series', + check_if_still_required=True): if result: message = result[0] path = result[1] @@ -159,17 +150,6 @@ def episode_download_subtitles(no, send_progress=False): languages = [] for language in ast.literal_eval(episode['missing_subtitles']): - # confirm if language is still missing or if cutoff have been reached - confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \ - .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \ - .dicts() \ - .get_or_none() - if not confirmed_missing_subs: - continue - - if language not in ast.literal_eval(confirmed_missing_subs['missing_subtitles']): - continue - if language is not None: hi_ = "True" if language.endswith(':hi') else "False" forced_ = "True" if language.endswith(':forced') else "False" @@ -183,7 +163,8 @@ def episode_download_subtitles(no, send_progress=False): audio_language, str(episode['scene_name']), episode['title'], - 'series'): + 'series', + check_if_still_required=True): if result: message = result[0] path = result[1] diff --git a/bazarr/subtitles/wanted/movies.py b/bazarr/subtitles/wanted/movies.py index 210d9271c..d82d4488d 100644 --- a/bazarr/subtitles/wanted/movies.py +++ b/bazarr/subtitles/wanted/movies.py @@ -29,17 +29,6 @@ def _wanted_movie(movie): languages = [] for language in ast.literal_eval(movie['missing_subtitles']): - # confirm if language is still missing or if cutoff have been reached - confirmed_missing_subs = TableMovies.select(TableMovies.missing_subtitles) \ - .where(TableMovies.radarrId == movie['radarrId']) \ - .dicts() \ - .get_or_none() - if not confirmed_missing_subs: - continue - - if language not in ast.literal_eval(confirmed_missing_subs['missing_subtitles']): - continue - if is_search_active(desired_language=language, attempt_string=movie['failedAttempts']): TableMovies.update({TableMovies.failedAttempts: updateFailedAttempts(desired_language=language, @@ -59,7 +48,9 @@ def _wanted_movie(movie): languages, audio_language, str(movie['sceneName']), - movie['title'], 'movie'): + movie['title'], + 'movie', + check_if_still_required=True): if result: message = result[0] diff --git a/bazarr/subtitles/wanted/series.py b/bazarr/subtitles/wanted/series.py index 668d4205b..de6836ca1 100644 --- a/bazarr/subtitles/wanted/series.py +++ b/bazarr/subtitles/wanted/series.py @@ -28,18 +28,6 @@ def _wanted_episode(episode): languages = [] for language in ast.literal_eval(episode['missing_subtitles']): - - # confirm if language is still missing or if cutoff have been reached - confirmed_missing_subs = TableEpisodes.select(TableEpisodes.missing_subtitles) \ - .where(TableEpisodes.sonarrEpisodeId == episode['sonarrEpisodeId']) \ - .dicts() \ - .get_or_none() - if not confirmed_missing_subs: - continue - - if language not in ast.literal_eval(confirmed_missing_subs['missing_subtitles']): - continue - if is_search_active(desired_language=language, attempt_string=episode['failedAttempts']): TableEpisodes.update({TableEpisodes.failedAttempts: updateFailedAttempts(desired_language=language, @@ -61,7 +49,8 @@ def _wanted_episode(episode): audio_language, str(episode['scene_name']), episode['title'], - 'series'): + 'series', + check_if_still_required=True): if result: message = result[0] path = result[1]