2018-03-24 17:24:34 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-19 14:28:59 +01:00
|
|
|
# Copyright 2018-2019 Mike Fährmann
|
2018-03-24 17:24:34 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import unittest
|
2018-08-15 20:39:13 +02:00
|
|
|
import string
|
2018-03-24 17:24:34 +01:00
|
|
|
|
2019-02-06 17:24:44 +01:00
|
|
|
from gallery_dl import extractor
|
2018-03-24 17:24:34 +01:00
|
|
|
from gallery_dl.extractor.common import Extractor, Message
|
|
|
|
from gallery_dl.extractor.directlink import DirectlinkExtractor as DLExtractor
|
|
|
|
|
|
|
|
|
|
|
|
class FakeExtractor(Extractor):
|
|
|
|
category = "fake"
|
|
|
|
subcategory = "test"
|
2019-02-08 13:45:40 +01:00
|
|
|
pattern = "fake:"
|
2018-03-24 17:24:34 +01:00
|
|
|
|
|
|
|
def items(self):
|
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Url, "text:foobar", {}
|
|
|
|
|
|
|
|
|
|
|
|
class TestExtractor(unittest.TestCase):
|
2019-02-09 14:39:38 +01:00
|
|
|
VALID_URIS = (
|
|
|
|
"https://example.org/file.jpg",
|
|
|
|
"tumblr:foobar",
|
|
|
|
"oauth:flickr",
|
|
|
|
"test:pixiv:",
|
|
|
|
"recursive:https://example.org/document.html",
|
|
|
|
)
|
2018-03-24 17:24:34 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
extractor._cache.clear()
|
|
|
|
extractor._module_iter = iter(extractor.modules)
|
|
|
|
|
|
|
|
def test_find(self):
|
2019-02-09 14:39:38 +01:00
|
|
|
for uri in self.VALID_URIS:
|
2018-03-24 17:24:34 +01:00
|
|
|
result = extractor.find(uri)
|
|
|
|
self.assertIsInstance(result, Extractor, uri)
|
|
|
|
|
|
|
|
for not_found in ("", "/tmp/file.ext"):
|
|
|
|
self.assertIsNone(extractor.find(not_found))
|
|
|
|
|
|
|
|
for invalid in (None, [], {}, 123, b"test:"):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
extractor.find(invalid)
|
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
uri = "fake:foobar"
|
|
|
|
self.assertIsNone(extractor.find(uri))
|
|
|
|
|
|
|
|
extractor.add(FakeExtractor)
|
|
|
|
self.assertIsInstance(extractor.find(uri), FakeExtractor)
|
|
|
|
|
|
|
|
def test_add_module(self):
|
|
|
|
uri = "fake:foobar"
|
|
|
|
self.assertIsNone(extractor.find(uri))
|
|
|
|
|
2019-02-08 20:08:16 +01:00
|
|
|
classes = extractor.add_module(sys.modules[__name__])
|
|
|
|
self.assertEqual(len(classes), 1)
|
|
|
|
self.assertEqual(classes[0].pattern, FakeExtractor.pattern)
|
|
|
|
self.assertEqual(classes[0], FakeExtractor)
|
2018-03-24 17:24:34 +01:00
|
|
|
self.assertIsInstance(extractor.find(uri), FakeExtractor)
|
|
|
|
|
|
|
|
def test_blacklist(self):
|
|
|
|
link_uri = "https://example.org/file.jpg"
|
|
|
|
test_uri = "test:"
|
|
|
|
fake_uri = "fake:"
|
|
|
|
|
|
|
|
self.assertIsInstance(extractor.find(link_uri), DLExtractor)
|
|
|
|
self.assertIsInstance(extractor.find(test_uri), Extractor)
|
|
|
|
self.assertIsNone(extractor.find(fake_uri))
|
|
|
|
|
|
|
|
with extractor.blacklist(["directlink"]):
|
|
|
|
self.assertIsNone(extractor.find(link_uri))
|
|
|
|
self.assertIsInstance(extractor.find(test_uri), Extractor)
|
|
|
|
self.assertIsNone(extractor.find(fake_uri))
|
|
|
|
|
|
|
|
with extractor.blacklist([], [DLExtractor, FakeExtractor]):
|
|
|
|
self.assertIsNone(extractor.find(link_uri))
|
|
|
|
self.assertIsInstance(extractor.find(test_uri), Extractor)
|
|
|
|
self.assertIsNone(extractor.find(fake_uri))
|
|
|
|
|
|
|
|
with extractor.blacklist(["test"], [DLExtractor]):
|
|
|
|
self.assertIsNone(extractor.find(link_uri))
|
|
|
|
self.assertIsNone(extractor.find(test_uri))
|
|
|
|
self.assertIsNone(extractor.find(fake_uri))
|
|
|
|
|
2019-02-09 14:39:38 +01:00
|
|
|
def test_from_url(self):
|
|
|
|
for uri in self.VALID_URIS:
|
|
|
|
cls = extractor.find(uri).__class__
|
|
|
|
extr = cls.from_url(uri)
|
|
|
|
self.assertIs(type(extr), cls)
|
|
|
|
self.assertIsInstance(extr, Extractor)
|
|
|
|
|
|
|
|
for not_found in ("", "/tmp/file.ext"):
|
|
|
|
self.assertIsNone(FakeExtractor.from_url(not_found))
|
|
|
|
|
|
|
|
for invalid in (None, [], {}, 123, b"test:"):
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
FakeExtractor.from_url(invalid)
|
|
|
|
|
2018-03-24 17:24:34 +01:00
|
|
|
def test_unique_pattern_matches(self):
|
|
|
|
test_urls = []
|
|
|
|
|
|
|
|
# collect testcase URLs
|
|
|
|
for extr in extractor.extractors():
|
2019-02-06 17:24:44 +01:00
|
|
|
for testcase in extr._get_tests():
|
2018-03-24 17:24:34 +01:00
|
|
|
test_urls.append((testcase[0], extr))
|
|
|
|
|
|
|
|
# iterate over all testcase URLs
|
|
|
|
for url, extr1 in test_urls:
|
|
|
|
matches = []
|
|
|
|
|
|
|
|
# ... and apply all regex patterns to each one
|
2019-02-08 20:08:16 +01:00
|
|
|
for extr2 in extractor._cache:
|
2018-03-24 17:24:34 +01:00
|
|
|
|
|
|
|
# skip DirectlinkExtractor pattern if it isn't tested
|
|
|
|
if extr1 != DLExtractor and extr2 == DLExtractor:
|
|
|
|
continue
|
|
|
|
|
2019-02-08 20:08:16 +01:00
|
|
|
match = extr2.pattern.match(url)
|
2018-03-24 17:24:34 +01:00
|
|
|
if match:
|
|
|
|
matches.append(match)
|
|
|
|
|
|
|
|
# fail if more or less than 1 match happened
|
|
|
|
if len(matches) > 1:
|
|
|
|
msg = "'{}' gets matched by more than one pattern:".format(url)
|
|
|
|
for match in matches:
|
|
|
|
msg += "\n- "
|
|
|
|
msg += match.re.pattern
|
|
|
|
self.fail(msg)
|
|
|
|
|
|
|
|
if len(matches) < 1:
|
|
|
|
msg = "'{}' isn't matched by any pattern".format(url)
|
|
|
|
self.fail(msg)
|
|
|
|
|
2018-04-18 18:01:43 +02:00
|
|
|
def test_docstrings(self):
|
|
|
|
"""ensure docstring uniqueness"""
|
|
|
|
for extr1 in extractor.extractors():
|
|
|
|
for extr2 in extractor.extractors():
|
|
|
|
if extr1 != extr2 and extr1.__doc__ and extr2.__doc__:
|
|
|
|
self.assertNotEqual(
|
|
|
|
extr1.__doc__,
|
|
|
|
extr2.__doc__,
|
|
|
|
"{} <-> {}".format(extr1, extr2),
|
|
|
|
)
|
|
|
|
|
2018-04-18 18:06:30 +02:00
|
|
|
def test_names(self):
|
|
|
|
"""Ensure extractor classes are named CategorySubcategoryExtractor"""
|
2018-08-15 20:39:13 +02:00
|
|
|
def capitalize(c):
|
|
|
|
if "-" in c:
|
|
|
|
return string.capwords(c.replace("-", " ")).replace(" ", "")
|
2019-01-19 14:28:59 +01:00
|
|
|
if "." in c:
|
|
|
|
c = c.replace(".", "")
|
2018-08-15 20:39:13 +02:00
|
|
|
return c.capitalize()
|
|
|
|
|
2018-04-18 18:06:30 +02:00
|
|
|
mapping = {
|
|
|
|
"2chan" : "futaba",
|
|
|
|
"3dbooru": "threedeebooru",
|
|
|
|
"4chan" : "fourchan",
|
|
|
|
"4plebs" : "fourplebs",
|
|
|
|
"8chan" : "infinitychan",
|
|
|
|
"oauth" : None,
|
|
|
|
}
|
|
|
|
|
|
|
|
for extr in extractor.extractors():
|
|
|
|
category = mapping.get(extr.category, extr.category)
|
|
|
|
if category:
|
|
|
|
expected = "{}{}Extractor".format(
|
2018-08-15 20:39:13 +02:00
|
|
|
capitalize(category),
|
|
|
|
capitalize(extr.subcategory),
|
2018-04-18 18:06:30 +02:00
|
|
|
)
|
|
|
|
self.assertEqual(expected, extr.__name__)
|
|
|
|
|
2018-03-24 17:24:34 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|