bazarr/bazarr/init.py

109 lines
3.8 KiB
Python
Raw Normal View History

# coding=utf-8
import os
import logging
import time
2019-01-26 03:56:55 +00:00
import rarfile
from cork import Cork
2019-01-16 03:11:15 +00:00
from ConfigParser2 import ConfigParser
from config import settings
2018-12-15 00:36:28 +00:00
from check_update import check_releases
from get_args import args
from utils import get_binary
from dogpile.cache.region import register_backend as register_cache_backend
import subliminal
import datetime
2019-04-03 13:56:34 +00:00
# set subliminal_patch user agent
os.environ["SZ_USER_AGENT"] = "Bazarr/1"
2019-04-08 01:27:41 +00:00
# set anti-captcha provider and key
if settings.general.anti_captcha_provider == 'anti-captcha' and settings.anticaptcha.anti_captcha_key != "":
2019-04-08 01:27:41 +00:00
os.environ["ANTICAPTCHA_CLASS"] = 'AntiCaptchaProxyLess'
os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = settings.anticaptcha.anti_captcha_key
elif settings.general.anti_captcha_provider == 'death-by-captcha' and settings.deathbycaptcha.username != "" and settings.deathbycaptcha.password != "":
2019-04-12 02:07:43 +00:00
os.environ["ANTICAPTCHA_CLASS"] = 'DeathByCaptchaProxyLess'
2019-06-11 18:45:48 +00:00
os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = ':'.join(
{settings.deathbycaptcha.username, settings.deathbycaptcha.password})
2019-04-08 01:27:41 +00:00
else:
2019-04-08 02:20:59 +00:00
os.environ["ANTICAPTCHA_CLASS"] = ''
2019-04-08 01:27:41 +00:00
# Check if args.config_dir exist
if not os.path.exists(args.config_dir):
# Create config_dir directory tree
try:
os.mkdir(os.path.join(args.config_dir))
2018-10-01 14:44:27 +00:00
logging.debug("BAZARR Created data directory")
except OSError:
logging.exception(
"BAZARR The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).")
exit(2)
if not os.path.exists(os.path.join(args.config_dir, 'config')):
os.mkdir(os.path.join(args.config_dir, 'config'))
2018-10-01 14:44:27 +00:00
logging.debug("BAZARR Created config folder")
if not os.path.exists(os.path.join(args.config_dir, 'db')):
os.mkdir(os.path.join(args.config_dir, 'db'))
2018-10-01 14:44:27 +00:00
logging.debug("BAZARR Created db folder")
if not os.path.exists(os.path.join(args.config_dir, 'log')):
os.mkdir(os.path.join(args.config_dir, 'log'))
2018-10-01 14:44:27 +00:00
logging.debug("BAZARR Created log folder")
if not os.path.exists(os.path.join(args.config_dir, 'cache')):
os.mkdir(os.path.join(args.config_dir, 'cache'))
logging.debug("BAZARR Created cache folder")
# Configure dogpile file caching for Subliminal request
register_cache_backend("subzero.cache.file", "subzero.cache_backends.file", "SZFileBackend")
subliminal.region.configure('subzero.cache.file', expiration_time=datetime.timedelta(days=30),
arguments={'appname': "sz_cache", 'app_cache_dir': args.config_dir})
subliminal.region.backend.sync()
2019-01-15 16:25:13 +00:00
2018-11-28 11:53:37 +00:00
if not os.path.exists(os.path.join(args.config_dir, 'config', 'releases.txt')):
2018-11-02 19:08:07 +00:00
check_releases()
logging.debug("BAZARR Created releases file")
config_file = os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini'))
cfg = ConfigParser()
2019-01-26 03:56:55 +00:00
2019-09-13 20:30:18 +00:00
if not os.path.exists(os.path.normpath(os.path.join(args.config_dir, 'config', 'users.json'))):
cork = Cork(os.path.normpath(os.path.join(args.config_dir, 'config')), initialize=True)
cork._store.roles[''] = 100
cork._store.save_roles()
tstamp = str(time.time())
username = password = ''
cork._store.users[username] = {
'role': '',
'hash': cork._hash(username, password),
'email_addr': username,
'desc': username,
'creation_date': tstamp
}
cork._store.save_users()
2019-01-26 03:56:55 +00:00
def init_binaries():
exe = get_binary("unrar")
2019-06-11 18:45:48 +00:00
2019-01-26 03:56:55 +00:00
rarfile.UNRAR_TOOL = exe
rarfile.ORIG_UNRAR_TOOL = exe
try:
rarfile.custom_check([rarfile.UNRAR_TOOL], True)
except:
logging.debug("custom check failed for: %s", exe)
2019-06-11 18:45:48 +00:00
2019-01-26 03:56:55 +00:00
rarfile.OPEN_ARGS = rarfile.ORIG_OPEN_ARGS
rarfile.EXTRACT_ARGS = rarfile.ORIG_EXTRACT_ARGS
rarfile.TEST_ARGS = rarfile.ORIG_TEST_ARGS
logging.info("Using UnRAR from: %s", exe)
unrar = exe
2019-06-11 18:45:48 +00:00
2019-01-26 03:56:55 +00:00
return unrar
2019-06-11 18:45:48 +00:00
2019-02-12 21:57:04 +00:00
init_binaries()