From fa144f38ede2f6e48c7d2d8552dc212d6ff71712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 4 Jan 2023 21:42:22 +0100 Subject: [PATCH] [ytdl} fix dfe4f00c for legacy yt-dlp --- gallery_dl/ytdl.py | 31 ++++++++++++++++++++----------- test/test_ytdl.py | 18 ++++++++++++------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/gallery_dl/ytdl.py b/gallery_dl/ytdl.py index da86bbca..7b713492 100644 --- a/gallery_dl/ytdl.py +++ b/gallery_dl/ytdl.py @@ -199,18 +199,27 @@ def parse_command_line(module, argv): action += args yield action - if opts.metafromtitle is not None: - if "pre_process" not in opts.parse_metadata: - opts.parse_metadata["pre_process"] = [] - opts.parse_metadata["pre_process"].append( - "title:%s" % opts.metafromtitle) - opts.metafromtitle = None + parse_metadata = getattr(opts, "parse_metadata", None) + if isinstance(parse_metadata, dict): + if opts.metafromtitle is not None: + if "pre_process" not in parse_metadata: + parse_metadata["pre_process"] = [] + parse_metadata["pre_process"].append( + "title:%s" % opts.metafromtitle) + opts.parse_metadata = { + k: list(itertools.chain.from_iterable(map( + metadataparser_actions, v))) + for k, v in parse_metadata.items() + } + else: + if parse_metadata is None: + parse_metadata = [] + if opts.metafromtitle is not None: + parse_metadata.append("title:%s" % opts.metafromtitle) + opts.parse_metadata = list(itertools.chain.from_iterable(map( + metadataparser_actions, parse_metadata))) - opts.parse_metadata = { - k: list(itertools.chain.from_iterable(map( - metadataparser_actions, v))) - for k, v in opts.parse_metadata.items() - } + opts.metafromtitle = None else: opts.parse_metadata = () diff --git a/test/test_ytdl.py b/test/test_ytdl.py index e7dce526..a2736042 100644 --- a/test/test_ytdl.py +++ b/test/test_ytdl.py @@ -262,15 +262,21 @@ class Test_CommandlineArguments_YtDlp(Test_CommandlineArguments): def test_metadata_from_title(self): opts = self._(["--metadata-from-title", "%(artist)s - %(title)s"]) + + try: + legacy = (self.module.version.__version__ < "2023.01.01") + except AttributeError: + legacy = True + + actions = [self.module.MetadataFromFieldPP.to_action( + "title:%(artist)s - %(title)s")] + if not legacy: + actions = {"pre_process": actions} + self.assertEqual(opts["postprocessors"][0], { "key" : "MetadataParser", "when" : "pre_process", - "actions": { - "pre_process": [ - self.module.MetadataFromFieldPP.to_action( - "title:%(artist)s - %(title)s") - ], - }, + "actions": actions, })