cache cookie validation

This commit is contained in:
Simon
2025-01-28 14:57:10 +07:00
parent 074311339a
commit 0b393304d1
2 changed files with 16 additions and 7 deletions

View File

@@ -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

View File

@@ -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: