[booru] smaller code adjustments
This commit is contained in:
@@ -11,9 +11,7 @@
|
|||||||
from . import booru
|
from . import booru
|
||||||
|
|
||||||
|
|
||||||
class ThreedeebooruExtractor(booru.JsonParserMixin,
|
class ThreedeebooruExtractor(booru.MoebooruPageMixin, booru.BooruExtractor):
|
||||||
booru.MoebooruPageMixin,
|
|
||||||
booru.BooruExtractor):
|
|
||||||
"""Base class for 3dbooru extractors"""
|
"""Base class for 3dbooru extractors"""
|
||||||
category = "3dbooru"
|
category = "3dbooru"
|
||||||
api_url = "http://behoimi.org/post/index.json"
|
api_url = "http://behoimi.org/post/index.json"
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ class BooruExtractor(SharedConfigExtractor):
|
|||||||
|
|
||||||
self.reset_page()
|
self.reset_page()
|
||||||
while True:
|
while True:
|
||||||
images, count = self.parse_response(
|
images = self.parse_response(
|
||||||
self.request(self.api_url, params=self.params))
|
self.request(self.api_url, params=self.params))
|
||||||
|
|
||||||
for data in images:
|
for data in images:
|
||||||
@@ -55,7 +55,7 @@ class BooruExtractor(SharedConfigExtractor):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if count < self.per_page:
|
if len(images) < self.per_page:
|
||||||
return
|
return
|
||||||
self.update_page(data)
|
self.update_page(data)
|
||||||
|
|
||||||
@@ -66,27 +66,24 @@ class BooruExtractor(SharedConfigExtractor):
|
|||||||
def update_page(self, data):
|
def update_page(self, data):
|
||||||
"""Update params to point to the next page"""
|
"""Update params to point to the next page"""
|
||||||
|
|
||||||
def get_metadata(self):
|
|
||||||
"""Collect metadata for extractor-job"""
|
|
||||||
|
|
||||||
|
|
||||||
class JsonParserMixin():
|
|
||||||
"""Class for JSON based API responses"""
|
|
||||||
sort = False
|
|
||||||
|
|
||||||
def parse_response(self, response):
|
def parse_response(self, response):
|
||||||
|
"""Parse JSON API response"""
|
||||||
images = response.json()
|
images = response.json()
|
||||||
if self.sort:
|
if self.sort:
|
||||||
images.sort(key=operator.itemgetter("score", "id"),
|
images.sort(key=operator.itemgetter("score", "id"),
|
||||||
reverse=True)
|
reverse=True)
|
||||||
return images, len(images)
|
return images
|
||||||
|
|
||||||
|
def get_metadata(self):
|
||||||
|
"""Collect metadata for extractor-job"""
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
class XmlParserMixin():
|
class XmlParserMixin():
|
||||||
"""Class for XML based API responses"""
|
"""Mixin for XML based API responses"""
|
||||||
def parse_response(self, response):
|
def parse_response(self, response):
|
||||||
root = ElementTree.fromstring(response.text)
|
root = ElementTree.fromstring(response.text)
|
||||||
return map(lambda x: x.attrib, root), len(root)
|
return [post.attrib for post in root]
|
||||||
|
|
||||||
|
|
||||||
class DanbooruPageMixin():
|
class DanbooruPageMixin():
|
||||||
@@ -98,7 +95,6 @@ class DanbooruPageMixin():
|
|||||||
class MoebooruPageMixin():
|
class MoebooruPageMixin():
|
||||||
"""Pagination for Moebooru and Danbooru v1"""
|
"""Pagination for Moebooru and Danbooru v1"""
|
||||||
def update_page(self, data):
|
def update_page(self, data):
|
||||||
print("update:", self.params)
|
|
||||||
if self.page_limit:
|
if self.page_limit:
|
||||||
self.params["page"] = None
|
self.params["page"] = None
|
||||||
self.params["before_id"] = data["id"]
|
self.params["before_id"] = data["id"]
|
||||||
@@ -154,19 +150,16 @@ class PostMixin():
|
|||||||
self.post = match.group("post")
|
self.post = match.group("post")
|
||||||
self.params["tags"] = "id:" + self.post
|
self.params["tags"] = "id:" + self.post
|
||||||
|
|
||||||
def get_metadata(self):
|
|
||||||
return {}
|
|
||||||
|
|
||||||
|
|
||||||
class PopularMixin():
|
class PopularMixin():
|
||||||
"""Extraction and metadata handling for Danbooru v2"""
|
"""Extraction and metadata handling for Danbooru v2"""
|
||||||
subcategory = "popular"
|
subcategory = "popular"
|
||||||
directory_fmt = ["{category}", "popular", "{scale}", "{date}"]
|
directory_fmt = ["{category}", "popular", "{scale}", "{date}"]
|
||||||
page_start = None
|
page_start = None
|
||||||
|
sort = True
|
||||||
|
|
||||||
def __init__(self, match):
|
def __init__(self, match):
|
||||||
super().__init__(match)
|
super().__init__(match)
|
||||||
self.sort = True
|
|
||||||
self.params.update(text.parse_query(match.group("query")))
|
self.params.update(text.parse_query(match.group("query")))
|
||||||
|
|
||||||
def get_metadata(self, fmt="%Y-%m-%d"):
|
def get_metadata(self, fmt="%Y-%m-%d"):
|
||||||
|
|||||||
@@ -17,9 +17,7 @@ BASE_PATTERN = (
|
|||||||
r"\.donmai\.us")
|
r"\.donmai\.us")
|
||||||
|
|
||||||
|
|
||||||
class DanbooruExtractor(booru.JsonParserMixin,
|
class DanbooruExtractor(booru.DanbooruPageMixin, booru.BooruExtractor):
|
||||||
booru.DanbooruPageMixin,
|
|
||||||
booru.BooruExtractor):
|
|
||||||
"""Base class for danbooru extractors"""
|
"""Base class for danbooru extractors"""
|
||||||
category = "danbooru"
|
category = "danbooru"
|
||||||
page_limit = 1000
|
page_limit = 1000
|
||||||
|
|||||||
@@ -11,9 +11,7 @@
|
|||||||
from . import booru
|
from . import booru
|
||||||
|
|
||||||
|
|
||||||
class E621Extractor(booru.JsonParserMixin,
|
class E621Extractor(booru.MoebooruPageMixin, booru.BooruExtractor):
|
||||||
booru.MoebooruPageMixin,
|
|
||||||
booru.BooruExtractor):
|
|
||||||
"""Base class for e621 extractors"""
|
"""Base class for e621 extractors"""
|
||||||
category = "e621"
|
category = "e621"
|
||||||
api_url = "https://e621.net/post/index.json"
|
api_url = "https://e621.net/post/index.json"
|
||||||
|
|||||||
@@ -11,9 +11,7 @@
|
|||||||
from . import booru
|
from . import booru
|
||||||
|
|
||||||
|
|
||||||
class KonachanExtractor(booru.JsonParserMixin,
|
class KonachanExtractor(booru.MoebooruPageMixin, booru.BooruExtractor):
|
||||||
booru.MoebooruPageMixin,
|
|
||||||
booru.BooruExtractor):
|
|
||||||
"""Base class for konachan extractors"""
|
"""Base class for konachan extractors"""
|
||||||
category = "konachan"
|
category = "konachan"
|
||||||
|
|
||||||
|
|||||||
@@ -11,9 +11,7 @@
|
|||||||
from . import booru
|
from . import booru
|
||||||
|
|
||||||
|
|
||||||
class YandereExtractor(booru.JsonParserMixin,
|
class YandereExtractor(booru.MoebooruPageMixin, booru.BooruExtractor):
|
||||||
booru.MoebooruPageMixin,
|
|
||||||
booru.BooruExtractor):
|
|
||||||
"""Base class for yandere extractors"""
|
"""Base class for yandere extractors"""
|
||||||
category = "yandere"
|
category = "yandere"
|
||||||
api_url = "https://yande.re/post.json"
|
api_url = "https://yande.re/post.json"
|
||||||
|
|||||||
Reference in New Issue
Block a user