Fix to ignore subtitles files with an unsupported encoding #66

pull/115/merge
morpheus65535 6 years ago
parent 3251dc6466
commit 80f0660643

@ -32,32 +32,35 @@ def store_subtitles(file):
pass pass
subtitles = core.search_external_subtitles(file) try:
subtitles = core.search_external_subtitles(file)
for subtitle, language in subtitles.iteritems(): except:
if str(language) != 'und': pass
actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) else:
else: for subtitle, language in subtitles.iteritems():
with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f: if str(language) != 'und':
text = list(islice(f, 100)) actual_subtitles.append([str(language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))])
text = ' '.join(text) else:
encoding = UnicodeDammit(text) with open(path_replace(os.path.join(os.path.dirname(file), subtitle)), 'r') as f:
try: text = list(islice(f, 100))
text = text.decode(encoding.original_encoding) text = ' '.join(text)
except Exception as e: encoding = UnicodeDammit(text)
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.') try:
else: text = text.decode(encoding.original_encoding)
detected_language = langdetect.detect(text) except Exception as e:
if len(detected_language) > 0: 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.')
actual_subtitles.append([str(detected_language), path_replace_reverse(os.path.join(os.path.dirname(file), subtitle))]) else:
detected_language = langdetect.detect(text)
conn_db = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30) if len(detected_language) > 0:
c_db = conn_db.cursor() 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 = sqlite3.connect(os.path.join(os.path.dirname(__file__), 'data/db/bazarr.db'), timeout=30)
conn_db.commit() 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 return actual_subtitles

Loading…
Cancel
Save