support sending presets with api/yt-dlp/url/info

This commit is contained in:
ArabCoders 2025-03-10 03:56:10 +03:00
parent dcab2530ec
commit 643688bf97
2 changed files with 31 additions and 7 deletions

View file

@ -47,6 +47,7 @@ from .Utils import (
validate_url,
validate_uuid,
)
from .YTDLPOpts import YTDLPOpts
LOG = logging.getLogger("http_api")
MIME = magic.Magic(mime=True)
@ -471,10 +472,21 @@ class HttpAPI(Common):
except ValueError as e:
return web.json_response(data={"error": str(e)}, status=web.HTTPBadRequest.status_code)
try:
key = self.cache.hash(url)
preset = request.query.get("preset")
if preset:
exists = Presets.get_instance().get(name=preset)
if not exists:
return web.json_response(
data={"status": False, "message": f"Preset '{preset}' does not exist."},
status=web.HTTPBadRequest.status_code,
)
else:
preset = self.config.default_preset
if self.cache.has(key):
try:
key = self.cache.hash(url+str(preset))
if self.cache.has(key) and not request.query.get("force"):
data = self.cache.get(key)
data["_cached"] = {
"key": key,
@ -484,12 +496,19 @@ class HttpAPI(Common):
}
return web.json_response(data=data, status=web.HTTPOk.status_code, dumps=self.encoder.encode)
opts = {
"proxy": self.config.ytdl_options.get("proxy", None),
}
opts = {}
if self.config.ytdl_options.get("proxy", None):
opts["proxy"] = self.config.ytdl_options.get("proxy", None)
data = get_video_info(
url=url,
ytdlp_opts=YTDLPOpts.get_instance().preset(name=preset, with_cookies=True).add(opts).get_all(),
no_archive=True,
)
data = get_video_info(url=url, ytdlp_opts=opts, no_archive=True)
self.cache.set(key=self.cache.hash(url), value=data, ttl=300)
data["_cached"] = {
"key": self.cache.hash(url),
"ttl": 300,

View file

@ -58,6 +58,11 @@ def get_video_info(url: str, ytdlp_opts: dict | None = None, no_archive: bool =
if no_archive and "download_archive" in params:
del params["download_archive"]
# Remove keys that are not needed for info extraction.
keys_to_remove = [key for key in params if str(key).startswith("write") or key in ["postprocessors"]]
for key in keys_to_remove:
params.pop(key, None)
return yt_dlp.YoutubeDL(params=params).extract_info(url, download=False)