Continuing development.

This commit is contained in:
Louis Vézina 2019-10-09 00:08:39 -04:00
parent fc37817530
commit 58024d1fed
4 changed files with 118 additions and 40 deletions

View File

@ -7,8 +7,6 @@ from peewee import *
from playhouse.sqliteq import SqliteQueueDatabase from playhouse.sqliteq import SqliteQueueDatabase
from playhouse.migrate import * from playhouse.migrate import *
from helper import path_replace, path_replace_movie, path_replace_reverse, path_replace_reverse_movie
database = SqliteQueueDatabase( database = SqliteQueueDatabase(
None, None,
use_gevent=False, use_gevent=False,
@ -21,11 +19,13 @@ migrator = SqliteMigrator(database)
@database.func('path_substitution') @database.func('path_substitution')
def path_substitution(path): def path_substitution(path):
from helper import path_replace
return path_replace(path) return path_replace(path)
@database.func('path_substitution_movie') @database.func('path_substitution_movie')
def path_substitution_movie(path): def path_substitution_movie(path):
from helper import path_replace_movie
return path_replace_movie(path) return path_replace_movie(path)

View File

@ -5,70 +5,108 @@ import os
import re import re
import types import types
import logging import logging
import libs
import chardet import chardet
from bs4 import UnicodeDammit from bs4 import UnicodeDammit
from config import settings 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): def path_replace(path):
if path is None: if path is None:
return None return None
for path_mapping in ast.literal_eval(settings.general.path_mappings): reverted_path = sonarr_regex.sub(lambda match: path_mapping[match.group(0)], path, count=1)
if path_mapping[0] in path:
path = path.replace(path_mapping[0], path_mapping[1]) from os.path import normpath
if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path):
path = path.replace('/', '\\') return normpath(reverted_path)
elif path.startswith('/'):
path = path.replace('\\', '/')
break
return path
def path_replace_reverse(path): def path_replace_reverse(path):
if path is None: if path is None:
return None return None
for path_mapping in ast.literal_eval(settings.general.path_mappings): reverted_path_temp = sonarr_reverse_regex.sub(lambda match: path_mapping_reverse[match.group(0)], path, count=1)
if path_mapping[1] in path:
path = path.replace(path_mapping[1], path_mapping[0]) if sonarr_platform == 'posix':
if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): from posixpath import normpath
path = path.replace('/', '\\') reverted_path = reverted_path_temp.replace('\\', '/')
elif path.startswith('/'): elif sonarr_platform == 'nt':
path = path.replace('\\', '/') from ntpath import normpath
break reverted_path = reverted_path_temp.replace('/', '\\')
return path
return normpath(reverted_path)
def path_replace_movie(path): def path_replace_movie(path):
if path is None: if path is None:
return None return None
for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): reverted_path = radarr_regex.sub(lambda match: path_mapping_movie[match.group(0)], path, count=1)
if path_mapping[0] in path:
path = path.replace(path_mapping[0], path_mapping[1]) from os.path import normpath
if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path):
path = path.replace('/', '\\') return normpath(reverted_path)
elif path.startswith('/'):
path = path.replace('\\', '/')
break
return path
def path_replace_reverse_movie(path): def path_replace_reverse_movie(path):
if path is None: if path is None:
return None return None
for path_mapping in ast.literal_eval(settings.general.path_mappings_movie): reverted_path_movie_temp = radarr_reverse_regex.sub(lambda match: path_mapping_reverse_movie[match.group(0)], path, count=1)
if path_mapping[1] in path:
path = path.replace(path_mapping[1], path_mapping[0]) if radarr_platform == 'posix':
if path.startswith('\\\\') or re.match(r'^[a-zA-Z]:\\', path): from posixpath import normpath
path = path.replace('/', '\\') reverted_path = reverted_path_movie_temp.replace('\\', '/')
elif path.startswith('/'): elif radarr_platform == 'nt':
path = path.replace('\\', '/') from ntpath import normpath
break reverted_path = reverted_path_movie_temp.replace('/', '\\')
return path
return normpath(reverted_path)
def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3, forced): def pp_replace(pp_command, episode, subtitles, language, language_code2, language_code3, forced):
@ -137,3 +175,9 @@ def force_unicode(s):
except UnicodeDecodeError: except UnicodeDecodeError:
s = UnicodeDammit(s).unicode_markup s = UnicodeDammit(s).unicode_markup
return s return s
sonarr_path_mapping_regex()
sonarr_path_mapping_reverse_regex()
radarr_path_mapping_regex()
radarr_path_mapping_reverse_regex()

View File

@ -93,7 +93,7 @@ def store_subtitles(file):
text = ' '.join(text) text = ' '.join(text)
encoding = UnicodeDammit(text) encoding = UnicodeDammit(text)
try: try:
text = text.decode(encoding.original_encoding) text = text.encode().decode(encoding.original_encoding)
detected_language = langdetect.detect(text) detected_language = langdetect.detect(text)
except Exception as e: except Exception as e:
logging.exception( logging.exception(
@ -190,7 +190,7 @@ def store_subtitles_movie(file):
text = ' '.join(text) text = ' '.join(text)
encoding = UnicodeDammit(text) encoding = UnicodeDammit(text)
try: try:
text = text.decode(encoding.original_encoding) text = text.encode().decode(encoding.original_encoding)
detected_language = langdetect.detect(text) detected_language = langdetect.detect(text)
except Exception as e: except Exception as e:
logging.exception( logging.exception(

View File

@ -111,6 +111,23 @@ def get_sonarr_version():
return 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(): def get_radarr_version():
use_radarr = settings.general.getboolean('use_radarr') use_radarr = settings.general.getboolean('use_radarr')
apikey_radarr = settings.radarr.apikey apikey_radarr = settings.radarr.apikey
@ -123,3 +140,20 @@ def get_radarr_version():
logging.DEBUG('BAZARR cannot get Radarr version') logging.DEBUG('BAZARR cannot get Radarr version')
return 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