From 69cde97c93495fd924cffe78d9330a6f841db85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Sat, 10 Dec 2016 00:01:00 +0100 Subject: [PATCH] add utility extractor that runs test-URLs --- gallery_dl/extractor/__init__.py | 1 + gallery_dl/extractor/test.py | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 gallery_dl/extractor/test.py diff --git a/gallery_dl/extractor/__init__.py b/gallery_dl/extractor/__init__.py index 3ca22311..bb6b8db3 100644 --- a/gallery_dl/extractor/__init__.py +++ b/gallery_dl/extractor/__init__.py @@ -65,6 +65,7 @@ modules = [ "yandere", "imagehosts", "recursive", + "test", ] def find(url): diff --git a/gallery_dl/extractor/test.py b/gallery_dl/extractor/test.py new file mode 100644 index 00000000..48b5333c --- /dev/null +++ b/gallery_dl/extractor/test.py @@ -0,0 +1,69 @@ +# -*- coding: utf-8 -*- + +# Copyright 2016 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 extractor to execute tests of other extractors""" + +from .common import Extractor, Message +from .. import extractor, exception + +class TestExtractor(Extractor): + """Extractor to select and run the test URLs of other extractors + + Examples: + - apply all test URLs of all pixiv extractors: + test:pixiv + + - apply all test URLs of the PixivWorkExtractor: + test:pixiv:work + + - apply only the first test URL of the PixivWorkExtractor: + test:pixiv:work:0 + + - apply all second test URLs of all pixiv extractors: + test:pixiv:*:1 + """ + category = "test" + pattern = [r"t(?:est)?:([^:]+)(?::([^:]*)(?::(\*|\d*))?)?$"] + + def __init__(self, match): + Extractor.__init__(self) + self.tcategory, self.tsubcategory, self.tindex = match.groups() + + def items(self): + # get all extractor classes matching the category + klasses = [ + klass for klass in extractor.extractors() + if klass.category == self.tcategory + ] + + # if a subcategory is given, find the respective class + if self.tsubcategory and self.tsubcategory != "*": + for klass in klasses: + if klass.subcategory == self.tsubcategory: + klasses = (klass,) + break + else: + raise exception.NotFoundError("test") + + # if an index is given, only consider tests at this array position + if self.tindex and self.tindex != "*": + index = int(self.tindex) + tests = [ + klass.test[index] + for klass in klasses + if len(klass.test) > index + ] + else: + tests = [test for klass in klasses for test in klass.test] + + if not tests: + raise exception.NotFoundError("test") + + yield Message.Version, 1 + for test in tests: + yield Message.Queue, test[0]