From 8b3821b43bd75a03c9ebb9789965ba161b0907ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Sun, 3 Nov 2019 21:48:21 -0500 Subject: [PATCH] Fix for #655. --- bazarr/list_subtitles.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index a1b5e983a..21bf7925e 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -11,6 +11,7 @@ import subliminal_patch import operator from subliminal import core from subliminal_patch import search_external_subtitles +from subzero.language import Language from bs4 import UnicodeDammit from itertools import islice @@ -62,6 +63,7 @@ def store_subtitles(file): subliminal_patch.core.CUSTOM_PATHS = [dest_folder] if dest_folder else [] subtitles = search_external_subtitles(file, languages=get_language_set(), only_one=settings.general.getboolean('single_language')) + subtitles = guess_external_subtitles(get_subtitle_destination_folder() or os.path.dirname(file), subtitles) except Exception as e: logging.exception("BAZARR unable to index external subtitles.") pass @@ -151,13 +153,13 @@ def store_subtitles_movie(file): "BAZARR error when trying to analyze this %s file: %s" % (os.path.splitext(file)[1], file)) pass - dest_folder = get_subtitle_destination_folder() or '' - subliminal_patch.core.CUSTOM_PATHS = [dest_folder] if dest_folder else [] brazilian_portuguese = [".pt-br", ".pob", "pb"] brazilian_portuguese_forced = [".pt-br.forced", ".pob.forced", "pb.forced"] try: - subtitles = search_external_subtitles(file, languages=get_language_set(), - only_one=settings.general.getboolean('single_language')) + dest_folder = get_subtitle_destination_folder() or '' + subliminal_patch.core.CUSTOM_PATHS = [dest_folder] if dest_folder else [] + subtitles = search_external_subtitles(file, languages=get_language_set()) + subtitles = guess_external_subtitles(get_subtitle_destination_folder() or os.path.dirname(file), subtitles) except Exception as e: logging.exception("BAZARR unable to index external subtitles.") pass @@ -396,3 +398,31 @@ def get_external_subtitles_path(file, subtitle): path = None return path + + +def guess_external_subtitles(dest_folder, subtitles): + for subtitle, language in subtitles.iteritems(): + if not language: + subtitle_path = os.path.join(dest_folder, subtitle) + if os.path.exists(subtitle_path) and os.path.splitext(subtitle_path)[1] in core.SUBTITLE_EXTENSIONS: + logging.debug("BAZARR falling back to file content analysis to detect language.") + detected_language = None + with open(subtitle_path, 'r') as f: + text = ' '.join(list(islice(f, 100))) + try: + encoding = UnicodeDammit(text) + text = text.decode(encoding.original_encoding) + detected_language = langdetect.detect(text) + except Exception as e: + logging.exception('BAZARR Error trying to detect language for this subtitles file: ' + + subtitle_path + ' You should try to delete this subtitles file manually and ask ' + 'Bazarr to download it again.') + else: + if detected_language: + logging.debug("BAZARR external subtitles detected and guessed this language: " + str( + detected_language)) + try: + subtitles[subtitle] = Language.rebuild(Language.fromietf(detected_language)) + except: + pass + return subtitles