1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-26 12:42:29 +01:00
gallery-dl/gallery_dl/extractor/__init__.py

90 lines
1.9 KiB
Python
Raw Normal View History

2015-06-28 22:53:52 +02:00
# -*- coding: utf-8 -*-
2014-10-12 21:56:44 +02:00
2015-06-28 22:53:52 +02:00
# Copyright 2015 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.
import re
import importlib
2015-10-05 18:10:18 +02:00
from .. import config
2015-06-28 22:53:52 +02:00
modules = [
"pixiv",
2015-10-31 16:50:20 +01:00
"exhentai",
2015-06-28 22:53:52 +02:00
"gelbooru",
"3dbooru",
"4chan",
"8chan",
"batoto",
2015-12-04 20:33:04 +01:00
"chronos",
2015-06-28 22:53:52 +02:00
"danbooru",
2015-10-05 20:29:48 +02:00
"deviantart",
2015-06-28 22:53:52 +02:00
"e621",
2015-11-15 01:30:26 +01:00
"hbrowse",
2015-11-14 03:19:44 +01:00
"hentaifoundry",
2015-10-28 16:24:35 +01:00
"hitomi",
2015-06-28 22:53:52 +02:00
"imagebam",
2015-12-03 00:16:36 +01:00
"imagetwist",
2015-06-28 22:53:52 +02:00
"imgbox",
"imgchili",
2015-10-28 23:26:47 +01:00
"imgth",
2015-10-12 22:34:45 +02:00
"imgur",
2015-11-07 13:28:07 +01:00
"kissmanga",
2015-11-06 13:24:43 +01:00
"konachan",
2015-11-26 03:06:08 +01:00
"mangahere",
2015-11-25 22:05:26 +01:00
"mangamint",
2015-11-08 00:02:37 +01:00
"mangapanda",
2015-06-28 22:53:52 +02:00
"mangareader",
2015-11-15 03:40:17 +01:00
"mangashare",
2015-11-08 00:03:14 +01:00
"mangastream",
2015-10-28 12:08:27 +01:00
"nhentai",
2015-06-28 22:53:52 +02:00
"nijie",
2015-10-08 20:43:52 +02:00
"powermanga",
2015-11-06 13:52:40 +01:00
"safebooru",
2015-11-09 02:29:33 +01:00
"sankaku",
2015-11-13 00:21:50 +01:00
"spectrumnexus",
2015-12-03 00:25:15 +01:00
"turboimagehost",
2015-06-28 22:53:52 +02:00
"yandere",
2015-12-03 00:50:34 +01:00
"generic",
2015-06-28 22:53:52 +02:00
]
2015-10-05 17:52:50 +02:00
def find(url):
2015-06-28 22:53:52 +02:00
"""Find extractor suitable for handling the given url"""
2015-11-21 03:12:36 +01:00
for pattern, klass in _list_patterns():
2015-06-28 22:53:52 +02:00
match = re.match(pattern, url)
if match:
2015-11-21 00:30:31 +01:00
return klass(match)
return None
2015-06-28 22:53:52 +02:00
# --------------------------------------------------------------------
# internals
_cache = []
_module_iter = iter(modules)
def _list_patterns():
"""Yield all available (pattern, class) tuples"""
2015-06-28 22:53:52 +02:00
for entry in _cache:
yield entry
2015-10-05 18:10:18 +02:00
2015-06-28 22:53:52 +02:00
for module_name in _module_iter:
module = importlib.import_module("."+module_name, __package__)
tuples = [
(pattern, klass)
for klass in _get_classes(module)
for pattern in klass.pattern
]
_cache.extend(tuples)
for etuple in tuples:
yield etuple
2015-11-20 19:54:07 +01:00
def _get_classes(module):
"""Return a list of all extractor classes in a module"""
return [
klass for klass in module.__dict__.values() if (
2015-11-21 00:30:31 +01:00
hasattr(klass, "pattern") and klass.__module__ == module.__name__
2015-11-20 19:54:07 +01:00
)
]