parse yt-dlp header cookie for yt-dlp/url/info

This commit is contained in:
ArabCoders 2025-03-11 00:08:24 +03:00
parent 72bac24f46
commit c1939144c9
2 changed files with 36 additions and 0 deletions

View file

@ -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"] = {

View file

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