merge SharedConfigMixin functionality into Extractor

This commit is contained in:
Mike Fährmann
2020-11-17 00:34:07 +01:00
parent ddfb4fd07a
commit 1e3dd7330e
11 changed files with 40 additions and 45 deletions

View File

@@ -8,7 +8,7 @@
"""Base classes for extractors for danbooru and co""" """Base classes for extractors for danbooru and co"""
from .common import Extractor, Message, SharedConfigMixin from .common import Extractor, Message
from .. import text, exception from .. import text, exception
from xml.etree import ElementTree from xml.etree import ElementTree
import collections import collections
@@ -17,7 +17,7 @@ import operator
import re import re
class BooruExtractor(SharedConfigMixin, Extractor): class BooruExtractor(Extractor):
"""Base class for all booru extractors""" """Base class for all booru extractors"""
basecategory = "booru" basecategory = "booru"
filename_fmt = "{category}_{id}_{md5}.{extension}" filename_fmt = "{category}_{id}_{md5}.{extension}"

View File

@@ -24,6 +24,7 @@ class Extractor():
category = "" category = ""
subcategory = "" subcategory = ""
basecategory = ""
categorytransfer = False categorytransfer = False
directory_fmt = ("{category}",) directory_fmt = ("{category}",)
filename_fmt = "{filename}.{extension}" filename_fmt = "{filename}.{extension}"
@@ -54,6 +55,10 @@ class Extractor():
if self._retries < 0: if self._retries < 0:
self._retries = float("inf") self._retries = float("inf")
if self.basecategory:
self.config = self._config_shared
self.config_accumulate = self._config_shared_accumulate
self._init_headers() self._init_headers()
self._init_cookies() self._init_cookies()
self._init_proxies() self._init_proxies()
@@ -80,6 +85,19 @@ class Extractor():
def config_accumulate(self, key): def config_accumulate(self, key):
return config.accumulate(self._cfgpath, key) return config.accumulate(self._cfgpath, key)
def _config_shared(self, key, default=None):
return config.interpolate_common(("extractor",), (
(self.category, self.subcategory),
(self.basecategory, self.subcategory),
), key, default)
def _config_shared_accumulate(self, key):
values = config.accumulate(self._cfgpath, key)
conf = config.get(("extractor",), self.basecategory)
if conf:
values[:0] = config.accumulate((self.subcategory,), key, conf=conf)
return values
def request(self, url, *, method="GET", session=None, retries=None, def request(self, url, *, method="GET", session=None, retries=None,
encoding=None, fatal=True, notfound=None, **kwargs): encoding=None, fatal=True, notfound=None, **kwargs):
tries = 1 tries = 1
@@ -506,28 +524,6 @@ class AsynchronousMixin():
messages.put(None) messages.put(None)
class SharedConfigMixin():
"""Enable sharing of config settings based on 'basecategory'"""
basecategory = ""
def config(self, key, default=None):
return config.interpolate_common(
("extractor",), (
(self.category, self.subcategory),
(self.basecategory, self.subcategory),
), key, default,
)
def config_accumulate(self, key):
values = config.accumulate(self._cfgpath, key)
conf = config.get(("extractor",), self.basecategory)
if conf:
values[:0] = config.accumulate((self.subcategory,), key, conf=conf)
return values
def generate_extractors(extractor_data, symtable, classes): def generate_extractors(extractor_data, symtable, classes):
"""Dynamically generate Extractor classes""" """Dynamically generate Extractor classes"""
extractors = config.get(("extractor",), classes[0].basecategory) extractors = config.get(("extractor",), classes[0].basecategory)

View File

@@ -8,7 +8,7 @@
"""Extractors for https://danbooru.donmai.us/""" """Extractors for https://danbooru.donmai.us/"""
from .common import Extractor, Message, SharedConfigMixin from .common import Extractor, Message
from .. import text from .. import text
import datetime import datetime
@@ -20,7 +20,7 @@ BASE_PATTERN = (
) )
class DanbooruExtractor(SharedConfigMixin, Extractor): class DanbooruExtractor(Extractor):
"""Base class for danbooru extractors""" """Base class for danbooru extractors"""
basecategory = "booru" basecategory = "booru"
category = "danbooru" category = "danbooru"

View File

@@ -8,13 +8,13 @@
"""Extractors for 4chan archives based on FoolFuuka""" """Extractors for 4chan archives based on FoolFuuka"""
from .common import Extractor, Message, SharedConfigMixin, generate_extractors from .common import Extractor, Message, generate_extractors
from .. import text from .. import text
import itertools import itertools
import operator import operator
class FoolfuukaThreadExtractor(SharedConfigMixin, Extractor): class FoolfuukaThreadExtractor(Extractor):
"""Base extractor for FoolFuuka based boards/archives""" """Base extractor for FoolFuuka based boards/archives"""
basecategory = "foolfuuka" basecategory = "foolfuuka"
subcategory = "thread" subcategory = "thread"

View File

