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