From c20fdca09bc230c901d1b7c8cac73200e9e96eef Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Sat, 11 Jan 2025 20:57:39 +0300 Subject: [PATCH] Added way to check for youtube cookies status --- app/library/HttpAPI.py | 52 ++++++++++++++++++++++++++++++++++++++++ app/library/config.py | 6 ++++- ui/layouts/default.vue | 29 ++++++++++++++++++++++ ui/stores/ConfigStore.js | 1 + 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/app/library/HttpAPI.py b/app/library/HttpAPI.py index b07cbdfa..d4117031 100644 --- a/app/library/HttpAPI.py +++ b/app/library/HttpAPI.py @@ -706,3 +706,55 @@ class HttpAPI(common): ) except Exception as e: return web.json_response(data={"error": str(e)}, status=web.HTTPInternalServerError.status_code) + + @route("GET", "api/youtube/auth") + async def is_authenticated(self, request: Request) -> Response: + cookie_file = self.config.ytdl_options.get("cookiefile", None) + if not cookie_file: + return web.json_response(data={"message": "No cookie file provided."}, status=web.HTTPForbidden.status_code) + + cookie_file = os.path.realpath(cookie_file) + if not os.path.exists(cookie_file): + return web.json_response( + data={"message": f"Cookie file '{cookie_file}' does not exist."}, status=web.HTTPNotFound.status_code + ) + + try: + import http.cookiejar + + cookies = http.cookiejar.MozillaCookieJar(cookie_file, None, None) + cookies.load() + except Exception as e: + LOG.error(f"failed to load cookies from '{cookie_file}'. '{e}'.") + LOG.exception(e) + return web.json_response( + data={"message": "Failed to load cookies"}, + status=web.HTTPInternalServerError.status_code, + ) + + url = "https://www.youtube.com/account" + + try: + opts = { + "proxy": self.config.ytdl_options.get("proxy", None), + "headers": { + "User-Agent": self.config.ytdl_options.get( + "user_agent", request.headers.get("User-Agent", f"YTPTube/{self.config.version}") + ), + }, + "cookies": cookies, + } + async with httpx.AsyncClient(**opts) as client: + LOG.debug(f"Checking '{url}' redirection.") + response = await client.request(method="GET", url=url, follow_redirects=False) + return web.json_response( + data={"message": "Authenticated." if response.status_code == 200 else "Not authenticated."}, + status=200 if response.status_code == 200 else 401, + ) + except Exception as e: + LOG.error(f"Failed to request '{url}'. '{e}'.") + LOG.exception(e) + return web.json_response( + data={"message": f"Failed to request website. {str(e)}"}, + status=web.HTTPInternalServerError.status_code, + ) diff --git a/app/library/config.py b/app/library/config.py index 980d4cc3..3fb7ee7e 100644 --- a/app/library/config.py +++ b/app/library/config.py @@ -310,4 +310,8 @@ class Config: dict: A dictionary with the frontend configuration """ - return {k: getattr(self, k) for k in self._frontend_vars} + data = {k: getattr(self, k) for k in self._frontend_vars} + hasCookies = self.ytdl_options.get("cookiefile", None) + data["has_cookies"] = hasCookies is not None and os.path.exists(hasCookies) + + return data diff --git a/ui/layouts/default.vue b/ui/layouts/default.vue index 0312d741..3233ca18 100644 --- a/ui/layouts/default.vue +++ b/ui/layouts/default.vue @@ -12,6 +12,13 @@