From 0b393304d1dc3b85810e7f7bde1d85b0e3d36443 Mon Sep 17 00:00:00 2001 From: Simon Date: Tue, 28 Jan 2025 14:57:10 +0700 Subject: [PATCH] cache cookie validation --- backend/appsettings/views.py | 7 +++++-- backend/download/src/yt_dlp_base.py | 16 +++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/backend/appsettings/views.py b/backend/appsettings/views.py index d1ccd972..95791a39 100644 --- a/backend/appsettings/views.py +++ b/backend/appsettings/views.py @@ -5,6 +5,7 @@ from appsettings.src.config import AppConfig from appsettings.src.snapshot import ElasticSnapshot from common.src.ta_redis import RedisArchivist from common.views_base import AdminOnly, ApiBaseView +from django.conf import settings from download.src.yt_dlp_base import CookieHandler, POTokenHandler from rest_framework.authtoken.models import Token from rest_framework.response import Response @@ -220,13 +221,15 @@ class CookieView(ApiBaseView): print(message) return Response({"message": message}, status=400) - print(f"cookie preview:\n\n{cookie[:300]}") + if settings.DEBUG: + print(f"[cookie] preview:\n\n{cookie[:300]}") + handler = CookieHandler(config) handler.set_cookie(cookie) validated = handler.validate() if not validated: handler.revoke() - print("cookie import failed, not valid") + print("[cookie]: import failed, not valid") status = 400 else: status = 200 diff --git a/backend/download/src/yt_dlp_base.py b/backend/download/src/yt_dlp_base.py index 6de7fb4a..a18e5ef0 100644 --- a/backend/download/src/yt_dlp_base.py +++ b/backend/download/src/yt_dlp_base.py @@ -134,7 +134,7 @@ class CookieHandler: RedisArchivist().set_message("cookie", cookie, save=True) AppConfig().update_config({"downloads.cookie_import": True}) self.config["downloads"]["cookie_import"] = True - print("cookie: activated and stored in Redis") + print("[cookie]: activated and stored in Redis") @staticmethod def revoke(): @@ -142,11 +142,16 @@ class CookieHandler: RedisArchivist().del_message("cookie") RedisArchivist().del_message("cookie:valid") AppConfig().update_config({"downloads.cookie_import": False}) - print("cookie: revoked") + print("[cookie]: revoked") def validate(self): """validate cookie using the liked videos playlist""" - print("validating cookie") + validation = RedisArchivist().get_message_dict("cookie:valid") + if validation: + print("[cookie]: used cached cookie validation") + return True + + print("[cookie] validating cookie") obs_request = { "skip_download": True, "extract_flat": True, @@ -170,8 +175,9 @@ class CookieHandler: RedisArchivist().set_message( "message:download", mess_dict, expire=4 ) - print("cookie validation failed, exiting...") + print("[cookie]: validation failed, exiting...") + print(f"[cookie]: validation success: {response}") return response @staticmethod @@ -183,7 +189,7 @@ class CookieHandler: "validated": int(now.timestamp()), "validated_str": now.strftime("%Y-%m-%d %H:%M"), } - RedisArchivist().set_message("cookie:valid", message) + RedisArchivist().set_message("cookie:valid", message, expire=3600) class POTokenHandler: