From 58024d1fed992d85576119da438211bd3b5eff33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Wed, 9 Oct 2019 00:08:39 -0400 Subject: [PATCH] Continuing development. --- bazarr/database.py | 4 +- bazarr/helper.py | 116 +++++++++++++++++++++++++++------------ bazarr/list_subtitles.py | 4 +- bazarr/utils.py | 34 ++++++++++++ 4 files changed, 118 insertions(+), 40 deletions(-) diff --git a/bazarr/database.py b/bazarr/database.py index 338bec489..6d3547b75 100644 --- a/bazarr/database.py +++ b/bazarr/database.py @@ -7,8 +7,6 @@ from peewee import * from playhouse.sqliteq import SqliteQueueDatabase from playhouse.migrate import * -from helper import path_replace, path_replace_movie, path_replace_reverse, path_replace_reverse_movie - database = SqliteQueueDatabase( None, use_gevent=False, @@ -21,11 +19,13 @@ migrator = SqliteMigrator(database) @database.func('path_substitution') def path_substitution(path): + from helper import path_replace return path_replace(path) @database.func('path_substitution_movie') def path_substitution_movie(path): + from helper import path_replace_movie return path_replace_movie(path) diff --git a/bazarr/helper.py b/bazarr/helper.py index 6cd409b5a..2e5ddb6d2 100644 --- a/bazarr/helper.py +++ b/bazarr/helper.py @@ -5,70 +5,108 @@ import os import re import types import logging + +import libs + import chardet from bs4 import UnicodeDammit from config import settings +from utils import get_sonarr_platform, get_radarr_platform + + +def sonarr_path_mapping_regex(): + global path_mapping + global sonarr_regex + + path_mapping = dict(ast.literal_eval(settings.general.path_mappings)) + sonarr_regex = re.compile("|".join(map(re.escape, path_mapping.keys()))) + + +def sonarr_path_mapping_reverse_regex(): + global sonarr_platform + global path_mapping_reverse + global sonarr_reverse_regex + + 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()))) + + +def radarr_path_mapping_regex(): + global path_mapping_movie + global radarr_regex + + path_mapping_movie = dict(ast.literal_eval(settings.general.path_mappings_movie)) + radarr_regex = re.compile("|".join(map(re.escape, path_mapping_movie.keys()))) + + +def radarr_path_mapping_reverse_regex(): + global radarr_platform + global path_mapping_reverse_movie + global radarr_reverse_regex + + radarr_platform = get_radarr_platform() + + path_mapping_reverse_movie_temp = ast.literal_eval(settings.general.path_mappings_movie) + path_mapping_reverse_movie = dict([sublist[::-1] for sublist in path_mapping_reverse_movie_temp]) + radarr_reverse_regex = re.compile("|".join(map(re.escape, path_mapping_reverse_movie.keys()))) def path_replace(path): if path is None: return None - for path_mapping in ast.literal_eval(settings.general.path_mappings): - if path_mapping[0] in path: - path = path.replace(path_mapping[0], path_mapping[1]) - if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): - path = path.replace('/', '\\') - elif path.startswith('/'): - path = path.replace('\\', '/') - break - return path + reverted_path = sonarr_regex.sub(lambda match: path_mapping[match.group(0)], path, count=1) + + from os.path import normpath + + return normpath(reverted_path) def path_replace_reverse(path): if path is None: return None - for path_mapping in ast.literal_eval(settings.general.path_mappings): - if path_mapping[1] in path: - path = path.replace(path_mapping[1], path_mapping[0]) - if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): - path = path.replace('/', '\\') - elif path.startswith('/'): - path = path.replace('\\', '/') - break - return path + reverted_path_temp = sonarr_reverse_regex.sub(lambda match: path_mapping_reverse[match.group(0)], path, count=1) + + if sonarr_platform == 'posix': + from posixpath import normpath + reverted_path = reverted_path_temp.replace('\\', '/') + elif sonarr_platform == 'nt': + from ntpath import normpath + reverted_path = reverted_path_temp.replace('/', '\\') + + return normpath(reverted_path) def path_replace_movie(path): if path is None: return None - for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): - if path_mapping[0] in path: - path = path.replace(path_mapping[0], path_mapping[1]) - if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): - path = path.replace('/', '\\') - elif path.startswith('/'): - path = path.replace('\\', '/') - break - return path + reverted_path = radarr_regex.sub(lambda match: path_mapping_movie[match.group(0)], path, count=1) + + from os.path import normpath + + return normpath(reverted_path) def path_replace_reverse_movie(path): if path is None: return None - for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): - if path_mapping[1] in path: - path = path.replace(path_mapping[1], path_mapping[0]) - if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): - path = path.replace('/', '\\') - elif path.startswith('/'): - path = path.replace('\\', '/') - break - return path + reverted_path_movie_temp = radarr_reverse_regex.sub(lambda match: path_mapping_reverse_movie[match.group(0)], path, count=1) + + if radarr_platform == 'posix': + from posixpath import normpath + reverted_path = reverted_path_movie_temp.replace('\\', '/') + elif radarr_platform == 'nt': + from ntpath import normpath + reverted_path = reverted_path_movie_temp.replace('/', '\\') + + return normpath(reverted_path) def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3, forced): @@ -137,3 +175,9 @@ def force_unicode(s): except UnicodeDecodeError: s = UnicodeDammit(s).unicode_markup return s + + +sonarr_path_mapping_regex() +sonarr_path_mapping_reverse_regex() +radarr_path_mapping_regex() +radarr_path_mapping_reverse_regex() diff --git a/bazarr/list_subtitles.py b/bazarr/list_subtitles.py index e75d85096..df2c0af01 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.decode(encoding.original_encoding) + text = text.encode().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.decode(encoding.original_encoding) + text = text.encode().decode(encoding.original_encoding) detected_language = langdetect.detect(text) except Exception as e: logging.exception( diff --git a/bazarr/utils.py b/bazarr/utils.py index 21feea412..5336476b5 100644 --- a/bazarr/utils.py +++ b/bazarr/utils.py @@ -111,6 +111,23 @@ def get_sonarr_version(): return sonarr_version +def get_sonarr_platform(): + use_sonarr = settings.general.getboolean('use_sonarr') + apikey_sonarr = settings.sonarr.apikey + sv = url_sonarr + "/api/system/status?apikey=" + apikey_sonarr + sonarr_platform = '' + if use_sonarr: + try: + if requests.get(sv, timeout=60, verify=False).json()['isLinux'] or requests.get(sv, timeout=60, verify=False).json()['isOsx']: + sonarr_platform = 'posix' + elif requests.get(sv, timeout=60, verify=False).json()['isWindows']: + sonarr_platform = 'nt' + except Exception as e: + logging.DEBUG('BAZARR cannot get Sonarr platform') + + return sonarr_platform + + def get_radarr_version(): use_radarr = settings.general.getboolean('use_radarr') apikey_radarr = settings.radarr.apikey @@ -123,3 +140,20 @@ def get_radarr_version(): logging.DEBUG('BAZARR cannot get Radarr version') return radarr_version + + +def get_radarr_platform(): + use_radarr = settings.general.getboolean('use_radarr') + apikey_radarr = settings.radarr.apikey + rv = url_radarr + "/api/system/status?apikey=" + apikey_radarr + radarr_platform = '' + if use_radarr: + try: + if requests.get(rv, timeout=60, verify=False).json()['isLinux'] or requests.get(rv, timeout=60, verify=False).json()['isOsx']: + radarr_platform = 'posix' + elif requests.get(rv, timeout=60, verify=False).json()['isWindows']: + radarr_platform = 'nt' + except Exception as e: + logging.DEBUG('BAZARR cannot get Radarr platform') + + return radarr_platform