2018-10-31 19:34:40 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
import os
|
|
|
|
import sqlite3
|
|
|
|
import logging
|
2018-10-31 19:34:40 +00:00
|
|
|
import time
|
2019-01-26 03:56:55 +00:00
|
|
|
import rarfile
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
from cork import Cork
|
2019-01-16 03:11:15 +00:00
|
|
|
from ConfigParser2 import ConfigParser
|
2019-01-01 07:32:40 +00:00
|
|
|
from config import settings
|
2018-12-15 00:36:28 +00:00
|
|
|
from check_update import check_releases
|
2018-10-31 19:34:40 +00:00
|
|
|
from get_args import args
|
2019-04-23 12:35:50 +00:00
|
|
|
from utils import get_binary
|
2018-08-23 19:58:15 +00:00
|
|
|
|
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':
|
|
|
|
os.environ["ANTICAPTCHA_CLASS"] = 'AntiCaptchaProxyLess'
|
|
|
|
os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = settings.anticaptcha.anti_captcha_key
|
2019-04-12 01:56:50 +00:00
|
|
|
elif settings.general.anti_captcha_provider == 'death-by-captcha':
|
2019-04-12 02:07:43 +00:00
|
|
|
os.environ["ANTICAPTCHA_CLASS"] = 'DeathByCaptchaProxyLess'
|
2019-04-08 02:25:19 +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
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
# Check if args.config_dir exist
|
|
|
|
if not os.path.exists(args.config_dir):
|
2018-08-23 19:58:15 +00:00
|
|
|
# Create config_dir directory tree
|
|
|
|
try:
|
2018-10-31 19:34:40 +00:00
|
|
|
os.mkdir(os.path.join(args.config_dir))
|
2018-10-01 14:44:27 +00:00
|
|
|
logging.debug("BAZARR Created data directory")
|
2018-08-23 19:58:15 +00:00
|
|
|
except OSError:
|
2018-10-31 19:34:40 +00:00
|
|
|
logging.exception(
|
|
|
|
"BAZARR The configuration directory doesn't exist and Bazarr cannot create it (permission issue?).")
|
2018-08-23 19:58:15 +00:00
|
|
|
exit(2)
|
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
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")
|
2018-10-31 19:34:40 +00:00
|
|
|
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")
|
2018-10-31 19:34:40 +00:00
|
|
|
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")
|
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")
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-10-31 19:34:40 +00:00
|
|
|
config_file = os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini'))
|
2018-08-23 19:58:15 +00:00
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
cfg = ConfigParser()
|
2018-08-23 19:58:15 +00:00
|
|
|
try:
|
|
|
|
# Get SQL script from file
|
|
|
|
fd = open(os.path.join(os.path.dirname(__file__), 'create_db.sql'), 'r')
|
|
|
|
script = fd.read()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
# Close SQL script file
|
|
|
|
fd.close()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
# Open database connection
|
2018-10-31 19:34:40 +00:00
|
|
|
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
|
2018-08-23 19:58:15 +00:00
|
|
|
c = db.cursor()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
# Execute script and commit change to database
|
|
|
|
c.executescript(script)
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-08-23 19:58:15 +00:00
|
|
|
# Close database connection
|
|
|
|
db.close()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-10-19 03:33:01 +00:00
|
|
|
logging.info('BAZARR Database created successfully')
|
2018-08-23 19:58:15 +00:00
|
|
|
except:
|
|
|
|
pass
|
2018-09-12 02:32:09 +00:00
|
|
|
|
|
|
|
# Remove unused settings
|
|
|
|
try:
|
|
|
|
with open(config_file, 'r') as f:
|
|
|
|
cfg.read_file(f)
|
|
|
|
except Exception:
|
|
|
|
pass
|
2018-09-17 12:18:52 +00:00
|
|
|
if cfg.has_section('auth'):
|
|
|
|
if cfg.has_option('auth', 'enabled'):
|
|
|
|
enabled = cfg.getboolean('auth', 'enabled')
|
2018-10-31 19:34:40 +00:00
|
|
|
if enabled:
|
2018-09-17 12:18:52 +00:00
|
|
|
cfg.set('auth', 'type', 'basic')
|
2018-10-31 19:34:40 +00:00
|
|
|
else:
|
2018-09-17 12:20:41 +00:00
|
|
|
cfg.set('auth', 'type', 'None')
|
2018-09-17 12:18:52 +00:00
|
|
|
cfg.remove_option('auth', 'enabled')
|
|
|
|
with open(config_file, 'w+') as configfile:
|
|
|
|
cfg.write(configfile)
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-10-30 18:06:30 +00:00
|
|
|
if cfg.has_section('general'):
|
|
|
|
if cfg.has_option('general', 'log_level'):
|
|
|
|
cfg.remove_option('general', 'log_level')
|
|
|
|
cfg.set('general', 'debug', 'False')
|
|
|
|
with open(config_file, 'w+') as configfile:
|
|
|
|
cfg.write(configfile)
|
2019-01-06 17:15:43 +00:00
|
|
|
|
|
|
|
if cfg.has_option('general', 'only_monitored'):
|
|
|
|
only_monitored = cfg.get('general', 'only_monitored')
|
|
|
|
cfg.set('sonarr', 'only_monitored', str(only_monitored))
|
|
|
|
cfg.set('radarr', 'only_monitored', str(only_monitored))
|
|
|
|
cfg.remove_option('general', 'only_monitored')
|
|
|
|
with open(config_file, 'w+') as configfile:
|
|
|
|
cfg.write(configfile)
|
2018-09-12 02:32:09 +00:00
|
|
|
|
2019-01-01 07:32:40 +00:00
|
|
|
# Move providers settings from DB to config file
|
|
|
|
try:
|
2019-02-12 21:57:04 +00:00
|
|
|
db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30)
|
2019-01-01 07:32:40 +00:00
|
|
|
c = db.cursor()
|
|
|
|
enabled_providers = c.execute("SELECT * FROM table_settings_providers WHERE enabled = 1").fetchall()
|
2019-01-01 18:27:50 +00:00
|
|
|
settings_providers = c.execute("SELECT * FROM table_settings_providers").fetchall()
|
2019-01-01 07:32:40 +00:00
|
|
|
c.execute("DROP TABLE table_settings_providers")
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
providers_list = []
|
2019-01-05 14:55:27 +00:00
|
|
|
if enabled_providers:
|
2019-01-01 07:32:40 +00:00
|
|
|
for provider in enabled_providers:
|
|
|
|
providers_list.append(provider[0])
|
2019-01-05 14:55:27 +00:00
|
|
|
else:
|
|
|
|
providers_list = None
|
|
|
|
|
|
|
|
if settings_providers:
|
2019-01-01 18:27:50 +00:00
|
|
|
for provider in settings_providers:
|
2019-01-01 07:32:40 +00:00
|
|
|
if provider[0] == 'opensubtitles':
|
|
|
|
settings.opensubtitles.username = provider[2]
|
|
|
|
settings.opensubtitles.password = provider[3]
|
|
|
|
elif provider[0] == 'addic7ed':
|
|
|
|
settings.addic7ed.username = provider[2]
|
|
|
|
settings.addic7ed.password = provider[3]
|
|
|
|
elif provider[0] == 'legendastv':
|
|
|
|
settings.legendastv.username = provider[2]
|
|
|
|
settings.legendastv.password = provider[3]
|
|
|
|
|
|
|
|
settings.general.enabled_providers = u'' if not providers_list else ','.join(providers_list)
|
2019-02-12 21:57:04 +00:00
|
|
|
with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle:
|
2019-01-01 07:32:40 +00:00
|
|
|
settings.write(handle)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2019-03-22 21:18:11 +00:00
|
|
|
if settings.general.throtteled_providers == '' or None:
|
|
|
|
settings.general.throtteled_providers = '{}'
|
|
|
|
with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle:
|
|
|
|
settings.write(handle)
|
|
|
|
|
2018-10-31 19:34:40 +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)
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
cork._store.roles[''] = 100
|
|
|
|
cork._store.save_roles()
|
2019-01-15 16:25:13 +00:00
|
|
|
|
2018-09-12 02:32:09 +00:00
|
|
|
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():
|
2019-04-23 12:35:50 +00:00
|
|
|
exe = get_binary("unrar")
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return unrar
|
|
|
|
|
2019-02-12 21:57:04 +00:00
|
|
|
init_binaries()
|