bazarr/bazarr/config.py

606 lines
19 KiB
Python
Raw Normal View History

2018-12-14 22:18:52 +00:00
# coding=utf-8
2020-03-26 19:37:17 +00:00
import hashlib
2018-12-14 22:18:52 +00:00
import os
2021-03-25 14:22:43 +00:00
import ast
2018-12-14 22:18:52 +00:00
from urllib.parse import quote_plus
2020-10-10 14:26:56 +00:00
from subliminal.cache import region
from simpleconfigparser import simpleconfigparser, configparser, NoOptionError
2018-12-14 22:18:52 +00:00
2019-01-02 19:43:40 +00:00
from get_args import args
2018-12-14 22:18:52 +00:00
class SimpleConfigParser(simpleconfigparser):
def get(self, section, option, raw=False, vars=None):
try:
return configparser.get(self, section, option, raw=raw, vars=vars)
except NoOptionError:
return None
defaults = {
'general': {
'ip': '0.0.0.0',
'port': '6767',
2019-12-30 19:18:42 +00:00
'base_url': '',
'path_mappings': '[]',
'debug': 'False',
'branch': 'master',
'auto_update': 'True',
'single_language': 'False',
'minimum_score': '90',
'use_scenename': 'True',
'use_postprocessing': 'False',
'postprocessing_cmd': '',
2020-05-15 18:12:31 +00:00
'postprocessing_threshold': '90',
'use_postprocessing_threshold': 'False',
'postprocessing_threshold_movie': '70',
'use_postprocessing_threshold_movie': 'False',
'use_sonarr': 'False',
'use_radarr': 'False',
'path_mappings_movie': '[]',
'serie_default_enabled': 'False',
'serie_default_profile': '',
'movie_default_enabled': 'False',
'movie_default_profile': '',
'page_size': '25',
2020-05-20 15:29:49 +00:00
'page_size_manual_search': '10',
'minimum_score_movie': '70',
'use_embedded_subs': 'True',
'embedded_subs_show_desired': 'True',
'utf8_encode': 'True',
'ignore_pgs_subs': 'False',
'ignore_vobsub_subs': 'False',
'ignore_ass_subs': 'False',
'adaptive_searching': 'False',
2021-03-25 14:22:43 +00:00
'enabled_providers': '[]',
'multithreading': 'True',
2019-03-26 19:49:03 +00:00
'chmod_enabled': 'False',
2019-02-27 20:55:06 +00:00
'chmod': '0640',
'subfolder': 'current',
'subfolder_custom': '',
2019-03-17 18:53:36 +00:00
'upgrade_subs': 'True',
'upgrade_frequency': '12',
2019-03-19 04:08:53 +00:00
'days_to_upgrade_subs': '7',
2019-04-07 20:09:17 +00:00
'upgrade_manual': 'True',
'anti_captcha_provider': 'None',
2020-05-07 11:39:59 +00:00
'wanted_search_frequency': '3',
2020-07-29 13:11:45 +00:00
'wanted_search_frequency_movie': '3',
2021-03-25 14:22:43 +00:00
'subzero_mods': '[]',
'dont_notify_manual_actions': 'False'
2019-01-15 16:25:13 +00:00
},
'auth': {
'type': 'None',
'username': '',
'password': ''
2019-01-15 16:25:13 +00:00
},
'sonarr': {
'ip': '127.0.0.1',
'port': '8989',
'base_url': '/',
'ssl': 'False',
'apikey': '',
2019-01-06 17:15:43 +00:00
'full_update': 'Daily',
'full_update_day': '6',
'full_update_hour': '4',
2019-01-06 17:15:43 +00:00
'only_monitored': 'False',
'series_sync': '60',
'episodes_sync': '60',
'excluded_tags': '[]',
'excluded_series_types': '[]',
'use_ffprobe_cache': 'True'
2019-10-05 21:15:57 +00:00
},
'radarr': {
'ip': '127.0.0.1',
'port': '7878',
'base_url': '/',
'ssl': 'False',
'apikey': '',
2019-01-06 17:15:43 +00:00
'full_update': 'Daily',
'full_update_day': '6',
'full_update_hour': '5',
2019-01-06 17:15:43 +00:00
'only_monitored': 'False',
'movies_sync': '60',
'excluded_tags': '[]',
'use_ffprobe_cache': 'True'
2019-10-05 21:15:57 +00:00
},
'proxy': {
'type': 'None',
'url': '',
'port': '',
'username': '',
'password': '',
2021-03-25 14:22:43 +00:00
'exclude': '["localhost","127.0.0.1"]'
2019-01-15 16:25:13 +00:00
},
'opensubtitles': {
'username': '',
2019-01-03 20:57:21 +00:00
'password': '',
2019-01-04 20:01:18 +00:00
'use_tag_search': 'False',
2019-01-24 15:55:36 +00:00
'vip': 'False',
'ssl': 'False',
2019-01-04 20:01:18 +00:00
'timeout': '15',
'skip_wrong_fps': 'False'
2019-01-15 16:25:13 +00:00
},
'opensubtitlescom': {
'username': '',
'password': '',
'use_hash': 'True'
},
'addic7ed': {
'username': '',
2020-05-20 23:29:11 +00:00
'password': ''
},
'podnapisi': {
'verify_ssl': 'True'
},
2020-01-16 20:38:41 +00:00
'legendasdivx': {
'username': '',
'password': '',
'skip_wrong_fps': 'False'
2020-01-16 20:38:41 +00:00
},
2021-08-31 17:26:16 +00:00
'ktuvit': {
'email': '',
'hashed_password': ''
},
'legendastv': {
'username': '',
'password': '',
'featured_only': 'False'
2019-01-15 16:25:13 +00:00
},
'xsubs': {
'username': '',
'password': ''
},
2019-01-04 20:01:18 +00:00
'assrt': {
'token': ''
2019-04-07 20:09:17 +00:00
},
'anticaptcha': {
'anti_captcha_key': ''
},
'deathbycaptcha': {
'username': '',
'password': ''
},
'napisy24': {
'username': '',
'password': ''
2019-06-21 01:34:14 +00:00
},
'subscene': {
'username': '',
'password': ''
},
'betaseries': {
'token': ''
},
'analytics': {
'enabled': 'True'
2019-10-05 21:15:57 +00:00
},
'titlovi': {
'username': '',
'password': ''
},
2021-09-21 13:11:15 +00:00
'titulky': {
'username': '',
2021-10-27 18:23:58 +00:00
'password': '',
'skip_wrong_fps': 'False',
'approved_only': 'False',
'multithreading': 'True'
2021-09-21 13:11:15 +00:00
},
'subsync': {
'use_subsync': 'False',
'use_subsync_threshold': 'False',
'subsync_threshold': '90',
'use_subsync_movie_threshold': 'False',
'subsync_movie_threshold': '70',
'debug': 'False'
2021-06-10 20:00:42 +00:00
},
'series_scores': {
"hash": 359,
"series": 180,
"year": 90,
"season": 30,
"episode": 30,
"release_group": 15,
"source": 7,
"audio_codec": 3,
"resolution": 2,
"video_codec": 2,
"hearing_impaired": 1,
"streaming_service": 0,
"edition": 0,
},
'movie_scores': {
"hash": 119,
"title": 60,
"year": 30,
"release_group": 15,
"source": 7,
"audio_codec": 3,
"resolution": 2,
"video_codec": 2,
"hearing_impaired": 1,
"streaming_service": 0,
"edition": 0,
2019-04-07 20:09:17 +00:00
}
}
settings = SimpleConfigParser(defaults=defaults, interpolation=None)
2019-01-02 19:43:40 +00:00
settings.read(os.path.join(args.config_dir, 'config', 'config.ini'))
2018-12-14 22:18:52 +00:00
settings.general.base_url = settings.general.base_url if settings.general.base_url else '/'
2021-03-25 14:22:43 +00:00
base_url = settings.general.base_url.rstrip('/')
ignore_keys = ['flask_secret_key',
'page_size',
'page_size_manual_search',
'throtteled_providers']
raw_keys = ['movie_default_forced', 'serie_default_forced']
array_keys = ['excluded_tags',
'exclude',
'subzero_mods',
'excluded_series_types',
'enabled_providers',
'path_mappings',
'path_mappings_movie']
str_keys = ['chmod']
empty_values = ['', 'None', 'null', 'undefined', None, []]
# Increase Sonarr and Radarr sync interval since we now use SignalR feed to update in real time
if int(settings.sonarr.series_sync) < 15:
settings.sonarr.series_sync = "60"
if int(settings.sonarr.episodes_sync) < 15:
settings.sonarr.episodes_sync = "60"
if int(settings.radarr.movies_sync) < 15:
settings.radarr.movies_sync = "60"
if os.path.exists(os.path.join(args.config_dir, 'config', 'config.ini')):
with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle:
settings.write(handle)
2021-03-25 14:22:43 +00:00
def get_settings():
result = dict()
sections = settings.sections()
for sec in sections:
sec_values = settings.items(sec, False)
values_dict = dict()
for sec_val in sec_values:
key = sec_val[0]
value = sec_val[1]
if key in ignore_keys:
continue
if key not in raw_keys:
# Do some postprocessings
if value in empty_values:
if key in array_keys:
value = []
else:
continue
elif key in array_keys:
value = get_array_from(value)
2021-03-25 14:22:43 +00:00
elif value == 'True':
value = True
elif value == 'False':
value = False
else:
if key not in str_keys:
try:
value = int(value)
except ValueError:
pass
values_dict[key] = value
result[sec] = values_dict
return result
2019-11-06 01:49:08 +00:00
2020-03-11 10:58:45 +00:00
def save_settings(settings_items):
2020-04-24 15:19:18 +00:00
from database import database
2020-05-08 04:22:14 +00:00
configure_debug = False
configure_captcha = False
update_schedule = False
sonarr_changed = False
radarr_changed = False
2020-05-19 13:27:13 +00:00
update_path_map = False
2020-05-08 04:22:14 +00:00
configure_proxy = False
2020-09-03 10:33:24 +00:00
exclusion_updated = False
sonarr_exclusion_updated = False
radarr_exclusion_updated = False
2020-05-08 04:22:14 +00:00
2021-03-25 14:22:43 +00:00
# Subzero Mods
update_subzero = False
subzero_mods = get_array_from(settings.general.subzero_mods)
if len(subzero_mods) == 1 and subzero_mods[0] == '':
subzero_mods = []
2020-03-11 10:58:45 +00:00
for key, value in settings_items:
2021-03-25 14:22:43 +00:00
settings_keys = key.split('-')
# Make sure that text based form values aren't pass as list
if isinstance(value, list) and len(value) == 1 and settings_keys[-1] not in array_keys:
2020-04-24 15:19:18 +00:00
value = value[0]
2021-08-11 13:39:59 +00:00
if value in empty_values and value != '':
2021-03-25 14:22:43 +00:00
value = None
2020-04-24 15:19:18 +00:00
2021-03-25 14:22:43 +00:00
# Make sure empty language list are stored correctly
if settings_keys[-1] in array_keys and value[0] in empty_values :
2020-04-29 02:39:30 +00:00
value = []
2021-03-25 14:22:43 +00:00
# Handle path mappings settings since they are array in array
if settings_keys[-1] in ['path_mappings', 'path_mappings_movie']:
value = [v.split(',') for v in value]
2020-03-11 10:58:45 +00:00
if value == 'true':
value = 'True'
elif value == 'false':
value = 'False'
2020-03-26 19:37:17 +00:00
if key == 'settings-auth-password':
2021-03-25 14:22:43 +00:00
if value != settings.auth.password and value != None:
value = hashlib.md5(value.encode('utf-8')).hexdigest()
2020-03-26 19:37:17 +00:00
2020-05-08 04:22:14 +00:00
if key == 'settings-general-debug':
configure_debug = True
if key in ['settings-general-anti_captcha_provider', 'settings-anticaptcha-anti_captcha_key',
'settings-deathbycaptcha-username', 'settings-deathbycaptcha-password']:
configure_captcha = True
2020-05-15 01:21:09 +00:00
if key in ['update_schedule', 'settings-general-use_sonarr', 'settings-general-use_radarr',
'settings-general-auto_update', 'settings-general-upgrade_subs',
'settings-sonarr-series_sync', 'settings-sonarr-episodes_sync', 'settings-radarr-movies_sync',
'settings-sonarr-full_update', 'settings-sonarr-full_update_day', 'settings-sonarr-full_update_hour',
'settings-radarr-full_update', 'settings-radarr-full_update_day', 'settings-radarr-full_update_hour',
'settings-general-wanted_search_frequency', 'settings-general-wanted_search_frequency_movie',
'settings-general-upgrade_frequency']:
2020-05-08 04:22:14 +00:00
update_schedule = True
if key in ['settings-general-use_sonarr', 'settings-sonarr-ip', 'settings-sonarr-port',
'settings-sonarr-base_url', 'settings-sonarr-ssl', 'settings-sonarr-apikey']:
sonarr_changed = True
if key in ['settings-general-use_radarr', 'settings-radarr-ip', 'settings-radarr-port',
'settings-radarr-base_url', 'settings-radarr-ssl', 'settings-radarr-apikey']:
radarr_changed = True
2020-05-19 13:27:13 +00:00
if key in ['settings-general-path_mappings', 'settings-general-path_mappings_movie']:
update_path_map = True
2020-05-08 04:22:14 +00:00
if key in ['settings-proxy-type', 'settings-proxy-url', 'settings-proxy-port', 'settings-proxy-username',
'settings-proxy-password']:
configure_proxy = True
if key in ['settings-sonarr-excluded_tags', 'settings-sonarr-only_monitored',
'settings-sonarr-excluded_series_types', 'settings.radarr.excluded_tags',
'settings-radarr-only_monitored']:
exclusion_updated = True
if key in ['settings-sonarr-excluded_tags', 'settings-sonarr-only_monitored',
'settings-sonarr-excluded_series_types']:
sonarr_exclusion_updated = True
if key in ['settings.radarr.excluded_tags', 'settings-radarr-only_monitored']:
radarr_exclusion_updated = True
2020-10-10 14:26:56 +00:00
if key == 'settings-addic7ed-username':
if key != settings.addic7ed.username:
region.delete('addic7ed_data')
if key == 'settings-legendasdivx-username':
if key != settings.legendasdivx.username:
region.delete('legendasdivx_cookies2')
if key == 'settings-opensubtitles-username':
if key != settings.opensubtitles.username:
region.delete('os_token')
if key == 'settings-opensubtitlescom-username':
if key != settings.opensubtitlescom.username:
region.delete('oscom_token')
2020-10-10 14:26:56 +00:00
if key == 'settings-subscene-username':
if key != settings.subscene.username:
region.delete('subscene_cookies2')
if key == 'settings-titlovi-username':
if key != settings.titlovi.username:
region.delete('titlovi_token')
2020-03-11 10:58:45 +00:00
if settings_keys[0] == 'settings':
settings[settings_keys[1]][settings_keys[2]] = str(value)
2021-03-25 14:22:43 +00:00
if settings_keys[0] == 'subzero':
mod = settings_keys[1]
enabled = value == 'True'
if mod in subzero_mods and not enabled:
subzero_mods.remove(mod)
elif enabled:
subzero_mods.append(mod)
# Handle color
if mod == 'color':
previous = None
for exist_mod in subzero_mods:
if exist_mod.startswith('color'):
previous = exist_mod
break
if previous is not None:
subzero_mods.remove(previous)
if value not in empty_values:
subzero_mods.append(value)
update_subzero = True
if update_subzero:
settings.set('general', 'subzero_mods', ','.join(subzero_mods))
2020-03-11 10:58:45 +00:00
with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle:
settings.write(handle)
2020-05-08 04:22:14 +00:00
# Reconfigure Bazarr to reflect changes
if configure_debug:
from logger import configure_logging
configure_logging(settings.general.getboolean('debug') or args.debug)
if configure_captcha:
configure_captcha_func()
if update_schedule:
2020-05-10 14:56:48 +00:00
from api import scheduler
scheduler.update_configurable_tasks()
2020-05-08 04:22:14 +00:00
if sonarr_changed:
from signalr_client import sonarr_signalr_client
try:
sonarr_signalr_client.restart()
except:
pass
if radarr_changed:
from signalr_client import radarr_signalr_client
try:
radarr_signalr_client.restart()
except:
pass
2020-05-19 13:27:13 +00:00
if update_path_map:
from helper import path_mappings
path_mappings.update()
2020-05-08 04:22:14 +00:00
if configure_proxy:
configure_proxy_func()
2020-03-11 10:58:45 +00:00
if exclusion_updated:
from event_handler import event_stream
event_stream(type='badges')
if sonarr_exclusion_updated:
event_stream(type='reset-episode-wanted')
if radarr_exclusion_updated:
event_stream(type='reset-movie-wanted')
2020-03-11 10:58:45 +00:00
2019-11-06 01:49:08 +00:00
def url_sonarr():
if settings.sonarr.getboolean('ssl'):
protocol_sonarr = "https"
else:
protocol_sonarr = "http"
if settings.sonarr.base_url == '':
settings.sonarr.base_url = "/"
if not settings.sonarr.base_url.startswith("/"):
settings.sonarr.base_url = "/" + settings.sonarr.base_url
if settings.sonarr.base_url.endswith("/"):
settings.sonarr.base_url = settings.sonarr.base_url[:-1]
2021-03-25 14:22:43 +00:00
if settings.sonarr.port in empty_values:
port = ""
else:
port = f":{settings.sonarr.port}"
2019-11-06 01:49:08 +00:00
2021-03-25 14:22:43 +00:00
return f"{protocol_sonarr}://{settings.sonarr.ip}{port}{settings.sonarr.base_url}"
2019-11-06 01:49:08 +00:00
2019-11-06 01:49:08 +00:00
def url_sonarr_short():
if settings.sonarr.getboolean('ssl'):
protocol_sonarr = "https"
else:
protocol_sonarr = "http"
2021-03-25 14:22:43 +00:00
if settings.sonarr.port in empty_values:
port = ""
else:
port = f":{settings.sonarr.port}"
2019-11-06 01:49:08 +00:00
2021-03-25 14:22:43 +00:00
return f"{protocol_sonarr}://{settings.sonarr.ip}{port}"
2019-11-06 01:49:08 +00:00
2019-11-06 01:49:08 +00:00
def url_radarr():
if settings.radarr.getboolean('ssl'):
protocol_radarr = "https"
else:
protocol_radarr = "http"
if settings.radarr.base_url == '':
settings.radarr.base_url = "/"
if not settings.radarr.base_url.startswith("/"):
settings.radarr.base_url = "/" + settings.radarr.base_url
if settings.radarr.base_url.endswith("/"):
settings.radarr.base_url = settings.radarr.base_url[:-1]
2021-03-25 14:22:43 +00:00
if settings.radarr.port in empty_values:
port = ""
else:
port = f":{settings.radarr.port}"
2019-11-06 01:49:08 +00:00
2021-03-25 14:22:43 +00:00
return f"{protocol_radarr}://{settings.radarr.ip}{port}{settings.radarr.base_url}"
2019-11-06 01:49:08 +00:00
2019-11-06 01:49:08 +00:00
def url_radarr_short():
if settings.radarr.getboolean('ssl'):
protocol_radarr = "https"
else:
protocol_radarr = "http"
2021-03-25 14:22:43 +00:00
if settings.radarr.port in empty_values:
port = ""
else:
port = f":{settings.radarr.port}"
2019-11-06 01:49:08 +00:00
2021-03-25 14:22:43 +00:00
return f"{protocol_radarr}://{settings.radarr.ip}{port}"
2020-05-08 04:22:14 +00:00
2021-03-25 14:22:43 +00:00
def get_array_from(property):
if property:
if '[' in property:
return ast.literal_eval(property)
elif ',' in property:
2021-03-25 14:22:43 +00:00
return property.split(',')
else:
return [property]
2021-03-25 14:22:43 +00:00
else:
return []
2020-05-08 04:22:14 +00:00
2020-05-08 04:22:14 +00:00
def configure_captcha_func():
# set anti-captcha provider and key
if settings.general.anti_captcha_provider == 'anti-captcha' and settings.anticaptcha.anti_captcha_key != "":
os.environ["ANTICAPTCHA_CLASS"] = 'AntiCaptchaProxyLess'
os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = str(settings.anticaptcha.anti_captcha_key)
elif settings.general.anti_captcha_provider == 'death-by-captcha' and settings.deathbycaptcha.username != "" and \
settings.deathbycaptcha.password != "":
os.environ["ANTICAPTCHA_CLASS"] = 'DeathByCaptchaProxyLess'
os.environ["ANTICAPTCHA_ACCOUNT_KEY"] = str(':'.join(
{settings.deathbycaptcha.username, settings.deathbycaptcha.password}))
else:
os.environ["ANTICAPTCHA_CLASS"] = ''
2020-05-19 13:27:13 +00:00
2020-05-08 04:22:14 +00:00
def configure_proxy_func():
if settings.proxy.type != 'None':
if settings.proxy.username != '' and settings.proxy.password != '':
proxy = settings.proxy.type + '://' + quote_plus(settings.proxy.username) + ':' + \
quote_plus(settings.proxy.password) + '@' + settings.proxy.url + ':' + settings.proxy.port
2020-05-08 04:22:14 +00:00
else:
proxy = settings.proxy.type + '://' + settings.proxy.url + ':' + settings.proxy.port
os.environ['HTTP_PROXY'] = str(proxy)
os.environ['HTTPS_PROXY'] = str(proxy)
2021-03-25 14:22:43 +00:00
exclude = ','.join(get_array_from(settings.proxy.exclude))
os.environ['NO_PROXY'] = exclude