From 74d74d95a13f4d45e1acabea36284d47fb01a54e Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 23 Jun 2022 06:48:55 +0700 Subject: [PATCH] store validation result, return in API --- tubearchivist/api/README.md | 7 +++++-- tubearchivist/api/views.py | 6 ++++-- tubearchivist/home/src/download/yt_dlp_base.py | 17 +++++++++++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/tubearchivist/api/README.md b/tubearchivist/api/README.md index c155eac..a5bc1b0 100644 --- a/tubearchivist/api/README.md +++ b/tubearchivist/api/README.md @@ -233,11 +233,14 @@ List of valid task names: ## Cookie View -Check your youtube cookie settings +Check your youtube cookie settings, *status* turns to `true` if cookie has been validated. GET /api/cookie/ ```json { - "cookie_enabled": true + "cookie_enabled": true, + "status": true, + "validated": , + "validated_str": "timestamp" } ``` diff --git a/tubearchivist/api/views.py b/tubearchivist/api/views.py index 9604ca5..b99ab6a 100644 --- a/tubearchivist/api/views.py +++ b/tubearchivist/api/views.py @@ -488,9 +488,11 @@ class CookieView(ApiBaseView): """handle get request""" # pylint: disable=unused-argument config = AppConfig().config - cookie_enabled = config["downloads"]["cookie_import"] + valid = RedisArchivist().get_message("cookie:valid") + response = {"cookie_enabled": config["downloads"]["cookie_import"]} + response.update(valid) - return Response({"cookie_enabled": cookie_enabled}) + return Response(response) @staticmethod def post(request): diff --git a/tubearchivist/home/src/download/yt_dlp_base.py b/tubearchivist/home/src/download/yt_dlp_base.py index df929f5..089da10 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 datetime import datetime from http import cookiejar from io import StringIO @@ -115,7 +116,8 @@ class CookieHandler: "extract_flat": True, } validator = YtWrap(obs_request, self.config) - response = validator.extract("LL") + response = bool(validator.extract("LL")) + self.store_validation(response) # update in redis to avoid expiring modified = validator.obs["cookiefile"].getvalue() @@ -134,4 +136,15 @@ class CookieHandler: ) print("cookie validation failed, exiting...") - return bool(response) + return response + + @staticmethod + def store_validation(response): + """remember last validation""" + now = datetime.now() + message = { + "status": response, + "validated": int(now.strftime("%s")), + "validated_str": now.strftime("%Y-%m-%d %H:%M"), + } + RedisArchivist().set_message("cookie:valid", message)