diff --git a/app/library/HttpAPI.py b/app/library/HttpAPI.py index f3a53d23..7287ef1b 100644 --- a/app/library/HttpAPI.py +++ b/app/library/HttpAPI.py @@ -44,6 +44,7 @@ from .Utils import ( get_file, get_mime_type, get_sidecar_subtitles, + parse_cookies, validate_url, validate_uuid, ) @@ -510,6 +511,15 @@ class HttpAPI(Common): follow_redirect=True, ) + if "formats" in data: + for index, item in enumerate(data["formats"]): + if "cookies" in item: + cookies = parse_cookies(item["cookies"]) + if len(cookies) > 0: + data["formats"][index]["h_cookies"] = "; ".join( + f"{key}={value}" for key, value in cookies.items() + ) + self.cache.set(key=key, value=data, ttl=300) data["_cached"] = { diff --git a/app/library/Utils.py b/app/library/Utils.py index 4bc4515f..a154a7ac 100644 --- a/app/library/Utils.py +++ b/app/library/Utils.py @@ -606,3 +606,29 @@ def decrypt_data(data: str, key: bytes) -> str: return plaintext.decode() except Exception: return None + + +def parse_cookies(cookie_str: str) -> dict: + """ + Parse a cookie string into a dictionary." + + Args: + cookie_str (str): The cookie string. + + Returns: + dict: The parsed cookies. + + """ + cookie_attributes = {"domain", "path", "expires", "secure", "httponly", "samesite"} + + tokens = cookie_str.split("; ") + + cookies = {} + + for token in tokens: + if "=" in token: + key, value = token.split("=", 1) + if str(key).lower() not in cookie_attributes: + cookies[key] = value + + return cookies