| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://inkbunny.net/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
|
| 15 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?inkbunny\.net" |
| 16 |
|
| 17 |
|
| 18 |
class InkbunnyExtractor(Extractor): |
| 19 |
"""Base class for inkbunny extractors""" |
| 20 |
category = "inkbunny" |
| 21 |
directory_fmt = ("{category}", "{username!l}") |
| 22 |
filename_fmt = "{submission_id} {file_id} {title}.{extension}" |
| 23 |
archive_fmt = "{file_id}" |
| 24 |
root = "https://inkbunny.net" |
| 25 |
|
| 26 |
def _init(self): |
| 27 |
self.api = InkbunnyAPI(self) |
| 28 |
|
| 29 |
def items(self): |
| 30 |
self.api.authenticate() |
| 31 |
metadata = self.metadata() |
| 32 |
to_bool = ("deleted", "favorite", "friends_only", "guest_block", |
| 33 |
"hidden", "public", "scraps") |
| 34 |
|
| 35 |
for post in self.posts(): |
| 36 |
post.update(metadata) |
| 37 |
post["date"] = self.parse_datetime_iso( |
| 38 |
post["create_datetime"][:19]) |
| 39 |
post["tags"] = [kw["keyword_name"] for kw in post["keywords"]] |
| 40 |
post["ratings"] = [r["name"] for r in post["ratings"]] |
| 41 |
files = post["files"] |
| 42 |
|
| 43 |
for key in to_bool: |
| 44 |
if key in post: |
| 45 |
post[key] = (post[key] == "t") |
| 46 |
|
| 47 |
del post["keywords"] |
| 48 |
del post["files"] |
| 49 |
|
| 50 |
yield Message.Directory, "", post |
| 51 |
for post["num"], file in enumerate(files, 1): |
| 52 |
post.update(file) |
| 53 |
post["deleted"] = (file["deleted"] == "t") |
| 54 |
post["date"] = self.parse_datetime_iso( |
| 55 |
file["create_datetime"][:19]) |
| 56 |
text.nameext_from_url(file["file_name"], post) |
| 57 |
|
| 58 |
url = file["file_url_full"] |
| 59 |
if "/private_files/" in url: |
| 60 |
url += "?sid=" + self.api.session_id |
| 61 |
yield Message.Url, url, post |
| 62 |
|
| 63 |
def posts(self): |
| 64 |
return () |
| 65 |
|
| 66 |
def metadata(self): |
| 67 |
return () |
| 68 |
|
| 69 |
|
| 70 |
class InkbunnyUserExtractor(InkbunnyExtractor): |
| 71 |
"""Extractor for inkbunny user profiles""" |
| 72 |
subcategory = "user" |
| 73 |
pattern = BASE_PATTERN + r"/(?!s/)(gallery/|scraps/)?(\w+)(?:$|[/?#])" |
| 74 |
example = "https://inkbunny.net/USER" |
| 75 |
|
| 76 |
def __init__(self, match): |
| 77 |
kind, self.user = match.groups() |
| 78 |
if not kind: |
| 79 |
self.scraps = None |
| 80 |
elif kind[0] == "g": |
| 81 |
self.subcategory = "gallery" |
| 82 |
self.scraps = "no" |
| 83 |
else: |
| 84 |
self.subcategory = "scraps" |
| 85 |
self.scraps = "only" |
| 86 |
InkbunnyExtractor.__init__(self, match) |
| 87 |
|
| 88 |
def posts(self): |
| 89 |
orderby = self.config("orderby") |
| 90 |
params = { |
| 91 |
"username": self.user, |
| 92 |
"scraps" : self.scraps, |
| 93 |
"orderby" : orderby, |
| 94 |
} |
| 95 |
if orderby and orderby.startswith("unread_"): |
| 96 |
params["unread_submissions"] = "yes" |
| 97 |
return self.api.search(params) |
| 98 |
|
| 99 |
|
| 100 |
class InkbunnyPoolExtractor(InkbunnyExtractor): |
| 101 |
"""Extractor for inkbunny pools""" |
| 102 |
subcategory = "pool" |
| 103 |
pattern = (BASE_PATTERN + r"/(?:" |
| 104 |
r"poolview_process\.php\?pool_id=(\d+)|" |
| 105 |
r"submissionsviewall\.php" |
| 106 |
r"\?((?:[^#]+&)?mode=pool(?:&[^#]+)?))") |
| 107 |
example = "https://inkbunny.net/poolview_process.php?pool_id=12345" |
| 108 |
|
| 109 |
def __init__(self, match): |
| 110 |
InkbunnyExtractor.__init__(self, match) |
| 111 |
if pid := match[1]: |
| 112 |
self.pool_id = pid |
| 113 |
self.orderby = "pool_order" |
| 114 |
else: |
| 115 |
params = text.parse_query(match[2]) |
| 116 |
self.pool_id = params.get("pool_id") |
| 117 |
self.orderby = params.get("orderby", "pool_order") |
| 118 |
|
| 119 |
def metadata(self): |
| 120 |
return {"pool_id": self.pool_id} |
| 121 |
|
| 122 |
def posts(self): |
| 123 |
params = { |
| 124 |
"pool_id": self.pool_id, |
| 125 |
"orderby": self.orderby, |
| 126 |
} |
| 127 |
return self.api.search(params) |
| 128 |
|
| 129 |
|
| 130 |
class InkbunnyFavoriteExtractor(InkbunnyExtractor): |
| 131 |
"""Extractor for inkbunny user favorites""" |
| 132 |
subcategory = "favorite" |
| 133 |
directory_fmt = ("{category}", "{favs_username!l}", "Favorites") |
| 134 |
pattern = (BASE_PATTERN + r"/(?:" |
| 135 |
r"userfavorites_process\.php\?favs_user_id=(\d+)|" |
| 136 |
r"submissionsviewall\.php" |
| 137 |
r"\?((?:[^#]+&)?mode=userfavs(?:&[^#]+)?))") |
| 138 |
example = ("https://inkbunny.net/userfavorites_process.php" |
| 139 |
"?favs_user_id=12345") |
| 140 |
|
| 141 |
def __init__(self, match): |
| 142 |
InkbunnyExtractor.__init__(self, match) |
| 143 |
if uid := match[1]: |
| 144 |
self.user_id = uid |
| 145 |
self.orderby = self.config("orderby", "fav_datetime") |
| 146 |
else: |
| 147 |
params = text.parse_query(match[2]) |
| 148 |
self.user_id = params.get("user_id") |
| 149 |
self.orderby = params.get("orderby", "fav_datetime") |
| 150 |
|
| 151 |
def metadata(self): |
| 152 |
|
| 153 |
url = (f"{self.root}/userfavorites_process.php" |
| 154 |
f"?favs_user_id={self.user_id}") |
| 155 |
page = self.request(url).text |
| 156 |
user_link = text.extr(page, '<a rel="author"', '</a>') |
| 157 |
favs_username = text.extr(user_link, 'href="/', '"') |
| 158 |
|
| 159 |
return { |
| 160 |
"favs_user_id": self.user_id, |
| 161 |
"favs_username": favs_username, |
| 162 |
} |
| 163 |
|
| 164 |
def posts(self): |
| 165 |
params = { |
| 166 |
"favs_user_id": self.user_id, |
| 167 |
"orderby" : self.orderby, |
| 168 |
} |
| 169 |
if self.orderby and self.orderby.startswith("unread_"): |
| 170 |
params["unread_submissions"] = "yes" |
| 171 |
return self.api.search(params) |
| 172 |
|
| 173 |
|
| 174 |
class InkbunnyUnreadExtractor(InkbunnyExtractor): |
| 175 |
"""Extractor for unread inkbunny submissions""" |
| 176 |
subcategory = "unread" |
| 177 |
pattern = (BASE_PATTERN + r"/submissionsviewall\.php" |
| 178 |
r"\?((?:[^#]+&)?mode=unreadsubs(?:&[^#]+)?)") |
| 179 |
example = ("https://inkbunny.net/submissionsviewall.php" |
| 180 |
"?text=&mode=unreadsubs&type=") |
| 181 |
|
| 182 |
def __init__(self, match): |
| 183 |
InkbunnyExtractor.__init__(self, match) |
| 184 |
self.params = text.parse_query(match[1]) |
| 185 |
|
| 186 |
def posts(self): |
| 187 |
params = self.params.copy() |
| 188 |
params.pop("rid", None) |
| 189 |
params.pop("mode", None) |
| 190 |
params["unread_submissions"] = "yes" |
| 191 |
return self.api.search(params) |
| 192 |
|
| 193 |
|
| 194 |
class InkbunnySearchExtractor(InkbunnyExtractor): |
| 195 |
"""Extractor for inkbunny search results""" |
| 196 |
subcategory = "search" |
| 197 |
pattern = (BASE_PATTERN + r"/submissionsviewall\.php" |
| 198 |
r"\?((?:[^#]+&)?mode=search(?:&[^#]+)?)") |
| 199 |
example = ("https://inkbunny.net/submissionsviewall.php" |
| 200 |
"?text=TAG&mode=search&type=") |
| 201 |
|
| 202 |
def __init__(self, match): |
| 203 |
InkbunnyExtractor.__init__(self, match) |
| 204 |
self.params = text.parse_query(match[1]) |
| 205 |
|
| 206 |
def metadata(self): |
| 207 |
return {"search": self.params} |
| 208 |
|
| 209 |
def posts(self): |
| 210 |
params = self.params.copy() |
| 211 |
pop = params.pop |
| 212 |
|
| 213 |
pop("rid", None) |
| 214 |
params["string_join_type"] = pop("stringtype", None) |
| 215 |
params["dayslimit"] = pop("days", None) |
| 216 |
params["username"] = pop("artist", None) |
| 217 |
|
| 218 |
if favsby := pop("favsby", None): |
| 219 |
|
| 220 |
url = f"{self.root}/{favsby}" |
| 221 |
page = self.request(url).text |
| 222 |
user_id = text.extr(page, "?user_id=", "'") |
| 223 |
params["favs_user_id"] = user_id.partition("&")[0] |
| 224 |
|
| 225 |
return self.api.search(params) |
| 226 |
|
| 227 |
|
| 228 |
class InkbunnyFollowingExtractor(InkbunnyExtractor): |
| 229 |
"""Extractor for inkbunny user watches""" |
| 230 |
subcategory = "following" |
| 231 |
pattern = (BASE_PATTERN + r"/(?:" |
| 232 |
r"watchlist_process\.php\?mode=watching&user_id=(\d+)|" |
| 233 |
r"usersviewall\.php" |
| 234 |
r"\?((?:[^#]+&)?mode=watching(?:&[^#]+)?))") |
| 235 |
example = ("https://inkbunny.net/watchlist_process.php" |
| 236 |
"?mode=watching&user_id=12345") |
| 237 |
|
| 238 |
def __init__(self, match): |
| 239 |
InkbunnyExtractor.__init__(self, match) |
| 240 |
self.user_id = match[1] or \ |
| 241 |
text.parse_query(match[2]).get("user_id") |
| 242 |
|
| 243 |
def items(self): |
| 244 |
url = self.root + "/watchlist_process.php" |
| 245 |
params = {"mode": "watching", "user_id": self.user_id} |
| 246 |
|
| 247 |
with self.request(url, params=params) as response: |
| 248 |
url, _, params = response.url.partition("?") |
| 249 |
page = response.text |
| 250 |
|
| 251 |
params = text.parse_query(params) |
| 252 |
params["page"] = text.parse_int(params.get("page"), 1) |
| 253 |
data = {"_extractor": InkbunnyUserExtractor} |
| 254 |
|
| 255 |
while True: |
| 256 |
for user in text.extract_iter( |
| 257 |
page, '<a class="widget_userNameSmall" href="', '"', |
| 258 |
page.index('id="changethumboriginal_form"')): |
| 259 |
yield Message.Queue, self.root + user, data |
| 260 |
|
| 261 |
if "<a title='next page' " not in page: |
| 262 |
return |
| 263 |
params["page"] += 1 |
| 264 |
page = self.request(url, params=params).text |
| 265 |
|
| 266 |
|
| 267 |
class InkbunnyPostExtractor(InkbunnyExtractor): |
| 268 |
"""Extractor for individual Inkbunny posts""" |
| 269 |
subcategory = "post" |
| 270 |
pattern = BASE_PATTERN + r"/s/(\d+)" |
| 271 |
example = "https://inkbunny.net/s/12345" |
| 272 |
|
| 273 |
def __init__(self, match): |
| 274 |
InkbunnyExtractor.__init__(self, match) |
| 275 |
self.submission_id = match[1] |
| 276 |
|
| 277 |
def posts(self): |
| 278 |
submissions = self.api.detail(({"submission_id": self.submission_id},)) |
| 279 |
if submissions[0] is None: |
| 280 |
raise self.exc.NotFoundError("submission") |
| 281 |
return submissions |
| 282 |
|
| 283 |
|
| 284 |
class InkbunnyAPI(): |
| 285 |
"""Interface for the Inkunny API |
| 286 |
|
| 287 |
Ref: https://wiki.inkbunny.net/wiki/API |
| 288 |
""" |
| 289 |
|
| 290 |
def __init__(self, extractor): |
| 291 |
self.extractor = extractor |
| 292 |
self.session_id = None |
| 293 |
|
| 294 |
def detail(self, submissions): |
| 295 |
"""Get full details about submissions with the given IDs""" |
| 296 |
ids = { |
| 297 |
sub["submission_id"]: idx |
| 298 |
for idx, sub in enumerate(submissions) |
| 299 |
} |
| 300 |
params = { |
| 301 |
"submission_ids": ",".join(ids), |
| 302 |
"show_description": "yes", |
| 303 |
"show_pools": "yes", |
| 304 |
} |
| 305 |
|
| 306 |
submissions = [None] * len(ids) |
| 307 |
for sub in self._call("submissions", params)["submissions"]: |
| 308 |
submissions[ids[sub["submission_id"]]] = sub |
| 309 |
return submissions |
| 310 |
|
| 311 |
def search(self, params): |
| 312 |
"""Perform a search""" |
| 313 |
return self._pagination_search(params) |
| 314 |
|
| 315 |
def set_allowed_ratings(self, nudity=True, sexual=True, |
| 316 |
violence=True, strong_violence=True): |
| 317 |
"""Change allowed submission ratings""" |
| 318 |
params = { |
| 319 |
"tag[2]": "yes" if nudity else "no", |
| 320 |
"tag[3]": "yes" if violence else "no", |
| 321 |
"tag[4]": "yes" if sexual else "no", |
| 322 |
"tag[5]": "yes" if strong_violence else "no", |
| 323 |
} |
| 324 |
self._call("userrating", params) |
| 325 |
|
| 326 |
def authenticate(self, invalidate=False): |
| 327 |
extr = self.extractor |
| 328 |
username, password = extr._get_auth_info() |
| 329 |
if not username: |
| 330 |
username, password = "guest", "" |
| 331 |
if invalidate: |
| 332 |
extr.cache_update(self._authenticate_impl, username, None) |
| 333 |
|
| 334 |
self.session_id = extr.cache( |
| 335 |
self._authenticate_impl, username, password, |
| 336 |
_exp=365*86400, _mem=False) |
| 337 |
|
| 338 |
if username == "guest": |
| 339 |
self.set_allowed_ratings() |
| 340 |
|
| 341 |
def _authenticate_impl(self, username, password): |
| 342 |
self.extractor.log.info("Logging in as %s", username) |
| 343 |
|
| 344 |
url = "https://inkbunny.net/api_login.php" |
| 345 |
data = {"username": username, "password": password} |
| 346 |
data = self.extractor.request_json(url, method="POST", data=data) |
| 347 |
|
| 348 |
if "sid" not in data: |
| 349 |
raise Extractor.exc.AuthenticationError(data.get("error_message")) |
| 350 |
return data["sid"] |
| 351 |
|
| 352 |
def _call(self, endpoint, params): |
| 353 |
url = "https://inkbunny.net/api_" + endpoint + ".php" |
| 354 |
|
| 355 |
while True: |
| 356 |
params["sid"] = self.session_id |
| 357 |
data = self.extractor.request_json(url, params=params) |
| 358 |
|
| 359 |
if "error_code" not in data: |
| 360 |
return data |
| 361 |
|
| 362 |
if str(data["error_code"]) == "2": |
| 363 |
self.authenticate(invalidate=True) |
| 364 |
continue |
| 365 |
|
| 366 |
raise self.extractor.exc.AbortExtraction(data.get("error_message")) |
| 367 |
|
| 368 |
def _pagination_search(self, params): |
| 369 |
params["page"] = 1 |
| 370 |
params["get_rid"] = "yes" |
| 371 |
params["submission_ids_only"] = "yes" |
| 372 |
|
| 373 |
while True: |
| 374 |
data = self._call("search", params) |
| 375 |
if not data["submissions"]: |
| 376 |
return |
| 377 |
|
| 378 |
yield from self.detail(data["submissions"]) |
| 379 |
|
| 380 |
if data["page"] >= data["pages_count"]: |
| 381 |
return |
| 382 |
if "get_rid" in params: |
| 383 |
del params["get_rid"] |
| 384 |
params["rid"] = data["rid"] |
| 385 |
params["page"] += 1 |