diff options
author | morpheus65535 <[email protected]> | 2023-02-23 11:18:57 -0500 |
---|---|---|
committer | GitHub <[email protected]> | 2023-02-23 11:18:57 -0500 |
commit | 5b283098f9ec0804706225cc0b1266515e034be7 (patch) | |
tree | 96d3fa3d7317dbb1f1bbc5935608ae59151424c9 | |
parent | 72066245e0a5b6a27d93cc46245403e309ee4b35 (diff) | |
download | bazarr-5b283098f9ec0804706225cc0b1266515e034be7.tar.gz bazarr-5b283098f9ec0804706225cc0b1266515e034be7.zip |
Refactored subtitles upgrade
-rw-r--r-- | bazarr/api/episodes/episodes_subtitles.py | 36 | ||||
-rw-r--r-- | bazarr/api/episodes/history.py | 55 | ||||
-rw-r--r-- | bazarr/api/movies/history.py | 54 | ||||
-rw-r--r-- | bazarr/api/movies/movies_subtitles.py | 33 | ||||
-rw-r--r-- | bazarr/api/providers/providers_episodes.py | 23 | ||||
-rw-r--r-- | bazarr/api/providers/providers_movies.py | 19 | ||||
-rw-r--r-- | bazarr/radarr/history.py | 11 | ||||
-rw-r--r-- | bazarr/sonarr/history.py | 11 | ||||
-rw-r--r-- | bazarr/subtitles/mass_download/movies.py | 17 | ||||
-rw-r--r-- | bazarr/subtitles/mass_download/series.py | 36 | ||||
-rw-r--r-- | bazarr/subtitles/processing.py | 29 | ||||
-rw-r--r-- | bazarr/subtitles/upgrade.py | 359 | ||||
-rw-r--r-- | bazarr/subtitles/wanted/movies.py | 18 | ||||
-rw-r--r-- | bazarr/subtitles/wanted/series.py | 18 |
14 files changed, 251 insertions, 468 deletions
diff --git a/bazarr/api/episodes/episodes_subtitles.py b/bazarr/api/episodes/episodes_subtitles.py index 5dfaf0eaa..74818a7cf 100644 --- a/bazarr/api/episodes/episodes_subtitles.py +++ b/bazarr/api/episodes/episodes_subtitles.py @@ -74,23 +74,9 @@ class EpisodesSubtitles(Resource): title, 'series', profile_id=get_profile_id(episode_id=sonarrEpisodeId))) if result: result = result[0] - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] - history_log(1, sonarrSeriesId, sonarrEpisodeId, message, path, language_code, provider, score, subs_id, - subs_path) - send_notifications(sonarrSeriesId, sonarrEpisodeId, message) - store_subtitles(path, episodePath) + history_log(1, sonarrSeriesId, sonarrEpisodeId, result) + send_notifications(sonarrSeriesId, sonarrEpisodeId, result.message) + store_subtitles(result.path, episodePath) else: event_stream(type='episode', payload=sonarrEpisodeId) @@ -157,22 +143,12 @@ class EpisodesSubtitles(Resource): if not result: logging.debug(f"BAZARR unable to process subtitles for this episode: {episodePath}") else: - message = result[0] - path = result[1] - subs_path = result[2] - if hi: - language_code = language + ":hi" - elif forced: - language_code = language + ":forced" - else: - language_code = language provider = "manual" score = 360 - history_log(4, sonarrSeriesId, sonarrEpisodeId, message, path, language_code, provider, score, - subtitles_path=subs_path) + history_log(4, sonarrSeriesId, sonarrEpisodeId, result, fake_provider=provider, fake_score=score) if not settings.general.getboolean('dont_notify_manual_actions'): - send_notifications(sonarrSeriesId, sonarrEpisodeId, message) - store_subtitles(path, episodePath) + send_notifications(sonarrSeriesId, sonarrEpisodeId, result.message) + store_subtitles(result.path, episodePath) except OSError: pass diff --git a/bazarr/api/episodes/history.py b/bazarr/api/episodes/history.py index 658253558..128f9eb32 100644 --- a/bazarr/api/episodes/history.py +++ b/bazarr/api/episodes/history.py @@ -1,17 +1,14 @@ # coding=utf-8 -import datetime import os import operator import pretty from flask_restx import Resource, Namespace, reqparse, fields from functools import reduce -from peewee import fn -from datetime import timedelta -from app.database import get_exclusion_clause, TableEpisodes, TableShows, TableHistory, TableBlacklist -from app.config import settings +from app.database import TableEpisodes, TableShows, TableHistory, TableBlacklist +from subtitles.upgrade import get_upgradable_episode_subtitles from utilities.path_mappings import path_mappings from api.swaggerui import subtitles_language_model @@ -70,45 +67,15 @@ class EpisodesHistory(Resource): length = args.get('length') episodeid = args.get('episodeid') - upgradable_episodes_not_perfect = [] - if settings.general.getboolean('upgrade_subs'): - days_to_upgrade_subs = settings.general.days_to_upgrade_subs - minimum_timestamp = (datetime.datetime.now() - timedelta(days=int(days_to_upgrade_subs))) - - if settings.general.getboolean('upgrade_manual'): - query_actions = [1, 2, 3, 6] - else: - query_actions = [1, 3] - - upgradable_episodes_conditions = [(TableHistory.action.in_(query_actions)), - (TableHistory.timestamp > minimum_timestamp), - (TableHistory.score.is_null(False))] - upgradable_episodes_conditions += get_exclusion_clause('series') - upgradable_episodes = TableHistory.select(TableHistory.video_path, - fn.MAX(TableHistory.timestamp).alias('timestamp'), - TableHistory.score, - TableShows.tags, - TableEpisodes.monitored, - TableShows.seriesType) \ - .join(TableEpisodes, on=(TableHistory.sonarrEpisodeId == TableEpisodes.sonarrEpisodeId)) \ - .join(TableShows, on=(TableHistory.sonarrSeriesId == TableShows.sonarrSeriesId)) \ - .where(reduce(operator.and_, upgradable_episodes_conditions)) \ - .group_by(TableHistory.video_path, - TableHistory.score, - TableShows.tags, - TableEpisodes.monitored, - TableShows.seriesType) \ - .dicts() - upgradable_episodes = list(upgradable_episodes) - for upgradable_episode in upgradable_episodes: - if upgradable_episode['timestamp'] > minimum_timestamp: - try: - int(upgradable_episode['score']) - except ValueError: - pass - else: - if int(upgradable_episode['score']) < 360: - upgradable_episodes_not_perfect.append(upgradable_episode) + upgradable_episodes_not_perfect = get_upgradable_episode_subtitles() + if len(upgradable_episodes_not_perfect): + upgradable_episodes_not_perfect = [{"video_path": x['video_path'], + "timestamp": x['timestamp'], + "score": x['score'], + "tags": x['tags'], + "monitored": x['monitored'], + "seriesType": x['seriesType']} + for x in upgradable_episodes_not_perfect] query_conditions = [(TableEpisodes.title.is_null(False))] if episodeid: diff --git a/bazarr/api/movies/history.py b/bazarr/api/movies/history.py index f11f604d1..b9d904bb5 100644 --- a/bazarr/api/movies/history.py +++ b/bazarr/api/movies/history.py @@ -1,17 +1,14 @@ # coding=utf-8 -import datetime import os import operator import pretty from flask_restx import Resource, Namespace, reqparse, fields from functools import reduce -from peewee import fn -from datetime import timedelta -from app.database import get_exclusion_clause, TableMovies, TableHistoryMovie, TableBlacklistMovie -from app.config import settings +from app.database import TableMovies, TableHistoryMovie, TableBlacklistMovie +from subtitles.upgrade import get_upgradable_movies_subtitles from utilities.path_mappings import path_mappings from api.swaggerui import subtitles_language_model @@ -66,45 +63,14 @@ class MoviesHistory(Resource): length = args.get('length') radarrid = args.get('radarrid') - upgradable_movies = [] - upgradable_movies_not_perfect = [] - if settings.general.getboolean('upgrade_subs'): - days_to_upgrade_subs = settings.general.days_to_upgrade_subs - minimum_timestamp = (datetime.datetime.now() - timedelta(days=int(days_to_upgrade_subs))) - - if settings.general.getboolean('upgrade_manual'): - query_actions = [1, 2, 3, 6] - else: - query_actions = [1, 3] - - upgradable_movies_conditions = [(TableHistoryMovie.action.in_(query_actions)), - (TableHistoryMovie.timestamp > minimum_timestamp), - (TableHistoryMovie.score.is_null(False))] - upgradable_movies_conditions += get_exclusion_clause('movie') - upgradable_movies = TableHistoryMovie.select(TableHistoryMovie.video_path, - fn.MAX(TableHistoryMovie.timestamp).alias('timestamp'), - TableHistoryMovie.score, - TableMovies.tags, - TableMovies.monitored) \ - .join(TableMovies, on=(TableHistoryMovie.radarrId == TableMovies.radarrId)) \ - .where(reduce(operator.and_, upgradable_movies_conditions)) \ - .group_by(TableHistoryMovie.video_path, - TableHistoryMovie.score, - TableMovies.tags, - TableMovies.monitored - ) \ - .dicts() - upgradable_movies = list(upgradable_movies) - - for upgradable_movie in upgradable_movies: - if upgradable_movie['timestamp'] > minimum_timestamp: - try: - int(upgradable_movie['score']) - except ValueError: - pass - else: - if int(upgradable_movie['score']) < 120: - upgradable_movies_not_perfect.append(upgradable_movie) + upgradable_movies_not_perfect = get_upgradable_movies_subtitles() + if len(upgradable_movies_not_perfect): + upgradable_movies_not_perfect = [{"video_path": x['video_path'], + "timestamp": x['timestamp'], + "score": x['score'], + "tags": x['tags'], + "monitored": x['monitored']} + for x in upgradable_movies_not_perfect] query_conditions = [(TableMovies.title.is_null(False))] if radarrid: diff --git a/bazarr/api/movies/movies_subtitles.py b/bazarr/api/movies/movies_subtitles.py index 0dbd43b69..8e7f2fc20 100644 --- a/bazarr/api/movies/movies_subtitles.py +++ b/bazarr/api/movies/movies_subtitles.py @@ -74,22 +74,8 @@ class MoviesSubtitles(Resource): sceneName, title, 'movie', profile_id=get_profile_id(movie_id=radarrId))) if result: result = result[0] - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = f"{result[2]}:hi" - elif forced: - language_code = f"{result[2]}:forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] - history_log_movie(1, radarrId, message, path, language_code, provider, score, subs_id, subs_path) - send_notifications_movie(radarrId, message) - store_subtitles_movie(path, moviePath) + history_log_movie(1, radarrId, result) + store_subtitles_movie(result.path, moviePath) else: event_stream(type='movie', payload=radarrId) return '', 204 @@ -152,21 +138,12 @@ class MoviesSubtitles(Resource): if not result: logging.debug(f"BAZARR unable to process subtitles for this movie: {moviePath}") else: - message = result[0] - path = result[1] - subs_path = result[2] - if hi: - language_code = f"{language}:hi" - elif forced: - language_code = f"{language}:forced" - else: - language_code = language provider = "manual" score = 120 - history_log_movie(4, radarrId, message, path, language_code, provider, score, subtitles_path=subs_path) + history_log_movie(4, radarrId, result, fake_provider=provider, fake_score=score) if not settings.general.getboolean('dont_notify_manual_actions'): - send_notifications_movie(radarrId, message) - store_subtitles_movie(path, moviePath) + send_notifications_movie(radarrId, result.message) + store_subtitles_movie(result.path, moviePath) return '', 204 # DELETE: Delete Subtitles diff --git a/bazarr/api/providers/providers_episodes.py b/bazarr/api/providers/providers_episodes.py index 0dae75e50..6d1a940f6 100644 --- a/bazarr/api/providers/providers_episodes.py +++ b/bazarr/api/providers/providers_episodes.py @@ -124,26 +124,11 @@ class ProviderEpisodes(Resource): result = manual_download_subtitle(episodePath, audio_language, hi, forced, subtitle, selected_provider, sceneName, title, 'series', use_original_format, profile_id=get_profile_id(episode_id=sonarrEpisodeId)) - if result is not None: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] - history_log(2, sonarrSeriesId, sonarrEpisodeId, message, path, language_code, provider, score, subs_id, - subs_path) + if result: + history_log(2, sonarrSeriesId, sonarrEpisodeId, result) if not settings.general.getboolean('dont_notify_manual_actions'): - send_notifications(sonarrSeriesId, sonarrEpisodeId, message) - store_subtitles(path, episodePath) - return result, 201 + send_notifications(sonarrSeriesId, sonarrEpisodeId, result.message) + store_subtitles(result.path, episodePath) except OSError: pass diff --git a/bazarr/api/providers/providers_movies.py b/bazarr/api/providers/providers_movies.py index f1fcba07a..1014933f4 100644 --- a/bazarr/api/providers/providers_movies.py +++ b/bazarr/api/providers/providers_movies.py @@ -121,23 +121,10 @@ class ProviderMovies(Resource): sceneName, title, 'movie', use_original_format, profile_id=get_profile_id(movie_id=radarrId)) if result is not None: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] - history_log_movie(2, radarrId, message, path, language_code, provider, score, subs_id, subs_path) + history_log_movie(2, radarrId, result) if not settings.general.getboolean('dont_notify_manual_actions'): - send_notifications_movie(radarrId, message) - store_subtitles_movie(path, moviePath) + send_notifications_movie(radarrId, result.message) + store_subtitles_movie(result.path, moviePath) except OSError: pass diff --git a/bazarr/radarr/history.py b/bazarr/radarr/history.py index 1f1c53a21..bc15b3de4 100644 --- a/bazarr/radarr/history.py +++ b/bazarr/radarr/history.py @@ -6,8 +6,15 @@ from app.database import TableHistoryMovie from app.event_handler import event_stream -def history_log_movie(action, radarr_id, description, video_path=None, language=None, provider=None, score=None, - subs_id=None, subtitles_path=None): +def history_log_movie(action, radarr_id, result, fake_provider=None, fake_score=None): + description = result.message + video_path = result.path + language = result.language_code + provider = fake_provider or result.provider + score = fake_score or result.score + subs_id = result.subs_id + subtitles_path = result.subs_path + TableHistoryMovie.insert({ TableHistoryMovie.action: action, TableHistoryMovie.radarrId: radarr_id, diff --git a/bazarr/sonarr/history.py b/bazarr/sonarr/history.py index 3ac09439a..af0a75c96 100644 --- a/bazarr/sonarr/history.py +++ b/bazarr/sonarr/history.py @@ -6,8 +6,15 @@ from app.database import TableHistory from app.event_handler import event_stream -def history_log(action, sonarr_series_id, sonarr_episode_id, description, video_path=None, language=None, provider=None, - score=None, subs_id=None, subtitles_path=None): +def history_log(action, sonarr_series_id, sonarr_episode_id, result, fake_provider=None, fake_score=None): + description = result.message + video_path = result.path + language = result.language_code + provider = fake_provider or result.provider + score = fake_score or result.score + subs_id = result.subs_id + subtitles_path = result.subs_path + TableHistory.insert({ TableHistory.action: action, TableHistory.sonarrSeriesId: sonarr_series_id, diff --git a/bazarr/subtitles/mass_download/movies.py b/bazarr/subtitles/mass_download/movies.py index c69be6fa5..b04e6df56 100644 --- a/bazarr/subtitles/mass_download/movies.py +++ b/bazarr/subtitles/mass_download/movies.py @@ -77,21 +77,8 @@ def movies_download_subtitles(no): check_if_still_required=True): if result: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles_movie(movie['path'], path_mappings.path_replace_movie(movie['path'])) - history_log_movie(1, no, message, path, language_code, provider, score, subs_id, subs_path) - send_notifications_movie(no, message) + history_log_movie(1, no, result) + send_notifications_movie(no, result.message) hide_progress(id='movie_search_progress_{}'.format(no)) diff --git a/bazarr/subtitles/mass_download/series.py b/bazarr/subtitles/mass_download/series.py index 6fb516bd2..e126877dd 100644 --- a/bazarr/subtitles/mass_download/series.py +++ b/bazarr/subtitles/mass_download/series.py @@ -81,23 +81,9 @@ def series_download_subtitles(no): 'series', check_if_still_required=True): if result: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles(episode['path'], path_mappings.path_replace(episode['path'])) - history_log(1, no, episode['sonarrEpisodeId'], message, path, language_code, provider, score, - subs_id, subs_path) - send_notifications(no, episode['sonarrEpisodeId'], message) + history_log(1, no, episode['sonarrEpisodeId'], result) + send_notifications(no, episode['sonarrEpisodeId'], result.message) else: logging.info("BAZARR All providers are throttled") break @@ -166,23 +152,9 @@ def episode_download_subtitles(no, send_progress=False): 'series', check_if_still_required=True): if result: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles(episode['path'], path_mappings.path_replace(episode['path'])) - history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path, - language_code, provider, score, subs_id, subs_path) - send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message) + history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], result) + send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], result.message) if send_progress: hide_progress(id='episode_search_progress_{}'.format(no)) diff --git a/bazarr/subtitles/processing.py b/bazarr/subtitles/processing.py index 9c0bfceae..c6312e66e 100644 --- a/bazarr/subtitles/processing.py +++ b/bazarr/subtitles/processing.py @@ -18,6 +18,24 @@ from .sync import sync_subtitles from .post_processing import postprocessing +class ProcessSubtitlesResult: + def __init__(self, message, reversed_path, downloaded_language_code2, downloaded_provider, score, forced, + subtitle_id, reversed_subtitles_path, hearing_impaired): + self.message = message + self.path = reversed_path + self.provider = downloaded_provider + self.score = score + self.subs_id = subtitle_id + self.subs_path = reversed_subtitles_path + + if hearing_impaired: + self.language_code = downloaded_language_code2 + ":hi" + elif forced: + self.language_code = downloaded_language_code2 + ":forced" + else: + self.language_code = downloaded_language_code2 + + def process_subtitle(subtitle, media_type, audio_language, path, max_score, is_upgrade=False, is_manual=False): use_postprocessing = settings.general.getboolean('use_postprocessing') postprocessing_cmd = settings.general.postprocessing_cmd @@ -116,5 +134,12 @@ def process_subtitle(subtitle, media_type, audio_language, path, max_score, is_u track_event(category=downloaded_provider, action=action, label=downloaded_language) - return message, reversed_path, downloaded_language_code2, downloaded_provider, subtitle.score, \ - subtitle.language.forced, subtitle.id, reversed_subtitles_path, subtitle.language.hi + return ProcessSubtitlesResult(message=message, + reversed_path=reversed_path, + downloaded_language_code2=downloaded_language_code2, + downloaded_provider=downloaded_provider, + score=subtitle.score, + forced=subtitle.language.forced, + subtitle_id=subtitle.id, + reversed_subtitles_path=reversed_subtitles_path, + hearing_impaired=subtitle.language.hi) diff --git a/bazarr/subtitles/upgrade.py b/bazarr/subtitles/upgrade.py index d2337033a..e292f1621 100644 --- a/bazarr/subtitles/upgrade.py +++ b/bazarr/subtitles/upgrade.py @@ -13,7 +13,6 @@ from app.database import get_exclusion_clause, get_audio_profile_languages, Tabl from app.event_handler import show_progress, hide_progress from app.get_providers import get_providers from app.notifier import send_notifications, send_notifications_movie -from peewee import fn from radarr.history import history_log_movie from sonarr.history import history_log from subtitles.indexer.movies import store_subtitles_movie @@ -23,153 +22,13 @@ from .download import generate_subtitles def upgrade_subtitles(): - days_to_upgrade_subs = settings.general.days_to_upgrade_subs - minimum_timestamp = (datetime.now() - timedelta(days=int(days_to_upgrade_subs))) - - if settings.general.getboolean('upgrade_manual'): - query_actions = [1, 2, 3, 4, 6] - else: - query_actions = [1, 3] - - if settings.general.getboolean('use_sonarr'): - upgradable_episodes_conditions = [(TableHistory.action << query_actions), - (TableHistory.timestamp > minimum_timestamp), - (TableHistory.score.is_null(False))] - upgradable_episodes_conditions += get_exclusion_clause('series') - upgradable_episodes = TableHistory.select(TableHistory.video_path, - TableHistory.language, - TableHistory.score, - TableShows.tags, - TableShows.profileId, - TableEpisodes.audio_language, - TableEpisodes.sceneName, - TableEpisodes.title, - TableEpisodes.sonarrSeriesId, - TableHistory.action, - TableHistory.subtitles_path, - TableEpisodes.sonarrEpisodeId, - fn.MAX(TableHistory.timestamp).alias('timestamp'), - TableEpisodes.monitored, - TableEpisodes.season, - TableEpisodes.episode, - TableShows.title.alias('seriesTitle'), - TableShows.seriesType) \ - .join(TableShows, on=(TableHistory.sonarrSeriesId == TableShows.sonarrSeriesId)) \ - .join(TableEpisodes, on=(TableHistory.sonarrEpisodeId == TableEpisodes.sonarrEpisodeId)) \ - .where(reduce(operator.and_, upgradable_episodes_conditions)) \ - .group_by(TableHistory.video_path, - TableHistory.language, - TableHistory.score, - TableShows.tags, - TableShows.profileId, - TableEpisodes.audio_language, - TableEpisodes.sceneName, - TableEpisodes.title, - TableEpisodes.sonarrSeriesId, - TableHistory.action, - TableHistory.subtitles_path, - TableEpisodes.sonarrEpisodeId, - TableEpisodes.monitored, - TableEpisodes.season, - TableEpisodes.episode, - TableShows.title.alias('seriesTitle'), - TableShows.seriesType) \ - .dicts() - upgradable_episodes_not_perfect = [] - for upgradable_episode in upgradable_episodes: - if upgradable_episode['timestamp'] > minimum_timestamp: - try: - int(upgradable_episode['score']) - except ValueError: - pass - else: - if int(upgradable_episode['score']) < 360 or (settings.general.getboolean('upgrade_manual') and - upgradable_episode['action'] in [2, 4, 6]): - #check episode is already in list and replace it is episode have better score - if upgradable_episode['video_path'] in [x['video_path'] for x in upgradable_episodes_not_perfect]: - for item in upgradable_episodes_not_perfect: - if item['video_path'] == upgradable_episode['video_path'] and \ - int(item['score']) <= int(upgradable_episode['score']): - upgradable_episodes_not_perfect.remove(item) - upgradable_episodes_not_perfect.append(upgradable_episode) - else: - upgradable_episodes_not_perfect.append(upgradable_episode) - - episodes_to_upgrade = [] - for episode in upgradable_episodes_not_perfect: - if os.path.exists(path_mappings.path_replace(episode['subtitles_path'])) and \ - os.path.exists(path_mappings.path_replace(episode['video_path'])) and \ - int(episode['score']) < 357: - episodes_to_upgrade.append(episode) + use_sonarr = settings.general.getboolean('use_sonarr') + use_radarr = settings.general.getboolean('use_radarr') + if use_sonarr: + episodes_to_upgrade = get_upgradable_episode_subtitles() count_episode_to_upgrade = len(episodes_to_upgrade) - if settings.general.getboolean('use_radarr'): - upgradable_movies_conditions = [(TableHistoryMovie.action << query_actions), - (TableHistoryMovie.timestamp > minimum_timestamp), - (TableHistoryMovie.score.is_null(False))] - upgradable_movies_conditions += get_exclusion_clause('movie') - upgradable_movies = TableHistoryMovie.select(TableHistoryMovie.video_path, - TableHistoryMovie.language, - TableHistoryMovie.score, - TableMovies.profileId, - TableHistoryMovie.action, - TableHistoryMovie.subtitles_path, - TableMovies.audio_language, - TableMovies.sceneName, - fn.MAX(TableHistoryMovie.timestamp).alias('timestamp'), - TableMovies.monitored, - TableMovies.tags, - TableMovies.radarrId, - TableMovies.title) \ - .join(TableMovies, on=(TableHistoryMovie.radarrId == TableMovies.radarrId)) \ - .where(reduce(operator.and_, upgradable_movies_conditions)) \ - .group_by(TableHistoryMovie.video_path, - TableHistoryMovie.language, - TableHistoryMovie.score, - TableMovies.profileId, - TableHistoryMovie.action, - TableHistoryMovie.subtitles_path, - TableMovies.audio_language, - TableMovies.sceneName, - TableMovies.monitored, - TableMovies.tags, - TableMovies.radarrId, - TableMovies.title - ) \ - .dicts() - upgradable_movies_not_perfect = [] - for upgradable_movie in upgradable_movies: - if upgradable_movie['timestamp'] > minimum_timestamp: - try: - int(upgradable_movie['score']) - except ValueError: - pass - else: - if int(upgradable_movie['score']) < 120 or (settings.general.getboolean('upgrade_manual') and - upgradable_movie['action'] in [2, 4, 6]): - # check episode is already in list and replace it is episode have better score - if upgradable_movie['video_path'] in [x['video_path'] for x in - upgradable_movies_not_perfect]: - for item in upgradable_movies_not_perfect: - if item['video_path'] == upgradable_movie['video_path'] and \ - int(item['score']) <= int(upgradable_movie['score']): - upgradable_movies_not_perfect.remove(item) - upgradable_movies_not_perfect.append(upgradable_movie) - else: - upgradable_movies_not_perfect.append(upgradable_movie) - upgradable_movies_not_perfect.append(upgradable_movie) - - movies_to_upgrade = [] - for movie in upgradable_movies_not_perfect: - if os.path.exists(path_mappings.path_replace_movie(movie['subtitles_path'])) and \ - os.path.exists(path_mappings.path_replace_movie(movie['video_path'])) and \ - int(movie['score']) < 117: - movies_to_upgrade.append(movie) - - count_movie_to_upgrade = len(movies_to_upgrade) - - if settings.general.getboolean('use_sonarr'): for i, episode in enumerate(episodes_to_upgrade): providers_list = get_providers() @@ -185,18 +44,8 @@ def upgrade_subtitles(): if not providers_list: logging.info("BAZARR All providers are throttled") return - if episode['language'].endswith('forced'): - language = episode['language'].split(':')[0] - is_forced = "True" - is_hi = "False" - elif episode['language'].endswith('hi'): - language = episode['language'].split(':')[0] - is_forced = "False" - is_hi = "True" - else: - language = episode['language'].split(':')[0] - is_forced = "False" - is_hi = "False" + + language, is_forced, is_hi = parse_language_string(episode['language']) audio_language_list = get_audio_profile_languages(episode['audio_language']) if len(audio_language_list) > 0: @@ -215,27 +64,16 @@ def upgrade_subtitles(): if result: result = result[0] - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles(episode['video_path'], path_mappings.path_replace(episode['video_path'])) - history_log(3, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path, - language_code, provider, score, subs_id, subs_path) - send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message) + history_log(3, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], result) + send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], result.message) hide_progress(id='upgrade_episodes_progress') - if settings.general.getboolean('use_radarr'): + if use_radarr: + movies_to_upgrade = get_upgradable_movies_subtitles() + count_movie_to_upgrade = len(movies_to_upgrade) + for i, movie in enumerate(movies_to_upgrade): providers_list = get_providers() @@ -248,18 +86,8 @@ def upgrade_subtitles(): if not providers_list: logging.info("BAZARR All providers are throttled") return - if movie['language'].endswith('forced'): - language = movie['language'].split(':')[0] - is_forced = "True" - is_hi = "False" - elif movie['language'].endswith('hi'): - language = movie['language'].split(':')[0] - is_forced = "False" - is_hi = "True" - else: - language = movie['language'].split(':')[0] - is_forced = "False" - is_hi = "False" + + language, is_forced, is_hi = parse_language_string(movie['language']) audio_language_list = get_audio_profile_languages(movie['audio_language']) if len(audio_language_list) > 0: @@ -277,25 +105,152 @@ def upgrade_subtitles(): is_upgrade=True)) if result: result = result[0] - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles_movie(movie['video_path'], path_mappings.path_replace_movie(movie['video_path'])) - history_log_movie(3, movie['radarrId'], message, path, language_code, provider, score, subs_id, - subs_path) - send_notifications_movie(movie['radarrId'], message) + history_log_movie(3, movie['radarrId'], result) + send_notifications_movie(movie['radarrId'], result.message) hide_progress(id='upgrade_movies_progress') logging.info('BAZARR Finished searching for Subtitles to upgrade. Check History for more information.') + + +def get_queries_condition_parameters(): + days_to_upgrade_subs = settings.general.days_to_upgrade_subs + minimum_timestamp = (datetime.now() - timedelta(days=int(days_to_upgrade_subs))) + + if settings.general.getboolean('upgrade_manual'): + query_actions = [1, 2, 3, 4, 6] + else: + query_actions = [1, 3] + + return [minimum_timestamp, query_actions] + + +def parse_upgradable_list(upgradable_list, perfect_score, media_type): + if media_type == 'series': + path_replace_method = path_mappings.path_replace + else: + path_replace_method = path_mappings.path_replace_movie + + items_to_upgrade = [] + + for item in upgradable_list: + logging.debug(f"Trying to validate eligibility to upgrade for this subtitles: " + f"{item['subtitles_path']}") + if (item['video_path'], item['language']) in \ + [(x['video_path'], x['language']) for x in items_to_upgrade]: + logging.debug("Newer video path and subtitles language combination already in list of subtitles to " + "upgrade, we skip this one.") + continue + + if os.path.exists(path_replace_method(item['subtitles_path'])) and \ + os.path.exists(path_replace_method(item['video_path'])): + logging.debug("Video and subtitles file are still there, we continue the eligibility validation.") + pass + + items_to_upgrade.append(item) + + if not settings.general.getboolean('upgrade_manual'): + logging.debug("Removing history items for manually downloaded or translated subtitles.") + items_to_upgrade = [x for x in items_to_upgrade if x['action'] in [2, 4, 6]] + + logging.debug("Removing history items for already perfectly scored subtitles.") + items_to_upgrade = [x for x in items_to_upgrade if x['score'] < perfect_score] + + logging.debug(f"Bazarr will try to upgrade {len(items_to_upgrade)} subtitles.") + + return items_to_upgrade + + +def parse_language_string(language_string): + if language_string.endswith('forced'): + language = language_string.split(':')[0] + is_forced = "True" + is_hi = "False" + elif language_string.endswith('hi'): + language = language_string.split(':')[0] + is_forced = "False" + is_hi = "True" + else: + language = language_string.split(':')[0] + is_forced = "False" + is_hi = "False" + + return [language, is_forced, is_hi] + + +def get_upgradable_episode_subtitles(): + minimum_timestamp, query_actions = get_queries_condition_parameters() + + upgradable_episodes_conditions = [(TableHistory.action << query_actions), + (TableHistory.timestamp > minimum_timestamp), + (TableHistory.score.is_null(False))] + upgradable_episodes_conditions += get_exclusion_clause('series') + upgradable_episodes = TableHistory.select(TableHistory.video_path, + TableHistory.language, + TableHistory.score, + TableShows.tags, + TableShows.profileId, + TableEpisodes.audio_language, + TableEpisodes.sceneName, + TableEpisodes.title, + TableEpisodes.sonarrSeriesId, + TableHistory.action, + TableHistory.subtitles_path, + TableEpisodes.sonarrEpisodeId, + TableHistory.timestamp.alias('timestamp'), + TableEpisodes.monitored, + TableEpisodes.season, + TableEpisodes.episode, + TableShows.title.alias('seriesTitle'), + TableShows.seriesType) \ + .join(TableShows, on=(TableHistory.sonarrSeriesId == TableShows.sonarrSeriesId)) \ + .join(TableEpisodes, on=(TableHistory.sonarrEpisodeId == TableEpisodes.sonarrEpisodeId)) \ + .where(reduce(operator.and_, upgradable_episodes_conditions)) \ + .order_by(TableHistory.timestamp.desc()) \ + .dicts() + + if not upgradable_episodes: + return [] + else: + upgradable_episodes = list(upgradable_episodes) + logging.debug(f"{len(upgradable_episodes)} potentially upgradable episode subtitles have been found, let's " + f"filter them...") + + return parse_upgradable_list(upgradable_list=upgradable_episodes, perfect_score=357, media_type='series') + + +def get_upgradable_movies_subtitles(): + minimum_timestamp, query_actions = get_queries_condition_parameters() + + upgradable_movies_conditions = [(TableHistoryMovie.action << query_actions), + (TableHistoryMovie.timestamp > minimum_timestamp), + (TableHistoryMovie.score.is_null(False))] + upgradable_movies_conditions += get_exclusion_clause('movie') + upgradable_movies = TableHistoryMovie.select(TableHistoryMovie.video_path, + TableHistoryMovie.language, + TableHistoryMovie.score, + TableMovies.profileId, + TableHistoryMovie.action, + TableHistoryMovie.subtitles_path, + TableMovies.audio_language, + TableMovies.sceneName, + TableHistoryMovie.timestamp.alias('timestamp'), + TableMovies.monitored, + TableMovies.tags, + TableMovies.radarrId, + TableMovies.title) \ + .join(TableMovies, on=(TableHistoryMovie.radarrId == TableMovies.radarrId)) \ + .where(reduce(operator.and_, upgradable_movies_conditions)) \ + .order_by(TableHistoryMovie.timestamp.desc()) \ + .dicts() + + if not upgradable_movies: + return [] + else: + upgradable_movies = list(upgradable_movies) + logging.debug(f"{len(upgradable_movies)} potentially upgradable movie subtitles have been found, let's filter " + f"them...") + + return parse_upgradable_list(upgradable_list=upgradable_movies, perfect_score=117, media_type='movie') diff --git a/bazarr/subtitles/wanted/movies.py b/bazarr/subtitles/wanted/movies.py index 9d7a85f06..f5ccd61df 100644 --- a/bazarr/subtitles/wanted/movies.py +++ b/bazarr/subtitles/wanted/movies.py @@ -53,24 +53,10 @@ def _wanted_movie(movie): check_if_still_required=True): if result: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = result[2] + ":hi" - elif forced: - language_code = result[2] + ":forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles_movie(movie['path'], path_mappings.path_replace_movie(movie['path'])) - history_log_movie(1, movie['radarrId'], message, path, language_code, provider, score, - subs_id, subs_path) + history_log_movie(1, movie['radarrId'], result) event_stream(type='movie-wanted', action='delete', payload=movie['radarrId']) - send_notifications_movie(movie['radarrId'], message) + send_notifications_movie(movie['radarrId'], result.message) def wanted_download_subtitles_movie(radarr_id): diff --git a/bazarr/subtitles/wanted/series.py b/bazarr/subtitles/wanted/series.py index 5f809d4f6..6725b128a 100644 --- a/bazarr/subtitles/wanted/series.py +++ b/bazarr/subtitles/wanted/series.py @@ -52,25 +52,11 @@ def _wanted_episode(episode): 'series', check_if_still_required=True): if result: - message = result[0] - path = result[1] - forced = result[5] - if result[8]: - language_code = f"{result[2]}:hi" - elif forced: - language_code = f"{result[2]}:forced" - else: - language_code = result[2] - provider = result[3] - score = result[4] - subs_id = result[6] - subs_path = result[7] store_subtitles(episode['path'], path_mappings.path_replace(episode['path'])) - history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message, path, - language_code, provider, score, subs_id, subs_path) + history_log(1, episode['sonarrSeriesId'], episode['sonarrEpisodeId'], result) event_stream(type='series', action='update', payload=episode['sonarrSeriesId']) event_stream(type='episode-wanted', action='delete', payload=episode['sonarrEpisodeId']) - send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], message) + send_notifications(episode['sonarrSeriesId'], episode['sonarrEpisodeId'], result.message) def wanted_download_subtitles(sonarr_episode_id): |