From ed64b484ff60825789a6f182137c61e441e68de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 6 Jul 2025 18:29:00 +0200 Subject: [PATCH] [ytdl] use '__gdl_initialize' key to trigger init code --- gallery_dl/downloader/ytdl.py | 21 ++++++++++++--------- gallery_dl/ytdl.py | 3 ++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/gallery_dl/downloader/ytdl.py b/gallery_dl/downloader/ytdl.py index 6ac3ee08..8016f134 100644 --- a/gallery_dl/downloader/ytdl.py +++ b/gallery_dl/downloader/ytdl.py @@ -30,7 +30,7 @@ class YoutubeDLDownloader(DownloaderBase): } self.ytdl_instance = None - self.rate = None + self.rate_dyn = None self.forward_cookies = self.config("forward-cookies", True) self.progress = self.config("progress", 3.0) self.outtmpl = self.config("outtmpl") @@ -68,11 +68,13 @@ class YoutubeDLDownloader(DownloaderBase): for cookie in self.session.cookies: set_cookie(cookie) - if self.progress is not None and not ytdl_instance._progress_hooks: - ytdl_instance.add_progress_hook(self._progress_hook) + if "__gdl_initialize" in ytdl_instance.params: + del ytdl_instance.params["__gdl_initialize"] - if rl := ytdl_instance.params.pop("_gdl_ratelimit", False): - self.rate = rl + if self.progress is not None: + ytdl_instance.add_progress_hook(self._progress_hook) + if rlf := ytdl_instance.params.pop("__gdl_ratelimit_func", False): + self.rate_dyn = rlf info_dict = kwdict.pop("_ytdl_info_dict", None) if not info_dict: @@ -136,8 +138,9 @@ class YoutubeDLDownloader(DownloaderBase): pathfmt.temppath = "" return True - if self.rate is not None: - ytdl_instance.params["ratelimit"] = self.rate() + if self.rate_dyn is not None: + # static ratelimits are set in ytdl.construct_YoutubeDL + ytdl_instance.params["ratelimit"] = self.rate_dyn() self.out.start(pathfmt.path) if self.part: @@ -168,8 +171,8 @@ class YoutubeDLDownloader(DownloaderBase): self._set_outtmpl(ytdl_instance, pathfmt.realpath) for entry in info_dict["entries"]: - if self.rate is not None: - ytdl_instance.params["ratelimit"] = self.rate() + if self.rate_dyn is not None: + ytdl_instance.params["ratelimit"] = self.rate_dyn() ytdl_instance.process_info(entry) return True diff --git a/gallery_dl/ytdl.py b/gallery_dl/ytdl.py index f47c5020..14b09be4 100644 --- a/gallery_dl/ytdl.py +++ b/gallery_dl/ytdl.py @@ -58,7 +58,7 @@ def construct_YoutubeDL(module, obj, user_opts, system_opts=None): if rate: func = util.build_selection_func(rate, 0, text.parse_bytes) if hasattr(func, "args"): - opts["_gdl_ratelimit"] = func + opts["__gdl_ratelimit_func"] = func else: opts["ratelimit"] = func() or None else: @@ -72,6 +72,7 @@ def construct_YoutubeDL(module, obj, user_opts, system_opts=None): if system_opts: opts.update(system_opts) + opts["__gdl_initialize"] = True return module.YoutubeDL(opts)