From 643688bf97a7c0bd0df7288bbbd8308a8108ec2b Mon Sep 17 00:00:00 2001 From: ArabCoders Date: Mon, 10 Mar 2025 03:56:10 +0300 Subject: [PATCH] support sending presets with api/yt-dlp/url/info --- app/library/HttpAPI.py | 33 ++++++++++++++++++++++++++------- app/library/Utils.py | 5 +++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/app/library/HttpAPI.py b/app/library/HttpAPI.py index 5cf0e176..3e8096a7 100644 --- a/app/library/HttpAPI.py +++ b/app/library/HttpAPI.py @@ -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, diff --git a/app/library/Utils.py b/app/library/Utils.py index dc73d4aa..cd3e1ead 100644 --- a/app/library/Utils.py +++ b/app/library/Utils.py @@ -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)