From 4b1ebf44c966d6ee3c08b05ef96396e5e93d818b Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 21 Jul 2022 23:01:32 +0700 Subject: [PATCH] handle filenotfounderror for manual cookie import --- .../home/src/download/yt_dlp_base.py | 9 +++-- tubearchivist/home/views.py | 33 ++++++++++++------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/tubearchivist/home/src/download/yt_dlp_base.py b/tubearchivist/home/src/download/yt_dlp_base.py index b3c94af4..397784ec 100644 --- a/tubearchivist/home/src/download/yt_dlp_base.py +++ b/tubearchivist/home/src/download/yt_dlp_base.py @@ -83,8 +83,13 @@ class CookieHandler: """import cookie from file""" cache_path = self.config["application"]["cache_dir"] import_path = os.path.join(cache_path, "import", "cookies.google.txt") - with open(import_path, encoding="utf-8") as cookie_file: - cookie = cookie_file.read() + + try: + with open(import_path, encoding="utf-8") as cookie_file: + cookie = cookie_file.read() + except FileNotFoundError as err: + print(f"cookie: {import_path} file not found") + raise err self.set_cookie(cookie) diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index c5a30e3b..55b6918c 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -929,26 +929,37 @@ class SettingsView(View): if config_value == "cookie_import": self.process_cookie(config, updated_value) - @staticmethod - def process_cookie(config, updated_value): + def process_cookie(self, config, updated_value): """import and validate cookie""" handler = CookieHandler(config) if updated_value: - handler.import_cookie() + 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() - key = "message:setting" - message = { - "status": key, - "level": "error", - "title": "Cookie import failed", - "message": "", - } - RedisArchivist().set_message(key, message=message, expire=True) + 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, + "level": "error", + "title": "Cookie import failed", + "message": message_line, + } + RedisArchivist().set_message(key, message=message, expire=True) + def progress(request): # pylint: disable=unused-argument