From 80f066064370cc78770cf04f82e8bf9b32dee0f4 Mon Sep 17 00:00:00 2001 From: morpheus65535 <5130500+morpheus65535@users.noreply.github.com> Date: Mon, 18 Jun 2018 19:37:25 -0400 Subject: [PATCH] Fix to ignore subtitles files with an unsupported encoding #66 --- list_subtitles.py | 51 +++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/list_subtitles.py b/list_subtitles.py index d9d77af66..077766fe0 100644 --- a/list_subtitles.py +++ b/list_subtitles.py @@ -32,32 +32,35 @@ def store_subtitles(file): pass - subtitles = core.search_external_subtitles(file) - - for subtitle, language in subtitles.iteritems(): - if str(language) != 'und': - actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - else: - with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: - text = list(islice(f, 100)) - text = ' '.join(text) - encoding = UnicodeDammit(text) - try: - text = text.decode(encoding.original_encoding) - except Exception as e: - logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.') - else: - detected_language = langdetect.detect(text) - if len(detected_language) > 0: - actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - - conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) - c_db = conn_db.cursor() + try: + subtitles = core.search_external_subtitles(file) + except: + pass + else: + for subtitle, language in subtitles.iteritems(): + if str(language) != 'und': + actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) + else: + with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: + text = list(islice(f, 100)) + text = ' '.join(text) + encoding = UnicodeDammit(text) + try: + text = text.decode(encoding.original_encoding) + except Exception as e: + logging.exception('Error trying to detect character encoding for this subtitles file: ' + path_replace(os.path.join(os.path.dirname(file), subtitle)) + ' You should try to delete this subtitles file manually and ask Bazarr to download it again.') + else: + detected_language = langdetect.detect(text) + if len(detected_language) > 0: + actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) - c_db.execute("UPDATE table_episodes SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse(file))) - conn_db.commit() + conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) + c_db = conn_db.cursor() - c_db.close() + c_db.execute("UPDATE table_episodes SET subtitles = ? WHERE path = ?", (str(actual_subtitles), path_replace_reverse(file))) + conn_db.commit() + + c_db.close() return actual_subtitles