| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for Moebooru based sites""" |
| 10 |
|
| 11 |
from .booru import BooruExtractor |
| 12 |
from .. import text, dt |
| 13 |
import collections |
| 14 |
|
| 15 |
|
| 16 |
class MoebooruExtractor(BooruExtractor): |
| 17 |
"""Base class for Moebooru extractors""" |
| 18 |
basecategory = "moebooru" |
| 19 |
filename_fmt = "{category}_{id}_{md5}.{extension}" |
| 20 |
page_start = 1 |
| 21 |
|
| 22 |
def _prepare(self, post): |
| 23 |
post["date"] = dt.parse_ts(post["created_at"]) |
| 24 |
|
| 25 |
def _html(self, post): |
| 26 |
url = f"{self.root}/post/show/{post['id']}" |
| 27 |
return self.request(url).text |
| 28 |
|
| 29 |
def _tags(self, post, page): |
| 30 |
tag_container = text.extr(page, '<ul id="tag-', '</ul>') |
| 31 |
if not tag_container: |
| 32 |
return |
| 33 |
|
| 34 |
tags = collections.defaultdict(list) |
| 35 |
pattern = text.re(r"tag-type-([^\"' ]+).*?[?;]tags=([^\"'+]+)") |
| 36 |
for tag_type, tag_name in pattern.findall(tag_container): |
| 37 |
tags[tag_type].append(text.unquote(tag_name)) |
| 38 |
for key, value in tags.items(): |
| 39 |
post["tags_" + key] = " ".join(value) |
| 40 |
|
| 41 |
def _notes(self, post, page): |
| 42 |
note_container = text.extr(page, 'id="note-container"', "<img ") |
| 43 |
if not note_container: |
| 44 |
return |
| 45 |
|
| 46 |
post["notes"] = notes = [] |
| 47 |
for note in note_container.split('class="note-box"')[1:]: |
| 48 |
extr = text.extract_from(note) |
| 49 |
notes.append({ |
| 50 |
"width" : int(extr("width:", "p")), |
| 51 |
"height": int(extr("height:", "p")), |
| 52 |
"y" : int(extr("top:", "p")), |
| 53 |
"x" : int(extr("left:", "p")), |
| 54 |
"id" : int(extr('id="note-body-', '"')), |
| 55 |
"body" : text.unescape(text.remove_html(extr(">", "</div>"))), |
| 56 |
}) |
| 57 |
|
| 58 |
def _pagination(self, url, params): |
| 59 |
params["page"] = self.page_start |
| 60 |
params["limit"] = self.per_page |
| 61 |
|
| 62 |
while True: |
| 63 |
posts = self.request_json(url, params=params) |
| 64 |
yield from posts |
| 65 |
|
| 66 |
if len(posts) < self.per_page: |
| 67 |
return |
| 68 |
params["page"] += 1 |
| 69 |
|
| 70 |
|
| 71 |
BASE_PATTERN = MoebooruExtractor.update({ |
| 72 |
"yandere": { |
| 73 |
"root": "https://yande.re", |
| 74 |
"pattern": r"yande\.re", |
| 75 |
}, |
| 76 |
"konachan": { |
| 77 |
"root": "https://konachan.com", |
| 78 |
"pattern": r"konachan\.(?:com|net)", |
| 79 |
}, |
| 80 |
"sakugabooru": { |
| 81 |
"root": "https://www.sakugabooru.com", |
| 82 |
"pattern": r"(?:www\.)?sakugabooru\.com", |
| 83 |
}, |
| 84 |
"lolibooru": { |
| 85 |
"root": "https://lolibooru.moe", |
| 86 |
"pattern": r"lolibooru\.moe", |
| 87 |
}, |
| 88 |
}) |
| 89 |
|
| 90 |
|
| 91 |
class MoebooruTagExtractor(MoebooruExtractor): |
| 92 |
subcategory = "tag" |
| 93 |
directory_fmt = ("{category}", "{search_tags}") |
| 94 |
archive_fmt = "t_{search_tags}_{id}" |
| 95 |
pattern = BASE_PATTERN + r"/post\?(?:[^&#]*&)*tags=([^&#]*)" |
| 96 |
example = "https://yande.re/post?tags=TAG" |
| 97 |
|
| 98 |
def __init__(self, match): |
| 99 |
MoebooruExtractor.__init__(self, match) |
| 100 |
self.tags = text.unquote(self.groups[-1].replace("+", " ")) |
| 101 |
|
| 102 |
def metadata(self): |
| 103 |
return {"search_tags": self.tags} |
| 104 |
|
| 105 |
def posts(self): |
| 106 |
params = {"tags": self.tags} |
| 107 |
return self._pagination(self.root + "/post.json", params) |
| 108 |
|
| 109 |
|
| 110 |
class MoebooruPoolExtractor(MoebooruExtractor): |
| 111 |
subcategory = "pool" |
| 112 |
directory_fmt = ("{category}", "pool", "{pool}") |
| 113 |
archive_fmt = "p_{pool}_{id}" |
| 114 |
pattern = BASE_PATTERN + r"/pool/show/(\d+)" |
| 115 |
example = "https://yande.re/pool/show/12345" |
| 116 |
|
| 117 |
def __init__(self, match): |
| 118 |
MoebooruExtractor.__init__(self, match) |
| 119 |
self.pool_id = self.groups[-1] |
| 120 |
|
| 121 |
def metadata(self): |
| 122 |
if self.config("metadata"): |
| 123 |
url = f"{self.root}/pool/show/{self.pool_id}.json" |
| 124 |
pool = self.request_json(url) |
| 125 |
pool["name"] = pool["name"].replace("_", " ") |
| 126 |
pool.pop("posts", None) |
| 127 |
return {"pool": pool} |
| 128 |
return {"pool": text.parse_int(self.pool_id)} |
| 129 |
|
| 130 |
def posts(self): |
| 131 |
params = {"tags": "pool:" + self.pool_id} |
| 132 |
return self._pagination(self.root + "/post.json", params) |
| 133 |
|
| 134 |
|
| 135 |
class MoebooruPostExtractor(MoebooruExtractor): |
| 136 |
subcategory = "post" |
| 137 |
archive_fmt = "{id}" |
| 138 |
pattern = BASE_PATTERN + r"/post/show/(\d+)" |
| 139 |
example = "https://yande.re/post/show/12345" |
| 140 |
|
| 141 |
def posts(self): |
| 142 |
params = {"tags": "id:" + self.groups[-1]} |
| 143 |
return self.request_json(self.root + "/post.json", params=params) |
| 144 |
|
| 145 |
|
| 146 |
class MoebooruPopularExtractor(MoebooruExtractor): |
| 147 |
subcategory = "popular" |
| 148 |
directory_fmt = ("{category}", "popular", "{scale}", "{date}") |
| 149 |
archive_fmt = "P_{scale[0]}_{date}_{id}" |
| 150 |
pattern = BASE_PATTERN + \ |
| 151 |
r"/post/popular_(by_(?:day|week|month)|recent)(?:\?([^#]*))?" |
| 152 |
example = "https://yande.re/post/popular_by_month?year=YYYY&month=MM" |
| 153 |
|
| 154 |
def __init__(self, match): |
| 155 |
MoebooruExtractor.__init__(self, match) |
| 156 |
self.scale = self.groups[-2] |
| 157 |
self.query = self.groups[-1] |
| 158 |
|
| 159 |
def metadata(self): |
| 160 |
self.params = params = text.parse_query(self.query) |
| 161 |
|
| 162 |
if "year" in params: |
| 163 |
date = (f"{params['year']:>04}-{params.get('month', '01'):>02}-" |
| 164 |
f"{params.get('day', '01'):>02}") |
| 165 |
else: |
| 166 |
date = dt.date.today().isoformat() |
| 167 |
|
| 168 |
scale = self.scale |
| 169 |
if scale.startswith("by_"): |
| 170 |
scale = scale[3:] |
| 171 |
if scale == "week": |
| 172 |
date = dt.date.fromisoformat(date) |
| 173 |
date = (date - dt.timedelta(days=date.weekday())).isoformat() |
| 174 |
elif scale == "month": |
| 175 |
date = date[:-3] |
| 176 |
|
| 177 |
return {"date": date, "scale": scale} |
| 178 |
|
| 179 |
def posts(self): |
| 180 |
url = f"{self.root}/post/popular_{self.scale}.json" |
| 181 |
return self.request_json(url, params=self.params) |