no log: added failsafe to encoding detection

pull/2182/head v1.2.2-beta.25
morpheus65535 1 year ago
parent edfbb1a5ca
commit f371d0585b

@ -76,25 +76,26 @@ def guess_external_subtitles(dest_folder, subtitles, media_type, previously_inde
with open(subtitle_path, 'rb') as f: with open(subtitle_path, 'rb') as f:
text = f.read() text = f.read()
encoding = detect(text)
if encoding and 'encoding' in encoding:
encoding = detect(text)['encoding'] encoding = detect(text)['encoding']
if not encoding: else:
logging.debug("BAZARR skipping this subtitles because we can't guess the encoding. " logging.debug("BAZARR skipping this subtitles because we can't guess the encoding. "
"It's probably a binary file: " + subtitle_path) "It's probably a binary file: " + subtitle_path)
continue continue
if 'UTF' in encoding: text = text.decode(encoding)
text = text.decode('utf-8')
detected_language = guess_language(text) detected_language = guess_language(text)
# add simplified and traditional chinese detection # add simplified and traditional chinese detection
if detected_language == 'zh': if detected_language == 'zh':
traditional_chinese_fuzzy = [u"", u"雙語"] traditional_chinese_fuzzy = [u"", u"雙語"]
traditional_chinese = [".cht", ".tc", ".zh-tw", ".zht", ".zh-hant", ".zhhant", ".zh_hant", traditional_chinese = [".cht", ".tc", ".zh-tw", ".zht", ".zh-hant", ".zhhant", ".zh_hant",
".hant", ".big5", ".traditional"] ".hant", ".big5", ".traditional"]
if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(traditional_chinese)) or (str(subtitle_path).lower())[:-5] in traditional_chinese_fuzzy: if str(os.path.splitext(subtitle)[0]).lower().endswith(tuple(traditional_chinese)) or \
detected_language == 'zt' (str(subtitle_path).lower())[:-5] in traditional_chinese_fuzzy:
else: detected_language = 'zt'
text = text.decode(encoding)
detected_language = guess_language(text)
if detected_language: if detected_language:
logging.debug("BAZARR external subtitles detected and guessed this language: " + str( logging.debug("BAZARR external subtitles detected and guessed this language: " + str(
detected_language)) detected_language))
@ -127,14 +128,13 @@ def guess_external_subtitles(dest_folder, subtitles, media_type, previously_inde
with open(subtitle_path, 'rb') as f: with open(subtitle_path, 'rb') as f:
text = f.read() text = f.read()
encoding = detect(text)
if encoding and 'encoding' in encoding:
encoding = detect(text)['encoding'] encoding = detect(text)['encoding']
if not encoding: else:
logging.debug("BAZARR skipping this subtitles because we can't guess the encoding. " logging.debug("BAZARR skipping this subtitles because we can't guess the encoding. "
"It's probably a binary file: " + subtitle_path) "It's probably a binary file: " + subtitle_path)
continue continue
if 'UTF' in encoding:
text = text.decode('utf-8')
else:
text = text.decode(encoding) text = text.decode(encoding)
if bool(re.search(hi_regex, text)): if bool(re.search(hi_regex, text)):

Loading…
Cancel
Save