|
|
|
@ -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()
|
|
|
|
|