From 10c56a561d6201e7ce9b076fc09be6b71e565ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sun, 20 Oct 2024 10:06:04 +0200 Subject: [PATCH] add '--print' and '--print-to-file' command-line options --- docs/options.md | 7 +++++- gallery_dl/option.py | 59 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/docs/options.md b/docs/options.md index 358dd6c4..ffbe1d1f 100644 --- a/docs/options.md +++ b/docs/options.md @@ -48,6 +48,11 @@ -K, --list-keywords Print a list of available keywords and example values for the given URLs -e, --error-file FILE Add input URLs which returned an error to FILE + -N, --print [EVENT:]FORMAT Write FORMAT during EVENT (default 'prepare') to + standard output. Examples: 'id' or + 'post:{md5[:8]}' + --print-to-file [EVENT:]FORMAT FILE + Append FORMAT during EVENT to FILE --list-modules Print a list of available extractor modules --list-extractors CATEGORIES Print a list of extractor classes with @@ -153,7 +158,7 @@ current filename format to FORMAT --ugoira FMT Convert Pixiv Ugoira to FMT using FFmpeg. Supported formats are 'webm', 'mp4', 'gif', - 'vp8', 'vp9', 'vp9-lossless', 'copy'. + 'vp8', 'vp9', 'vp9-lossless', 'copy', 'zip'. --exec CMD Execute CMD for each downloaded file. Supported replacement fields are {} or {_path}, {_directory}, {_filename}. Example: --exec diff --git a/gallery_dl/option.py b/gallery_dl/option.py index c4f5b940..b38ad743 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -10,6 +10,7 @@ import argparse import logging +import os.path import sys from . import job, util, version @@ -152,6 +153,49 @@ class UgoiraAction(argparse.Action): namespace.postprocessors.append(pp) +class PrintAction(argparse.Action): + def __call__(self, parser, namespace, value, option_string=None): + if self.const: + filename = self.const + base = None + mode = "w" + else: + value, path = value + base, filename = os.path.split(path) + mode = "a" + + event, sep, format_string = value.partition(":") + if not sep: + format_string = event + event = ("prepare",) + else: + event = event.strip().lower() + if event not in {"init", "file", "after", "skip", "error", + "prepare", "prepare-after", "post", "post-after", + "finalize", "finalize-success", "finalize-error"}: + format_string = value + event = ("prepare",) + + if not format_string: + return + + if "{" not in format_string and \ + " " not in format_string and \ + format_string[0] != "\f": + format_string = "{" + format_string + "}" + if format_string[-1] != "\n": + format_string += "\n" + + namespace.postprocessors.append({ + "name" : "metadata", + "event" : event, + "filename" : filename, + "base-directory": base or ".", + "content-format": format_string, + "open" : mode, + }) + + class Formatter(argparse.HelpFormatter): """Custom HelpFormatter class to customize help output""" def __init__(self, prog): @@ -342,6 +386,19 @@ def build_parser(): dest="errorfile", metavar="FILE", action=ConfigAction, help="Add input URLs which returned an error to FILE", ) + output.add_argument( + "-N", "--print", + dest="postprocessors", metavar="[EVENT:]FORMAT", + action=PrintAction, const="-", default=[], + help=("Write FORMAT during EVENT (default 'prepare') to standard " + "output. Examples: 'id' or 'post:{md5[:8]}'"), + ) + output.add_argument( + "--print-to-file", + dest="postprocessors", metavar="[EVENT:]FORMAT FILE", + action=PrintAction, nargs=2, + help="Append FORMAT during EVENT to FILE", + ) output.add_argument( "--list-modules", dest="list_modules", action="store_true", @@ -616,7 +673,7 @@ def build_parser(): postprocessor = parser.add_argument_group("Post-processing Options") postprocessor.add_argument( "-P", "--postprocessor", - dest="postprocessors", metavar="NAME", action="append", default=[], + dest="postprocessors", metavar="NAME", action="append", help="Activate the specified post processor", ) postprocessor.add_argument(