Skip to content

Commit

Permalink
[booru] smaller code adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Jan 6, 2018
1 parent 03b8a54 commit 974e73b
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 33 deletions.
4 changes: 1 addition & 3 deletions gallery_dl/extractor/3dbooru.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from . import booru


class ThreedeebooruExtractor(booru.JsonParserMixin,
booru.MoebooruPageMixin,
booru.BooruExtractor):
class ThreedeebooruExtractor(booru.MoebooruPageMixin, booru.BooruExtractor):
"""Base class for 3dbooru extractors"""
category = "3dbooru"
api_url = "http://behoimi.org/post/index.json"
Expand Down
29 changes: 11 additions & 18 deletions gallery_dl/extractor/booru.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def items(self):

self.reset_page()
while True:
images, count = self.parse_response(
images = self.parse_response(
self.request(self.api_url, params=self.params))

for data in images:
Expand All @@ -55,7 +55,7 @@ def items(self):
except KeyError:
continue

if count < self.per_page:
if len(images) < self.per_page:
return
self.update_page(data)

Expand All @@ -66,27 +66,24 @@ def reset_page(self):
def update_page(self, data):
"""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):
"""Parse JSON API response"""
images = response.json()
if self.sort:
images.sort(key=operator.itemgetter("score", "id"),
reverse=True)
return images, len(images)
return images

def get_metadata(self):
"""Collect metadata for extractor-job"""
return {}


class XmlParserMixin():
"""Class for XML based API responses"""
"""Mixin for XML based API responses"""
def parse_response(self, response):
root = ElementTree.fromstring(response.text)
return map(lambda x: x.attrib, root), len(root)
return [post.attrib for post in root]


class DanbooruPageMixin():
Expand All @@ -98,7 +95,6 @@ def update_page(self, data):
class MoebooruPageMixin():
"""Pagination for Moebooru and Danbooru v1"""
def update_page(self, data):
print("update:", self.params)
if self.page_limit:
self.params["page"] = None
self.params["before_id"] = data["id"]
Expand Down Expand Up @@ -154,19 +150,16 @@ def __init__(self, match):
self.post = match.group("post")
self.params["tags"] = "id:" + self.post

def get_metadata(self):
return {}


class PopularMixin():
"""Extraction and metadata handling for Danbooru v2"""
subcategory = "popular"
directory_fmt = ["{category}", "popular", "{scale}", "{date}"]
page_start = None
sort = True

def __init__(self, match):
super().__init__(match)
self.sort = True
self.params.update(text.parse_query(match.group("query")))

def get_metadata(self, fmt="%Y-%m-%d"):
Expand Down
4 changes: 1 addition & 3 deletions gallery_dl/extractor/danbooru.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
r"\.donmai\.us")


class DanbooruExtractor(booru.JsonParserMixin,
booru.DanbooruPageMixin,
booru.BooruExtractor):
class DanbooruExtractor(booru.DanbooruPageMixin, booru.BooruExtractor):
"""Base class for danbooru extractors"""
category = "danbooru"
page_limit = 1000
Expand Down
4 changes: 1 addition & 3 deletions gallery_dl/extractor/e621.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from . import booru


class E621Extractor(booru.JsonParserMixin,
booru.MoebooruPageMixin,
booru.BooruExtractor):
class E621Extractor(booru.MoebooruPageMixin, booru.BooruExtractor):
"""Base class for e621 extractors"""
category = "e621"
api_url = "https://e621.net/post/index.json"
Expand Down
4 changes: 1 addition & 3 deletions gallery_dl/extractor/konachan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from . import booru


class KonachanExtractor(booru.JsonParserMixin,
booru.MoebooruPageMixin,
booru.BooruExtractor):
class KonachanExtractor(booru.MoebooruPageMixin, booru.BooruExtractor):
"""Base class for konachan extractors"""
category = "konachan"

Expand Down
4 changes: 1 addition & 3 deletions gallery_dl/extractor/yandere.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
from . import booru


class YandereExtractor(booru.JsonParserMixin,
booru.MoebooruPageMixin,
booru.BooruExtractor):
class YandereExtractor(booru.MoebooruPageMixin, booru.BooruExtractor):
"""Base class for yandere extractors"""
category = "yandere"
api_url = "https://yande.re/post.json"
Expand Down

0 comments on commit 974e73b

Please sign in to comment.