1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00
gallery-dl/gallery_dl/extractor/__init__.py

172 lines
3.5 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
# Copyright 2015-2018 Mike Fährmann
2015-06-28 22:53:52 +02: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 re
import importlib
modules = [
2017-07-14 08:44:31 +02:00
"2chan",
2015-06-28 22:53:52 +02:00
"3dbooru",
"4chan",
2017-07-03 16:43:04 +02:00
"4plebs",
2015-06-28 22:53:52 +02:00
"8chan",
2017-07-14 13:25:53 +02:00
"archivedmoe",
2017-07-15 13:23:04 +02:00
"archiveofsins",
"artstation",
"b4k",
2018-08-01 21:46:55 +02:00
"behance",
2015-06-28 22:53:52 +02:00
"danbooru",
2017-07-11 17:14:50 +02:00
"desuarchive",
2015-10-05 20:29:48 +02:00
"deviantart",
2016-09-26 21:58:18 +02:00
"dokireader",
2016-09-22 17:20:57 +02:00
"dynastyscans",
2015-06-28 22:53:52 +02:00
"e621",
2016-08-04 18:08:48 +02:00
"exhentai",
2017-02-06 20:05:58 +01:00
"fallenangels",
2017-07-15 14:51:58 +02:00
"fireden",
2017-05-30 17:43:02 +02:00
"flickr",
2016-08-04 18:08:48 +02:00
"gelbooru",
2017-05-28 17:09:54 +02:00
"gfycat",
2015-11-15 01:30:26 +01:00
"hbrowse",
2016-02-19 15:24:49 +01:00
"hentai2read",
2015-11-14 03:19:44 +01:00
"hentaifoundry",
"hentaihere",
2015-10-28 16:24:35 +01:00
"hitomi",
"idolcomplex",
2015-06-28 22:53:52 +02:00
"imagebam",
2016-08-09 14:05:12 +02:00
"imagefap",
2015-06-28 22:53:52 +02:00
"imgbox",
2015-10-28 23:26:47 +01:00
"imgth",
2015-10-12 22:34:45 +02:00
"imgur",
2016-12-29 16:41:08 +01:00
"jaiminisbox",
2016-04-20 08:34:44 +02:00
"khinsider",
2017-04-07 11:41:48 +02:00
"kireicake",
2017-04-05 12:16:23 +02:00
"kissmanga",
"komikcast",
2015-11-06 13:24:43 +01:00
"konachan",
2016-08-01 15:36:56 +02:00
"luscious",
"mangadex",
2017-01-14 19:39:21 +01:00
"mangafox",
2015-11-26 03:06:08 +01:00
"mangahere",
2015-11-08 00:02:37 +01:00
"mangapanda",
2015-12-08 22:29:34 +01:00
"mangapark",
2015-06-28 22:53:52 +02:00
"mangareader",
2015-11-08 00:03:14 +01:00
"mangastream",
"myportfolio",
2015-10-28 12:08:27 +01:00
"nhentai",
2015-06-28 22:53:52 +02:00
"nijie",
2017-07-08 17:16:41 +02:00
"nyafuu",
"paheal",
"pawoo",
2016-09-02 19:11:16 +02:00
"pinterest",
"pixiv",
2015-10-08 20:43:52 +02:00
"powermanga",
"readcomiconline",
"rebeccablacktech",
"reddit",
2016-09-17 18:12:37 +02:00
"rule34",
2015-11-06 13:52:40 +01:00
"safebooru",
2015-11-09 02:29:33 +01:00
"sankaku",
2017-04-07 13:20:35 +02:00
"seaotterscans",
2016-08-09 16:36:30 +02:00
"seiga",
2016-08-02 17:42:22 +02:00
"senmanga",
2016-10-25 15:25:25 +02:00
"sensescans",
"simplyhentai",
"slideshare",
"smugmug",
"subapics",
2017-07-23 15:45:17 +02:00
"thebarchive",
2016-02-20 11:29:10 +01:00
"tumblr",
2016-10-06 19:12:07 +02:00
"twitter",
2017-08-18 19:52:58 +02:00
"warosu",
2016-10-26 23:10:41 +02:00
"worldthree",
2015-06-28 22:53:52 +02:00
"yandere",
2017-11-02 15:36:53 +01:00
"xvideos",
"imagehosts",
2017-05-24 12:51:18 +02:00
"directlink",
2016-10-01 15:54:27 +02:00
"recursive",
"oauth",
"test",
2015-06-28 22:53:52 +02:00
]
2017-02-01 00:53:19 +01:00
2015-10-05 17:52:50 +02:00
def find(url):
"""Find suitable extractor for the given url"""
2015-11-21 03:12:36 +01:00
for pattern, klass in _list_patterns():
2016-08-23 16:36:39 +02:00
match = pattern.match(url)
if match and klass not in _blacklist:
2015-11-21 00:30:31 +01:00
return klass(match)
return None
2015-06-28 22:53:52 +02:00
2017-02-01 00:53:19 +01:00
def add(klass):
"""Add 'klass' to the list of available extractors"""
for pattern in klass.pattern:
_cache.append((re.compile(pattern), klass))
def add_module(module):
"""Add all extractors in 'module' to the list of available extractors"""
tuples = [
(re.compile(pattern), klass)
for klass in _get_classes(module)
for pattern in klass.pattern
]
_cache.extend(tuples)
return tuples
def extractors():
"""Yield all available extractor classes"""
return sorted(
set(klass for _, klass in _list_patterns()),
key=lambda x: x.__name__
)
2017-02-01 00:53:19 +01:00
class blacklist():
"""Context Manager to blacklist extractor modules"""
def __init__(self, categories, extractors=None):
self.extractors = extractors or []
for _, klass in _list_patterns():
if klass.category in categories:
self.extractors.append(klass)
def __enter__(self):
_blacklist.update(self.extractors)
def __exit__(self, etype, value, traceback):
_blacklist.clear()
2015-06-28 22:53:52 +02:00
# --------------------------------------------------------------------
# internals
_cache = []
_blacklist = set()
2015-06-28 22:53:52 +02:00
_module_iter = iter(modules)
2017-02-01 00:53:19 +01:00
2015-06-28 22:53:52 +02:00
def _list_patterns():
"""Yield all available (pattern, class) tuples"""
2016-08-23 16:36:39 +02:00
yield from _cache
2015-10-05 18:10:18 +02:00
2015-06-28 22:53:52 +02:00
for module_name in _module_iter:
yield from add_module(
importlib.import_module("."+module_name, __package__)
)
2015-11-20 19:54:07 +01:00
2017-02-01 00:53:19 +01:00
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
)
]