From 8aba2bdebf99f8aacab0d597e1c4a708e126e5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Thu, 17 Jan 2019 21:18:12 +0100 Subject: [PATCH] [postprocessor:metadata] add 'tags' and 'custom' modes (#135) --- docs/configuration.rst | 38 ++++++++++++++++++ gallery_dl/postprocessor/metadata.py | 60 +++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index 6b9a75a6..cdd057df 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -1083,6 +1083,44 @@ Description The command to run. =========== ===== +metadata +-------- + +Write image metadata to separate files + +metadata.mode +------------- +=========== ===== +Type ``string`` +Default ``"json"`` +Description Select how to write metadata. + + - ``"json"``: all metadata using `json.dump() + `_ + - ``"tags"``: ``tags`` separated by newlines + - ``"custom"``: result of applying `metadata.format`_ to a file's + metadata dictionary +=========== ===== + +metadata.extension +------------------ +=========== ===== +Type ``string`` +Default ``"json"`` or ``"txt"`` +Description Filename extension for metadata files. +=========== ===== + +metadata.format +--------------- +=========== ===== +Type ``string`` +Example ``"tags:\n\n{tags:J\n}\n"`` +Description Custom format string to build content of metadata files. + + Note: Only applies for ``"mode": "custom"``. +=========== ===== + + ugoira ------ diff --git a/gallery_dl/postprocessor/metadata.py b/gallery_dl/postprocessor/metadata.py index 658b3bf4..3aae9431 100644 --- a/gallery_dl/postprocessor/metadata.py +++ b/gallery_dl/postprocessor/metadata.py @@ -9,6 +9,7 @@ """Write metadata to JSON files""" from .common import PostProcessor +from .. import util import json @@ -16,18 +17,57 @@ class MetadataPP(PostProcessor): def __init__(self, pathfmt, options): PostProcessor.__init__(self) - self.indent = options.get("indent", 4) - self.ascii = options.get("ascii", False) + + mode = options.get("mode", "json") + ext = "txt" + + if mode == "custom": + self.write = self._write_custom + self.formatter = util.Formatter(options.get("format")) + elif mode == "tags": + self.write = self._write_tags + else: + self.write = self._write_json + self.indent = options.get("indent", 4) + self.ascii = options.get("ascii", False) + ext = "json" + + self.extension = options.get("extension", ext) def run(self, pathfmt): - with open(pathfmt.realpath + ".json", "w") as file: - json.dump( - pathfmt.keywords, - file, - sort_keys=True, - indent=self.indent, - ensure_ascii=self.ascii, - ) + path = "{}.{}".format(pathfmt.realpath, self.extension) + with open(path, "w", encoding="utf-8") as file: + self.write(file, pathfmt) + + def _write_custom(self, file, pathfmt): + output = self.formatter.format_map(pathfmt.keywords) + file.write(output) + + def _write_tags(self, file, pathfmt): + kwds = pathfmt.keywords + tags = kwds.get("tags") or kwds.get("tag_string") + + if not tags: + return + + if not isinstance(tags, list): + for separator in (" :: ", ", ", " "): + taglist = tags.split(separator) + if len(taglist) >= len(tags) / 16: + break + tags = taglist + + file.write("\n".join(tags)) + file.write("\n") + + def _write_json(self, file, pathfmt): + json.dump( + pathfmt.keywords, + file, + sort_keys=True, + indent=self.indent, + ensure_ascii=self.ascii, + ) __postprocessor__ = MetadataPP