From 852dfba8c5014e7c89048bb47796deac20b878a0 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 3 Aug 2024 21:40:28 +0200 Subject: [PATCH] add config value update hooks --- tubearchivist/appsettings/src/config.py | 43 +++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tubearchivist/appsettings/src/config.py b/tubearchivist/appsettings/src/config.py index 9124242e..c14626e6 100644 --- a/tubearchivist/appsettings/src/config.py +++ b/tubearchivist/appsettings/src/config.py @@ -9,9 +9,11 @@ from time import sleep from typing import Literal, TypedDict import requests +from appsettings.src.snapshot import ElasticSnapshot from common.src.es_connect import ElasticWrap from common.src.ta_redis import RedisArchivist from django.conf import settings +from download.src.yt_dlp_base import CookieHandler class SubscriptionsConfigType(TypedDict): @@ -122,6 +124,47 @@ class AppConfig: if exists is None: raise ValueError(f"trying to access invalid config key: {key_map}") + def post_process_updated(self, data: dict) -> None: + """apply hooks for some config keys""" + for config_value, updated_value in data: + if config_value == "downloads.cookie_import": + self.process_cookie(updated_value) + if config_value == "application.enable_snapshot" and updated_value: + ElasticSnapshot().setup() + + def process_cookie(self, updated_value): + """import and validate cookie""" + handler = CookieHandler(self.config) + if updated_value: + try: + handler.import_cookie() + except FileNotFoundError: + print("cookie: import failed, file not found") + handler.revoke() + self._fail_message("Cookie file not found.") + return + + valid = handler.validate() + if not valid: + handler.revoke() + self._fail_message("Failed to validate cookie file.") + else: + handler.revoke() + + @staticmethod + def _fail_message(message_line): + """notify our failure""" + key = "message:setting" + message = { + "status": key, + "group": "setting:application", + "level": "error", + "title": "Cookie import failed", + "messages": [message_line], + "id": "0000", + } + RedisArchivist().set_message(key, message=message, expire=True) + class ReleaseVersion: """compare local version with remote version"""