From 2ad093a9a8722a0dd4766e56932f752e20a40552 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 15 Jun 2022 10:39:48 +0700 Subject: [PATCH] handle cookiejar.LoadError dont import invalid cookie --- .../home/src/download/yt_dlp_base.py | 6 +++- tubearchivist/home/views.py | 32 +++++++++++++++---- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/tubearchivist/home/src/download/yt_dlp_base.py b/tubearchivist/home/src/download/yt_dlp_base.py index 260b955..458241c 100644 --- a/tubearchivist/home/src/download/yt_dlp_base.py +++ b/tubearchivist/home/src/download/yt_dlp_base.py @@ -5,6 +5,7 @@ functionality: """ import os +from http import cookiejar from io import StringIO import yt_dlp @@ -54,9 +55,12 @@ class YtWrap: """make extract request""" try: response = yt_dlp.YoutubeDL(self.obs).extract_info(url) + except cookiejar.LoadError: + print("cookie file is invalid") + return False except (yt_dlp.utils.ExtractorError, yt_dlp.utils.DownloadError): print(f"{url}: failed to get info from youtube") - response = False + return False return response diff --git a/tubearchivist/home/views.py b/tubearchivist/home/views.py index 30738d2..3a38f92 100644 --- a/tubearchivist/home/views.py +++ b/tubearchivist/home/views.py @@ -829,18 +829,38 @@ class SettingsView(View): sleep(1) return redirect("settings", permanent=True) - @staticmethod - def post_process_updated(updated, config): + def post_process_updated(self, updated, config): """apply changes for config""" if not updated: return for config_value, updated_value in updated: if config_value == "cookie_import": - if updated_value: - CookieHandler(config).import_cookie() - else: - CookieHandler(config).revoke() + self.process_cookie(config, updated_value) + + @staticmethod + def process_cookie(config, updated_value): + """import and validate cookie""" + handler = CookieHandler(config) + if updated_value: + handler.import_cookie() + valid = handler.validate() + if not valid: + RedisArchivist().set_message( + "config", False, path=".downloads.cookie_import" + ) + handler.revoke() + message = { + "status": "message:setting", + "level": "error", + "title": "Cookie import failed", + "message": "", + } + RedisArchivist().set_message( + "message:setting", message=message + ) + else: + handler.revoke() def progress(request):