From 48a8717a7c988f49f457be31557fa72d94d9efb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 8 Oct 2018 20:28:54 +0200 Subject: [PATCH] add 'output.num-to-str' option ... to convert any numeric values to string when outputting them as JSON (during '--dump-json' or otherwise) --- gallery_dl/job.py | 11 +++++++++-- gallery_dl/util.py | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/gallery_dl/job.py b/gallery_dl/job.py index c198b886f6..6017f47a75 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -498,7 +498,6 @@ def __init__(self, url, parent=None, file=sys.stdout): Job.__init__(self, url, parent) self.file = file self.data = [] - self.ensure_ascii = config.get(("output", "ascii"), True) def run(self): # collect data @@ -510,10 +509,15 @@ def run(self): except BaseException: pass + if config.get(("output", "num-to-str"), False): + for msg in self.data: + util.transform_dict(msg[-1], util.number_to_string) + # dump to 'file' json.dump( self.data, self.file, - sort_keys=True, indent=2, ensure_ascii=self.ensure_ascii + sort_keys=True, indent=2, + ensure_ascii=config.get(("output", "ascii"), True), ) self.file.write("\n") @@ -528,3 +532,6 @@ def handle_directory(self, keywords): def handle_queue(self, url, keywords): self.data.append((Message.Queue, url, keywords.copy())) + + def handle_finalize(self): + self.file.close() diff --git a/gallery_dl/util.py b/gallery_dl/util.py index 7449288523..8993496799 100644 --- a/gallery_dl/util.py +++ b/gallery_dl/util.py @@ -57,7 +57,7 @@ def wrap(): def combine_dict(a, b): - """Recursively combine the contents of b into a""" + """Recursively combine the contents of 'b' into 'a'""" for key, value in b.items(): if key in a and isinstance(value, dict) and isinstance(a[key], dict): combine_dict(a[key], value) @@ -66,6 +66,20 @@ def combine_dict(a, b): return a +def transform_dict(a, func): + """Recursively apply 'func' to all values in 'a'""" + for key, value in a.items(): + if isinstance(value, dict): + transform_dict(value, func) + else: + a[key] = func(value) + + +def number_to_string(value): + """Convert numbers (int, float) to string; Return everything else as is.""" + return str(value) if isinstance(value, (int, float)) else value + + def expand_path(path): """Expand environment variables and tildes (~)""" if not path: