From 17577b35be971bbbfd6866ed88146d3d8fbab254 Mon Sep 17 00:00:00 2001 From: morpheus65535 Date: Sun, 31 Jan 2021 08:39:10 -0500 Subject: [PATCH] Moving GA visitor and throttled providers to dedicated files to prevent config.ini corruption. --- bazarr/analytics.py | 11 ++++----- bazarr/api.py | 5 +++-- bazarr/config.py | 1 - bazarr/get_providers.py | 49 +++++++++++++++++++---------------------- bazarr/init.py | 14 ++++++++++++ bazarr/main.py | 5 +---- 6 files changed, 47 insertions(+), 38 deletions(-) diff --git a/bazarr/analytics.py b/bazarr/analytics.py index 69eb8211d..3827aaf2b 100644 --- a/bazarr/analytics.py +++ b/bazarr/analytics.py @@ -28,8 +28,10 @@ def track_event(category=None, action=None, label=None): tracker = Tracker('UA-138214134-3', 'none', conf=anonymousConfig) try: - if settings.analytics.visitor: - visitor = pickle.loads(codecs.decode(settings.analytics.visitor.encode(), "base64")) + if os.path.isfile(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat'))): + with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat')), 'r') as handle: + visitor_text = handle.read() + visitor = pickle.loads(codecs.decode(visitor_text.encode(), "base64")) if visitor.user_agent is None: visitor.user_agent = os.environ.get("SZ_USER_AGENT") if visitor.unique_id > int(0x7fffffff): @@ -62,6 +64,5 @@ def track_event(category=None, action=None, label=None): logging.debug("BAZARR unable to track event.") pass else: - settings.analytics.visitor = codecs.encode(pickle.dumps(visitor), "base64").decode() - with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: - settings.write(handle) + with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat')), 'w+') as handle: + handle.write(codecs.encode(pickle.dumps(visitor), "base64").decode()) diff --git a/bazarr/api.py b/bazarr/api.py index bde16e1f0..e1e0dcb73 100644 --- a/bazarr/api.py +++ b/bazarr/api.py @@ -28,7 +28,8 @@ from list_subtitles import store_subtitles, store_subtitles_movie, series_scan_s from utils import history_log, history_log_movie, blacklist_log, blacklist_delete, blacklist_delete_all, \ blacklist_log_movie, blacklist_delete_movie, blacklist_delete_all_movie, get_sonarr_version, get_radarr_version, \ delete_subtitles, subtitles_apply_mods -from get_providers import get_providers, get_providers_auth, list_throttled_providers, reset_throttled_providers +from get_providers import get_providers, get_providers_auth, list_throttled_providers, reset_throttled_providers, \ + get_throttled_providers, set_throttled_providers from event_handler import event_stream from scheduler import scheduler from subsyncer import subsync @@ -109,7 +110,7 @@ class BadgesProviders(Resource): @authenticate def get(self): result = { - "throttled_providers": len(eval(str(settings.general.throtteled_providers))) + "throttled_providers": len(eval(str(get_throttled_providers()))) } return jsonify(result) diff --git a/bazarr/config.py b/bazarr/config.py index 88c884864..9e20b1925 100644 --- a/bazarr/config.py +++ b/bazarr/config.py @@ -44,7 +44,6 @@ defaults = { 'ignore_vobsub_subs': 'False', 'adaptive_searching': 'False', 'enabled_providers': '', - 'throtteled_providers': '{}', 'multithreading': 'True', 'chmod_enabled': 'False', 'chmod': '0640', diff --git a/bazarr/get_providers.py b/bazarr/get_providers.py index f0754c1b9..796baccb3 100644 --- a/bazarr/get_providers.py +++ b/bazarr/get_providers.py @@ -75,11 +75,6 @@ PROVIDERS_FORCED_OFF = ["addic7ed", "tvsubtitles", "legendasdivx", "legendastv", throttle_count = {} -if not settings.general.throtteled_providers: - tp = {} -else: - tp = eval(str(settings.general.throtteled_providers)) - def provider_pool(): if settings.general.getboolean('multithreading'): @@ -88,7 +83,6 @@ def provider_pool(): def get_providers(): - changed = False providers_list = [] if settings.general.enabled_providers: for provider in settings.general.enabled_providers.lower().split(','): @@ -104,12 +98,7 @@ def get_providers(): else: logging.info("Using %s again after %s, (disabled because: %s)", provider, throttle_desc, reason) del tp[provider] - settings.general.throtteled_providers = str(tp) - changed = True - - if changed: - with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: - settings.write(handle) + set_throttled_providers(str(tp)) # if forced only is enabled: # fixme: Prepared for forced only implementation to remove providers with don't support forced only subtitles # for provider in providers_list: @@ -203,9 +192,7 @@ def provider_throttle(name, exception): logging.debug("Couldn't remove cache file: %s", os.path.basename(fn)) else: tp[name] = (cls_name, throttle_until, throttle_description) - settings.general.throtteled_providers = str(tp) - with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: - settings.write(handle) + set_throttled_providers(str(tp)) logging.info("Throttling %s for %s, until %s, because of: %s. Exception info: %r", name, throttle_description, throttle_until.strftime("%y/%m/%d %H:%M"), cls_name, exception.args[0] @@ -249,8 +236,7 @@ def update_throttled_provider(): for provider in list(tp): if provider not in settings.general.enabled_providers: del tp[provider] - settings.general.throtteled_providers = str(tp) - changed = True + set_throttled_providers(str(tp)) reason, until, throttle_desc = tp.get(provider, (None, None, None)) @@ -261,12 +247,7 @@ def update_throttled_provider(): else: logging.info("Using %s again after %s, (disabled because: %s)", provider, throttle_desc, reason) del tp[provider] - settings.general.throtteled_providers = str(tp) - changed = True - - if changed: - with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: - settings.write(handle) + set_throttled_providers(str(tp)) event_stream(type='badges_providers') @@ -284,8 +265,24 @@ def list_throttled_providers(): def reset_throttled_providers(): for provider in list(tp): del tp[provider] - settings.general.throtteled_providers = str(tp) - with open(os.path.join(args.config_dir, 'config', 'config.ini'), 'w+') as handle: - settings.write(handle) + set_throttled_providers(str(tp)) update_throttled_provider() logging.info('BAZARR throttled providers have been reset.') + + +def get_throttled_providers(): + providers = {} + if os.path.exists(os.path.join(args.config_dir, 'config', 'throttled_providers.dat')): + with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'throttled_providers.dat')), 'r') as handle: + providers = handle.read() + if not providers: + providers = {} + return providers + + +def set_throttled_providers(data): + with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'throttled_providers.dat')), 'w+') as handle: + handle.write(data) + + +tp = eval(str(get_throttled_providers())) diff --git a/bazarr/init.py b/bazarr/init.py index 07a3affe0..f503b4170 100644 --- a/bazarr/init.py +++ b/bazarr/init.py @@ -124,6 +124,20 @@ if not os.path.exists(os.path.join(args.config_dir, 'config', 'releases.txt')): config_file = os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini')) +# Move GA visitor from config.ini to dedicated file +if settings.analytics.visitor: + with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'analytics.dat')), 'w+') as handle: + handle.write(settings.analytics.visitor) + with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini')), 'w+') as handle: + settings.remove_option('analytics', 'visitor') + settings.write(handle) + +# Clean unused settings from config.ini +with open(os.path.normpath(os.path.join(args.config_dir, 'config', 'config.ini')), 'w+') as handle: + settings.remove_option('general', 'throtteled_providers') + settings.write(handle) + + # Commenting out the password reset process as it could be having unwanted effects and most of the users have already # moved to new password hashing algorithm. diff --git a/bazarr/main.py b/bazarr/main.py index 92a94ebe8..7ab47c6e0 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -126,10 +126,7 @@ def login_page(): @app.context_processor def template_variable_processor(): - restart_required = database.execute("SELECT configured, updated FROM system", only_one=True) - - return dict(restart_required=restart_required['configured'], update_required=restart_required['updated'], - settings=settings, args=args) + return dict(settings=settings, args=args) def api_authorize():