From 2a32b12043d332eb6de4cc3bf6b36112300860bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Mon, 20 Feb 2017 22:02:49 +0100 Subject: [PATCH] add '--items' option this allows to specify which manga-chapters/comic-issues to download when using gallery-dl on a manga/comic URL --- gallery_dl/__init__.py | 9 ++++++++ gallery_dl/job.py | 7 +++++-- gallery_dl/util.py | 47 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 gallery_dl/util.py diff --git a/gallery_dl/__init__.py b/gallery_dl/__init__.py index 5a52207e..1f4bd70d 100644 --- a/gallery_dl/__init__.py +++ b/gallery_dl/__init__.py @@ -51,6 +51,13 @@ def build_cmdline_parser(): metavar="FILE", dest="inputfile", help="download URLs found in local FILE", ) + parser.add_argument( + "--items", + metavar="ITEM-SPEC", dest="items", + help=("specify which items to download through a comma seperated list " + "of indices or index-ranges; for example '--items -2,4,6-8,10-' " + "will download items 1, 2, 4, 6, 7, 8 and 10 up to the last one") + ) parser.add_argument( "-c", "--config", metavar="CFG", dest="cfgfiles", action="append", @@ -120,6 +127,8 @@ def main(): config.set(("username",), args.username) if args.password: config.set(("password",), args.password) + if args.items: + config.set(("items",), args.items) for opt in args.option: parse_option(opt) diff --git a/gallery_dl/job.py b/gallery_dl/job.py index 105feda0..8fac59a0 100644 --- a/gallery_dl/job.py +++ b/gallery_dl/job.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2015, 2016 Mike Fährmann +# Copyright 2015-2017 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 @@ -8,7 +8,7 @@ import json import hashlib -from . import extractor, downloader, path, output, exception +from . import extractor, downloader, config, util, path, output, exception from .extractor.message import Message @@ -81,6 +81,9 @@ class DownloadJob(Job): def run(self): Job.run(self) if self.queue: + itemspec = config.get(("items",)) + if itemspec: + self.queue = util.apply_range(self.queue, str(itemspec)) for url in self.queue: try: DownloadJob(url).run() diff --git a/gallery_dl/util.py b/gallery_dl/util.py new file mode 100644 index 00000000..86151802 --- /dev/null +++ b/gallery_dl/util.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- + +# Copyright 2017 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. + +"""Utility functions""" + + +def apply_range(iterable, rangespec): + """Return a new iterable containing only the items specified in the given + integer range + """ + try: + maxval = len(iterable) + except TypeError: + maxval = 0 + rset = parse_range(rangespec, maxval) + return ( + item + for index, item in enumerate(iterable, 1) + if index in rset + ) + + +def parse_range(rangespec, maxval=0): + """Parse an integer range and return the resulting set + + Examples + parse_range("-2,4,6-8,10-", 12) -> set(1, 2, 4, 6, 7, 8, 10, 11, 12) + parse_range(" - 3 , 4- 4, 6-2") -> set(1, 2, 3, 4) + """ + result = set() + for group in rangespec.split(","): + parts = group.split("-", maxsplit=1) + try: + if len(parts) == 1: + result.add(int(parts[0])) + else: + beg = int(parts[0]) if parts[0].strip() else 1 + end = int(parts[1]) if parts[1].strip() else maxval + result.update(range(beg, end+1)) + except ValueError: + pass + return result