diff --git a/bazarr/api.py b/bazarr/api.py index e0fff003d..1a4838602 100644 --- a/bazarr/api.py +++ b/bazarr/api.py @@ -30,7 +30,7 @@ from list_subtitles import store_subtitles, store_subtitles_movie, series_scan_s list_missing_subtitles, list_missing_subtitles_movies from utils import history_log, history_log_movie, blacklist_log, blacklist_delete, blacklist_delete_all, \ blacklist_log_movie, blacklist_delete_movie, blacklist_delete_all_movie, get_sonarr_version, get_radarr_version, \ - delete_subtitles + delete_subtitles, subtitles_apply_mods from get_providers import get_providers, get_providers_auth, list_throttled_providers, reset_throttled_providers from event_handler import event_stream from scheduler import scheduler @@ -1748,6 +1748,18 @@ class SyncSubtitles(Resource): return '', 200 +class SubMods(Resource): + @authenticate + def post(self): + language = request.form.get('language') + subtitles_path = request.form.get('subtitlesPath') + mod = request.form.get('mod') + + subtitles_apply_mods(language, subtitles_path, [mod]) + + return '', 200 + + class BrowseBazarrFS(Resource): @authenticate def get(self): @@ -1844,6 +1856,7 @@ api.add_resource(BlacklistMovieSubtitlesRemove, '/blacklist_movie_subtitles_remo api.add_resource(BlacklistMovieSubtitlesRemoveAll, '/blacklist_movie_subtitles_remove_all') api.add_resource(SyncSubtitles, '/sync_subtitles') +api.add_resource(SubMods, '/sub_mods') api.add_resource(BrowseBazarrFS, '/browse_bazarr_filesystem') api.add_resource(BrowseSonarrFS, '/browse_sonarr_filesystem') diff --git a/bazarr/utils.py b/bazarr/utils.py index 7c4db5fe0..97f5a2cbc 100644 --- a/bazarr/utils.py +++ b/bazarr/utils.py @@ -11,10 +11,11 @@ from get_args import args from config import settings, url_sonarr, url_radarr from database import database from event_handler import event_stream -from get_languages import alpha2_from_alpha3, language_from_alpha3 +from get_languages import alpha2_from_alpha3, language_from_alpha3, alpha3_from_alpha2 from helper import path_mappings from list_subtitles import store_subtitles, store_subtitles_movie - +from subliminal_patch.subtitle import Subtitle +from subzero.language import Language from subliminal import region as subliminal_cache_region import datetime import glob @@ -250,3 +251,29 @@ def delete_subtitles(media_type, language, forced, media_path, subtitles_path, s store_subtitles_movie(path_mappings.path_replace_reverse_movie(media_path), media_path) notify_radarr(radarr_id) return True + + +def subtitles_apply_mods(language, subtitle_path, mods): + + if language == 'pob': + lang_obj = Language('por', 'BR') + else: + lang_obj = Language(language) + + sub = Subtitle(lang_obj, mods=mods) + with open(subtitle_path, 'rb') as f: + sub.content = f.read() + + if not sub.is_valid(): + logging.exception('BAZARR Invalid subtitle file: ' + subtitle_path) + return + + content = sub.get_modified_content() + if content: + if os.path.exists(subtitle_path): + os.remove(subtitle_path) + + with open(subtitle_path, 'wb') as f: + f.write(content) + + diff --git a/views/episodes.html b/views/episodes.html index 3aea12760..091c7b477 100644 --- a/views/episodes.html +++ b/views/episodes.html @@ -331,7 +331,7 @@