From b43cd881015bdd4cb5c990281332f6c87cc0f63a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 12 Apr 2017 18:43:41 +0200 Subject: [PATCH] add '-j/--dump-json' option this outputs the extractor-results in JSON format rather then downloading files --- gallery_dl/__init__.py | 2 ++ gallery_dl/job.py | 35 ++++++++++++++++++++++++++++++++++- gallery_dl/option.py | 4 ++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index e3785d34..71715ae5 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -83,6 +83,8 @@ def main(): jobtype.maxdepth = args.list_urls elif args.list_keywords: jobtype = job.KeywordJob + elif args.list_data: + jobtype = job.DataJob else: jobtype = job.DownloadJob diff --git a/gallery_dl/job.py b/gallery_dl/job.py index 41da2ae7..7453305c 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -6,6 +6,7 @@ # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. +import sys import json import hashlib from . import extractor, downloader, config, util, output, exception @@ -79,7 +80,7 @@ class Job(): ) # TODO: support for multiple message versions - def handle_url(self, url, kexwords): + def handle_url(self, url, keywords): """Handle Message.Url""" def handle_directory(self, keywords): @@ -265,3 +266,35 @@ class TestJob(DownloadJob): """Update the content hash""" if self.content: self.get_downloader(url).download(url, self.fileobj) + + +class DataJob(Job): + """Collect extractor results and dump them""" + + def __init__(self, url, file=sys.stdout): + Job.__init__(self, url) + self.file = file + self.data = [] + self.ensure_ascii = config.get(("output", "ascii"), True) + + def run(self): + # collect data + try: + for msg in self.extractor: + if msg[0] in (Message.Headers, Message.Cookies): + copy = (msg[0], dict(msg[1])) + else: + copy = [ + part.copy() if hasattr(part, "copy") else part + for part in msg + ] + self.data.append(copy) + except Exception as exc: + self.data.append((exc.__class__.__name__, str(exc))) + + # dump to 'file' + json.dump( + self.data, self.file, + sort_keys=True, indent=2, ensure_ascii=self.ensure_ascii + ) + self.file.write("\n") diff --git a/gallery_dl/option.py b/gallery_dl/option.py index 5f40d953..9a87c9ad 100644 --- a/gallery_dl/option.py +++ b/gallery_dl/option.py @@ -63,6 +63,10 @@ def build_parser(): "-g", "--get-urls", dest="list_urls", action="count", help="Print download urls", ) + parser.add_argument( + "-j", "--dump-json", dest="list_data", action="store_true", + help="Print JSON information", + ) parser.add_argument( "-d", "--dest", metavar="DEST", action=ConfigAction, dest="base-directory",