Files
gallery-dl/gallery_dl/postprocessor/metadata.py
Mike Fährmann 523ebc9b0b Fix serialization of 'datetime' objects in '--write-metadata'
Simplified universal serialization support in json.dump() can be achieved
by passing 'default=str', which was already the case in DataJob.run()
for -j/--dump-json, but not for the 'metadata' post-processor.

This commit introduces util.dump_json() that (more or less) unifies the
JSON output procedure of both --write-metadata and --dump-json.

(#251, #252)
2019-05-09 16:49:22 +02:00

67 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2019 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Write metadata to JSON files"""
from .common import PostProcessor
from .. import util
class MetadataPP(PostProcessor):
def __init__(self, pathfmt, options):
PostProcessor.__init__(self)
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):
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):
util.dump_json(pathfmt.keywords, file, self.ascii, self.indent)
__postprocessor__ = MetadataPP