From e622e004f0f7a7dc0b8533389f4c63c22fbd61a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Tue, 13 Jul 2021 16:59:55 +0200 Subject: [PATCH] [ytdl] improve module imports (#1680) Apply 'extractor.ytdl.module' for every URL, not just the first. --- gallery_dl/downloader/ytdl.py | 6 +----- gallery_dl/extractor/ytdl.py | 30 ++++++++++++------------------ 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/gallery_dl/downloader/ytdl.py b/gallery_dl/downloader/ytdl.py index 704d8702..9a55dc91 100644 --- a/gallery_dl/downloader/ytdl.py +++ b/gallery_dl/downloader/ytdl.py @@ -15,13 +15,9 @@ import os class YoutubeDLDownloader(DownloaderBase): scheme = "ytdl" - module = None def __init__(self, job): - module = self.module - if not module: - module_name = self.config("module") or "youtube_dl" - module = YoutubeDLDownloader.module = __import__(module_name) + module = __import__(self.config("module") or "youtube_dl") DownloaderBase.__init__(self, job) extractor = job.extractor diff --git a/gallery_dl/extractor/ytdl.py b/gallery_dl/extractor/ytdl.py index 68f3b7ea..394caf48 100644 --- a/gallery_dl/extractor/ytdl.py +++ b/gallery_dl/extractor/ytdl.py @@ -20,43 +20,37 @@ class YoutubeDLExtractor(Extractor): archive_fmt = "{extractor_key} {id}" pattern = r"ytdl:(.*)" test = ("ytdl:https://www.youtube.com/watch?v=BaW_jenozKc&t=1s&end=9",) - ytdl_module = None - ytdl_module_name = None def __init__(self, match): # import main youtube_dl module - module = self.ytdl_module - if not module: - name = YoutubeDLExtractor.ytdl_module_name = config.get( - ("extractor", "ytdl"), "module") or "youtube_dl" - module = YoutubeDLExtractor.ytdl_module = __import__(name) + module_name = self.ytdl_module_name = config.get( + ("extractor", "ytdl"), "module") or "youtube_dl" + module = __import__(module_name) # find suitable youtube_dl extractor self.ytdl_url = url = match.group(1) generic = config.interpolate(("extractor", "ytdl"), "generic", True) if generic == "force": - self.ytdl_ie = ie = module.extractor.GenericIE + self.ytdl_ie_key = "Generic" self.force_generic_extractor = True else: for ie in module.extractor.gen_extractor_classes(): if ie.suitable(url): - self.ytdl_ie = ie + self.ytdl_ie_key = ie.ie_key() break - if not generic and ie == module.extractor.GenericIE: + if not generic and self.ytdl_ie_key == "Generic": raise exception.NoExtractorError() self.force_generic_extractor = False # set subcategory to youtube_dl extractor's key - self.subcategory = ie.ie_key() + self.subcategory = self.ytdl_ie_key Extractor.__init__(self, match) def items(self): - # check subcategory module; import and use if needed - name = config.get(("extractor", "ytdl", self.subcategory), "module") - if name and name != self.ytdl_module_name: - ytdl_module = __import__(name) - else: - ytdl_module = self.ytdl_module + # import subcategory module + ytdl_module = __import__( + config.get(("extractor", "ytdl", self.subcategory), "module") or + self.ytdl_module_name) self.log.debug("Using %s", ytdl_module) # construct YoutubeDL object @@ -92,7 +86,7 @@ class YoutubeDLExtractor(Extractor): # extract youtube_dl info_dict info_dict = ytdl._YoutubeDL__extract_info( self.ytdl_url, - ytdl.get_info_extractor(self.ytdl_ie.ie_key()), + ytdl.get_info_extractor(self.ytdl_ie_key), False, {}, True) if "entries" in info_dict: