updated yt-dlp/url/info to give cookies in valid way.

This commit is contained in:
ArabCoders 2025-03-11 02:08:13 +03:00
parent cd977ea28d
commit 1aeb8efa1e
2 changed files with 14 additions and 37 deletions

View file

@ -18,6 +18,7 @@ import httpx
import magic import magic
from aiohttp import web from aiohttp import web
from aiohttp.web import Request, RequestHandler, Response from aiohttp.web import Request, RequestHandler, Response
from yt_dlp.cookies import LenientSimpleCookie
from .cache import Cache from .cache import Cache
from .common import Common from .common import Common
@ -44,7 +45,6 @@ from .Utils import (
get_file, get_file,
get_mime_type, get_mime_type,
get_sidecar_subtitles, get_sidecar_subtitles,
parse_cookies,
validate_url, validate_url,
validate_uuid, validate_uuid,
) )
@ -496,15 +496,17 @@ class HttpAPI(Common):
"ttl_left": data.get("_cached", {}).get("expires", time.time() + 300) - time.time(), "ttl_left": data.get("_cached", {}).get("expires", time.time() + 300) - time.time(),
"expires": data.get("_cached", {}).get("expires", time.time() + 300), "expires": data.get("_cached", {}).get("expires", time.time() + 300),
} }
return web.json_response(data=data, status=web.HTTPOk.status_code, dumps=self.encoder.encode) return web.Response(body=json.dumps(data, indent=4), status=web.HTTPOk.status_code)
opts = {} opts = {}
if self.config.ytdl_options.get("proxy", None): if self.config.ytdl_options.get("proxy", None):
opts["proxy"] = self.config.ytdl_options.get("proxy", None) opts["proxy"] = self.config.ytdl_options.get("proxy", None)
ytdlp_opts = YTDLPOpts.get_instance().preset(name=preset, with_cookies=True).add(opts).get_all()
data = extract_info( data = extract_info(
config=YTDLPOpts.get_instance().preset(name=preset, with_cookies=True).add(opts).get_all(), config=ytdlp_opts,
url=url, url=url,
debug=False, debug=False,
no_archive=True, no_archive=True,
@ -513,14 +515,11 @@ class HttpAPI(Common):
if "formats" in data: if "formats" in data:
for index, item in enumerate(data["formats"]): for index, item in enumerate(data["formats"]):
if "cookies" in item: if "cookies" in item and len(item["cookies"]) > 0:
cookies = parse_cookies(item["cookies"]) cookies = [f"{c.key}={c.value}" for c in LenientSimpleCookie(item["cookies"]).values()]
if len(cookies) > 0: if len(cookies) > 0:
data["formats"][index]["h_cookies"] = "; ".join( data["formats"][index]["h_cookies"] = "; ".join(cookies)
f"{key}={value}" for key, value in cookies.items() data["formats"][index]["h_cookies"] = data["formats"][index]["h_cookies"].strip()
)
self.cache.set(key=key, value=data, ttl=300)
data["_cached"] = { data["_cached"] = {
"status": "miss", "status": "miss",
@ -530,7 +529,9 @@ class HttpAPI(Common):
"expires": time.time() + 300, "expires": time.time() + 300,
} }
return web.json_response(data=data, status=web.HTTPOk.status_code, dumps=self.encoder.encode) self.cache.set(key=key, value=data, ttl=300)
return web.Response(body=json.dumps(data, indent=4), status=web.HTTPOk.status_code)
except Exception as e: except Exception as e:
LOG.error(f"Error encountered while grabbing video info '{url}'. '{e}'.") LOG.error(f"Error encountered while grabbing video info '{url}'. '{e}'.")
LOG.exception(e) LOG.exception(e)

View file

@ -76,6 +76,8 @@ def extract_info(
config (dict): Configuration options. config (dict): Configuration options.
url (str): URL to extract information from. url (str): URL to extract information from.
debug (bool): Enable debug logging. debug (bool): Enable debug logging.
no_archive (bool): Disable download archive.
follow_redirect (bool): Follow URL redirects.
Returns: Returns:
dict: Video information. dict: Video information.
@ -606,29 +608,3 @@ def decrypt_data(data: str, key: bytes) -> str:
return plaintext.decode() return plaintext.decode()
except Exception: except Exception:
return None 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