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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# -*- coding: utf-8 -*-
# Copyright 2021-2022 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 Gelbooru Beta 0.2 sites"""
from . import booru
from .. import text, util, exception
from xml.etree import ElementTree
import collections
import re
class GelbooruV02Extractor(booru.BooruExtractor):
basecategory = "gelbooru_v02"
def _init(self):
self.api_key = self.config("api-key")
self.user_id = self.config("user-id")
self.api_root = self.config_instance("api_root") or self.root
if self.category == "realbooru":
self.items = self._items_realbooru
self._tags = self._tags_realbooru
def _api_request(self, params):
url = self.api_root + "/index.php?page=dapi&s=post&q=index"
return ElementTree.fromstring(self.request(url, params=params).text)
def _pagination(self, params):
params["pid"] = self.page_start
params["limit"] = self.per_page
post = total = None
count = 0
while True:
try:
root = self._api_request(params)
except ElementTree.ParseError:
if "tags" not in params or post is None:
raise
taglist = [tag for tag in params["tags"].split()
if not tag.startswith("id:<")]
taglist.append("id:<" + str(post.attrib["id"]))
params["tags"] = " ".join(taglist)
params["pid"] = 0
continue
if total is None:
try:
total = int(root.attrib["count"])
self.log.debug("%s posts in total", total)
except Exception as exc:
total = 0
self.log.debug(
"Failed to get total number of posts (%s: %s)",
exc.__class__.__name__, exc)
post = None
for post in root:
yield post.attrib
num = len(root)
count += num
if num < self.per_page:
if not total or count >= total:
return
if not num:
self.log.debug("Empty response - Retrying")
continue
params["pid"] += 1
def _pagination_html(self, params):
url = self.root + "/index.php"
params["pid"] = self.page_start * self.per_page
data = {}
while True:
num_ids = 0
page = self.request(url, params=params).text
for data["id"] in text.extract_iter(page, '" id="p', '"'):
num_ids += 1
for post in self._api_request(data):
yield post.attrib
if num_ids < self.per_page:
return
params["pid"] += self.per_page
@staticmethod
def _prepare(post):
post["date"] = text.parse_datetime(
post["created_at"], "%a %b %d %H:%M:%S %z %Y")
def _html(self, post):
return self.request("{}/index.php?page=post&s=view&id={}".format(
self.root, post["id"])).text
def _tags(self, post, page):
tag_container = (text.extr(page, '<ul id="tag-', '</ul>') or
text.extr(page, '<ul class="tag-', '</ul>'))
if not tag_container:
return
tags = collections.defaultdict(list)
pattern = re.compile(
r"tag-type-([^\"' ]+).*?[?;]tags=([^\"'&]+)", re.S)
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 _file_url_realbooru(self, post):
url = post["file_url"]
md5 = post["md5"]
if md5 not in post["preview_url"] or url.count("/") == 5:
url = "{}/images/{}/{}/{}.{}".format(
self.root, md5[0:2], md5[2:4], md5, url.rpartition(".")[2])
return url
def _items_realbooru(self):
from .common import Message
data = self.metadata()
for post in self.posts():
try:
html = self._html(post)
fallback = post["file_url"]
url = post["file_url"] = text.rextract(
html, 'href="', '"', html.index(">Original<"))[0]
except Exception:
self.log.debug("Unable to fetch download URL for post %s "
"(md5: %s)", post.get("id"), post.get("md5"))
continue
text.nameext_from_url(url, post)
post.update(data)
self._prepare(post)
self._tags(post, html)
path = url.rpartition("/")[0]
post["_fallback"] = (
"{}/{}.{}".format(path, post["md5"], post["extension"]),
fallback,
)
yield Message.Directory, post
yield Message.Url, url, post
def _tags_realbooru(self, post, page):
tag_container = text.extr(page, 'id="tagLink"', '</div>')
tags = collections.defaultdict(list)
pattern = re.compile(
r'<a class="(?: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)
BASE_PATTERN = GelbooruV02Extractor.update({
"realbooru": {
"root": "https://realbooru.com",
"pattern": r"realbooru\.com",
},
"rule34": {
"root": "https://rule34.xxx",
"pattern": r"(?:www\.)?rule34\.xxx",
"api_root": "https://api.rule34.xxx",
},
"safebooru": {
"root": "https://safebooru.org",
"pattern": r"safebooru\.org",
},
"tbib": {
"root": "https://tbib.org",
"pattern": r"tbib\.org",
},
"hypnohub": {
"root": "https://hypnohub.net",
"pattern": r"hypnohub\.net",
},
"xbooru": {
"root": "https://xbooru.com",
"pattern": r"xbooru\.com",
},
})
class GelbooruV02TagExtractor(GelbooruV02Extractor):
subcategory = "tag"
directory_fmt = ("{category}", "{search_tags}")
archive_fmt = "t_{search_tags}_{id}"
pattern = BASE_PATTERN + r"/index\.php\?page=post&s=list&tags=([^&#]*)"
example = "https://safebooru.org/index.php?page=post&s=list&tags=TAG"
def __init__(self, match):
GelbooruV02Extractor.__init__(self, match)
tags = match.group(match.lastindex)
self.tags = text.unquote(tags.replace("+", " "))
def metadata(self):
return {"search_tags": self.tags}
def posts(self):
if self.tags == "all":
self.tags = ""
return self._pagination({"tags": self.tags})
class GelbooruV02PoolExtractor(GelbooruV02Extractor):
subcategory = "pool"
directory_fmt = ("{category}", "pool", "{pool}")
archive_fmt = "p_{pool}_{id}"
pattern = BASE_PATTERN + r"/index\.php\?page=pool&s=show&id=(\d+)"
example = "https://safebooru.org/index.php?page=pool&s=show&id=12345"
def __init__(self, match):
GelbooruV02Extractor.__init__(self, match)
self.pool_id = match.group(match.lastindex)
if self.category == "rule34":
self.posts = self._posts_pages
self.per_page = 45
else:
self.post_ids = ()
def skip(self, num):
self.page_start += num
return num
def metadata(self):
url = "{}/index.php?page=pool&s=show&id={}".format(
self.root, self.pool_id)
page = self.request(url).text
name, pos = text.extract(page, "<h4>Pool: ", "</h4>")
if not name:
raise exception.NotFoundError("pool")
self.post_ids = text.extract_iter(
page, 'class="thumb" id="p', '"', pos)
return {
"pool": text.parse_int(self.pool_id),
"pool_name": text.unescape(name),
}
def posts(self):
params = {}
for params["id"] in util.advance(self.post_ids, self.page_start):
for post in self._api_request(params):
yield post.attrib
def _posts_pages(self):
return self._pagination_html({
"page": "pool",
"s" : "show",
"id" : self.pool_id,
})
class GelbooruV02FavoriteExtractor(GelbooruV02Extractor):
subcategory = "favorite"
directory_fmt = ("{category}", "favorites", "{favorite_id}")
archive_fmt = "f_{favorite_id}_{id}"
per_page = 50
pattern = BASE_PATTERN + r"/index\.php\?page=favorites&s=view&id=(\d+)"
example = "https://safebooru.org/index.php?page=favorites&s=view&id=12345"
def __init__(self, match):
GelbooruV02Extractor.__init__(self, match)
self.favorite_id = match.group(match.lastindex)
def metadata(self):
return {"favorite_id": text.parse_int(self.favorite_id)}
def posts(self):
return self._pagination_html({
"page": "favorites",
"s" : "view",
"id" : self.favorite_id,
})
class GelbooruV02PostExtractor(GelbooruV02Extractor):
subcategory = "post"
archive_fmt = "{id}"
pattern = BASE_PATTERN + r"/index\.php\?page=post&s=view&id=(\d+)"
example = "https://safebooru.org/index.php?page=post&s=view&id=12345"
def __init__(self, match):
GelbooruV02Extractor.__init__(self, match)
self.post_id = match.group(match.lastindex)
def posts(self):
return self._pagination({"id": self.post_id})
|