1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 10:42:34 +01:00

add '--print' and '--print-to-file' command-line options

This commit is contained in:
Mike Fährmann 2024-10-20 10:06:04 +02:00
parent 2e1dab3036
commit 10c56a561d
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 64 additions and 2 deletions

View File

@ -48,6 +48,11 @@
-K, --list-keywords Print a list of available keywords and example -K, --list-keywords Print a list of available keywords and example
values for the given URLs values for the given URLs
-e, --error-file FILE Add input URLs which returned an error to FILE -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-modules Print a list of available extractor modules
--list-extractors CATEGORIES --list-extractors CATEGORIES
Print a list of extractor classes with Print a list of extractor classes with
@ -153,7 +158,7 @@
current filename format to FORMAT current filename format to FORMAT
--ugoira FMT Convert Pixiv Ugoira to FMT using FFmpeg. --ugoira FMT Convert Pixiv Ugoira to FMT using FFmpeg.
Supported formats are 'webm', 'mp4', 'gif', 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 --exec CMD Execute CMD for each downloaded file. Supported
replacement fields are {} or {_path}, replacement fields are {} or {_path},
{_directory}, {_filename}. Example: --exec {_directory}, {_filename}. Example: --exec

View File

@ -10,6 +10,7 @@
import argparse import argparse
import logging import logging
import os.path
import sys import sys
from . import job, util, version from . import job, util, version
@ -152,6 +153,49 @@ class UgoiraAction(argparse.Action):
namespace.postprocessors.append(pp) 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): class Formatter(argparse.HelpFormatter):
"""Custom HelpFormatter class to customize help output""" """Custom HelpFormatter class to customize help output"""
def __init__(self, prog): def __init__(self, prog):
@ -342,6 +386,19 @@ def build_parser():
dest="errorfile", metavar="FILE", action=ConfigAction, dest="errorfile", metavar="FILE", action=ConfigAction,
help="Add input URLs which returned an error to FILE", 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( output.add_argument(
"--list-modules", "--list-modules",
dest="list_modules", action="store_true", dest="list_modules", action="store_true",
@ -616,7 +673,7 @@ def build_parser():
postprocessor = parser.add_argument_group("Post-processing Options") postprocessor = parser.add_argument_group("Post-processing Options")
postprocessor.add_argument( postprocessor.add_argument(
"-P", "--postprocessor", "-P", "--postprocessor",
dest="postprocessors", metavar="NAME", action="append", default=[], dest="postprocessors", metavar="NAME", action="append",
help="Activate the specified post processor", help="Activate the specified post processor",
) )
postprocessor.add_argument( postprocessor.add_argument(