diff --git a/bazarr/helper.py b/bazarr/helper.py index 699db5811..e307e8f9a 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -5,11 +5,13 @@ import os import re import types import logging +import operator import libs import chardet from bs4 import UnicodeDammit +from collections import OrderedDict from config import settings from utils import get_sonarr_platform, get_radarr_platform @@ -20,8 +22,9 @@ def sonarr_path_mapping_regex(): global sonarr_regex global sonarr_use_path_mapping - path_mapping_temp = dict(ast.literal_eval(settings.general.path_mappings)) - path_mapping = {k: v for k, v in path_mapping_temp.items() if k != ''} + path_mapping = ast.literal_eval(settings.general.path_mappings) + path_mapping = sorted(path_mapping, key=operator.itemgetter(0), reverse=True) + path_mapping = OrderedDict((mapping[0], mapping[1]) for mapping in path_mapping if mapping[0] != '') if any(item for sublist in path_mapping for item in sublist): sonarr_use_path_mapping = True sonarr_regex = re.compile("|".join(path_mapping.keys())) @@ -33,12 +36,18 @@ def sonarr_path_mapping_reverse_regex(): global sonarr_platform global path_mapping_reverse global sonarr_reverse_regex + global sonarr_use_path_mapping sonarr_platform = get_sonarr_platform() - path_mapping_reverse_temp = ast.literal_eval(settings.general.path_mappings) - path_mapping_reverse = dict([sublist[::-1] for sublist in path_mapping_reverse_temp]) - sonarr_reverse_regex = re.compile("|".join(map(re.escape, path_mapping_reverse.keys()))) + path_mapping_reverse = ast.literal_eval(settings.general.path_mappings) + path_mapping_reverse = sorted(path_mapping_reverse, key=operator.itemgetter(0), reverse=True) + path_mapping_reverse = OrderedDict((mapping[1], mapping[0]) for mapping in path_mapping_reverse if mapping[0] != '') + if any(item for sublist in path_mapping_reverse for item in sublist): + sonarr_use_path_mapping = True + sonarr_reverse_regex = re.compile("|".join(map(re.escape, path_mapping_reverse.keys()))) + else: + sonarr_use_path_mapping = False def radarr_path_mapping_regex(): @@ -73,8 +82,8 @@ def path_replace(path): def path_replace_reverse(path): - if path is None: - return None + if path is None or sonarr_use_path_mapping is False: + return path reverted_path_temp = sonarr_reverse_regex.sub(lambda match: path_mapping_reverse[match.group(0)], path, count=1) diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index df2c0af01..e75d85096 100644 --- a/bazarr/list_subtitles.py +++ b/bazarr/list_subtitles.py @@ -93,7 +93,7 @@ def store_subtitles(file): text = ' '.join(text) encoding = UnicodeDammit(text) try: - text = text.encode().decode(encoding.original_encoding) + text = text.decode(encoding.original_encoding) detected_language = langdetect.detect(text) except Exception as e: logging.exception( @@ -190,7 +190,7 @@ def store_subtitles_movie(file): text = ' '.join(text) encoding = UnicodeDammit(text) try: - text = text.encode().decode(encoding.original_encoding) + text = text.decode(encoding.original_encoding) detected_language = langdetect.detect(text) except Exception as e: logging.exception(