@@ -12,7 +12,6 @@ from .common import (
Extractor, Extractor,
ChapterExtractor, ChapterExtractor,
MangaExtractor, MangaExtractor,
SharedConfigMixin,
Message, Message,
generate_extractors, generate_extractors,
) )
@@ -20,7 +19,7 @@ from .. import text, util
import json import json
class FoolslideBase(SharedConfigMixin): class FoolslideBase():
"""Base class for FoOlSlide extractors""" """Base class for FoOlSlide extractors"""
basecategory = "foolslide" basecategory = "foolslide"

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2016-2019 Mike Fährmann # Copyright 2016-2020 Mike Fährmann
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License version 2 as
@@ -8,13 +8,13 @@
"""Collection of extractors for various imagehosts""" """Collection of extractors for various imagehosts"""
from .common import Extractor, Message, SharedConfigMixin from .common import Extractor, Message
from .. import text, exception from .. import text, exception
from ..cache import memcache from ..cache import memcache
from os.path import splitext from os.path import splitext
class ImagehostImageExtractor(SharedConfigMixin, Extractor): class ImagehostImageExtractor(Extractor):
"""Base class for single-image extractors for various imagehosts""" """Base class for single-image extractors for various imagehosts"""
basecategory = "imagehost" basecategory = "imagehost"
subcategory = "image" subcategory = "image"

View File

@@ -6,13 +6,13 @@
# it under the terms of the GNU General Public License version 2 as # it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation. # published by the Free Software Foundation.
"""Extract images from https://rule34.paheal.net/""" """Extractors for https://rule34.paheal.net/"""
from .common import Extractor, Message, SharedConfigMixin from .common import Extractor, Message
from .. import text from .. import text
class PahealExtractor(SharedConfigMixin, Extractor): class PahealExtractor(Extractor):
"""Base class for paheal extractors""" """Base class for paheal extractors"""
basecategory = "booru" basecategory = "booru"
category = "paheal" category = "paheal"

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2019 Mike Fährmann # Copyright 2019-2020 Mike Fährmann
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License version 2 as
@@ -8,7 +8,7 @@
"""Generic extractors for *reactor sites""" """Generic extractors for *reactor sites"""
from .common import Extractor, Message, SharedConfigMixin from .common import Extractor, Message
from .. import text from .. import text
import urllib.parse import urllib.parse
import random import random
@@ -19,7 +19,7 @@ import json
BASE_PATTERN = r"(?:https?://)?((?:[^/.]+\.)?reactor\.cc)" BASE_PATTERN = r"(?:https?://)?((?:[^/.]+\.)?reactor\.cc)"
class ReactorExtractor(SharedConfigMixin, Extractor): class ReactorExtractor(Extractor):
"""Base class for *reactor.cc extractors""" """Base class for *reactor.cc extractors"""
basecategory = "reactor" basecategory = "reactor"
filename_fmt = "{post_id}_{num:>02}{title[:100]:?_//}.{extension}" filename_fmt = "{post_id}_{num:>02}{title[:100]:?_//}.{extension}"

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2014-2019 Mike Fährmann # Copyright 2014-2020 Mike Fährmann
# #
# This program is free software; you can redistribute it and/or modify # 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 # it under the terms of the GNU General Public License version 2 as
@@ -8,7 +8,7 @@
"""Extractors for https://chan.sankakucomplex.com/""" """Extractors for https://chan.sankakucomplex.com/"""
from .common import Extractor, Message, SharedConfigMixin from .common import Extractor, Message
from .. import text, util, exception from .. import text, util, exception
from ..cache import cache from ..cache import cache
import collections import collections
@@ -17,7 +17,7 @@ import time
import re import re
class SankakuExtractor(SharedConfigMixin, Extractor): class SankakuExtractor(Extractor):
"""Base class for sankaku extractors""" """Base class for sankaku extractors"""
basecategory = "booru" basecategory = "booru"
category = "sankaku" category = "sankaku"

View File

@@ -8,12 +8,12 @@
"""Extractors for Shopify instances""" """Extractors for Shopify instances"""
from .common import Extractor, Message, SharedConfigMixin, generate_extractors from .common import Extractor, Message, generate_extractors
from .. import text from .. import text
import re import re
class ShopifyExtractor(SharedConfigMixin, Extractor): class ShopifyExtractor(Extractor):
"""Base class for Shopify extractors""" """Base class for Shopify extractors"""
basecategory = "shopify" basecategory = "shopify"
filename_fmt = "{product[title]}_{num:>02}_{id}.{extension}" filename_fmt = "{product[title]}_{num:>02}_{id}.{extension}"

View File

@@ -368,7 +368,7 @@ def generate_tests():
# filter available extractor classes # filter available extractor classes
extractors = [ extractors = [
extr for extr in extractor.extractors() extr for extr in extractor.extractors()
if fltr(extr.category, getattr(extr, "basecategory", None)) if fltr(extr.category, extr.basecategory)
] ]
# add 'test_...' methods # add 'test_...' methods