| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Extractors for https://www.fanbox.cc/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, util |
| 11 |
|
| 12 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?fanbox\.cc" |
| 13 |
USER_PATTERN = ( |
| 14 |
r"(?:https?://)?(?:" |
| 15 |
r"(?!www\.)([\w-]+)\.fanbox\.cc|" |
| 16 |
r"(?:www\.)?fanbox\.cc/@([\w-]+))" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
class FanboxExtractor(Extractor): |
| 21 |
"""Base class for fanbox extractors""" |
| 22 |
category = "fanbox" |
| 23 |
root = "https://www.fanbox.cc" |
| 24 |
directory_fmt = ("{category}", "{creatorId}") |
| 25 |
filename_fmt = "{id}_{num}.{extension}" |
| 26 |
archive_fmt = "{id}_{num}" |
| 27 |
browser = "firefox" |
| 28 |
_warning = True |
| 29 |
|
| 30 |
def _init(self): |
| 31 |
self.headers = { |
| 32 |
"Accept": "application/json, text/plain, */*", |
| 33 |
"Cookie": None, |
| 34 |
} |
| 35 |
self.embeds = self.config("embeds", True) |
| 36 |
self.fee_min = self.config("fee-min") |
| 37 |
self.fee_max = self.config("fee-max") |
| 38 |
|
| 39 |
if includes := self.config("metadata"): |
| 40 |
if isinstance(includes, str): |
| 41 |
includes = includes.split(",") |
| 42 |
elif not isinstance(includes, (list, tuple)): |
| 43 |
includes = ("user", "plan") |
| 44 |
self._meta_user = ("user" in includes) |
| 45 |
self._meta_plan = ("plan" in includes) |
| 46 |
self._meta_comments = ("comments" in includes) |
| 47 |
else: |
| 48 |
self._meta_user = self._meta_plan = self._meta_comments = False |
| 49 |
|
| 50 |
if self.config("comments"): |
| 51 |
self._meta_comments = True |
| 52 |
|
| 53 |
if self._warning: |
| 54 |
if not self.cookies_check(("FANBOXSESSID",)): |
| 55 |
self.log.warning("no 'FANBOXSESSID' cookie set") |
| 56 |
FanboxExtractor._warning = False |
| 57 |
|
| 58 |
def items(self): |
| 59 |
for item in self.posts(): |
| 60 |
if self._check_fee(item): |
| 61 |
item = self._request_post(item) |
| 62 |
content_body, post = self._extract_post(item) |
| 63 |
yield Message.Directory, "", post |
| 64 |
yield from self._get_urls_from_post(content_body, post) |
| 65 |
|
| 66 |
def posts(self): |
| 67 |
"""Return all relevant post objects""" |
| 68 |
|
| 69 |
def _pagination(self, url): |
| 70 |
while url: |
| 71 |
url = text.ensure_http_scheme(url) |
| 72 |
body = self.request_json(url, headers=self.headers)["body"] |
| 73 |
|
| 74 |
yield from body["items"] |
| 75 |
|
| 76 |
url = body["nextUrl"] |
| 77 |
|
| 78 |
def _check_fee(self, item): |
| 79 |
if self.fee_min is not None and self.fee_min > item["feeRequired"]: |
| 80 |
self.log.warning("Skipping post %s (feeRequired of %s < %s)", |
| 81 |
item["id"], item["feeRequired"], self.fee_min) |
| 82 |
elif self.fee_max is not None and self.fee_max < item["feeRequired"]: |
| 83 |
self.log.warning("Skipping post %s (feeRequired of %s > %s)", |
| 84 |
item["id"], item["feeRequired"], self.fee_max) |
| 85 |
else: |
| 86 |
return True |
| 87 |
|
| 88 |
def _request_post(self, item): |
| 89 |
try: |
| 90 |
url = "https://api.fanbox.cc/post.info?postId=" + item["id"] |
| 91 |
item = self.request_json(url, headers=self.headers)["body"]["post"] |
| 92 |
except Exception as exc: |
| 93 |
self.log.warning("Skipping post %s (%s: %s)", |
| 94 |
item["id"], exc.__class__.__name__, exc) |
| 95 |
return item |
| 96 |
|
| 97 |
def _extract_post(self, post): |
| 98 |
"""Fetch and process post data""" |
| 99 |
post["archives"] = () |
| 100 |
|
| 101 |
if content_body := post.pop("body", None): |
| 102 |
if "html" in content_body: |
| 103 |
post["html"] = content_body["html"] |
| 104 |
if post["type"] == "article": |
| 105 |
post["articleBody"] = content_body.copy() |
| 106 |
if "blocks" in content_body: |
| 107 |
content = [] |
| 108 |
images = [] |
| 109 |
files = [] |
| 110 |
|
| 111 |
for block in content_body["blocks"]: |
| 112 |
if "text" in block: |
| 113 |
content.append(block["text"]) |
| 114 |
if "links" in block: |
| 115 |
for link in block["links"]: |
| 116 |
content.append(link["url"]) |
| 117 |
if "imageId" in block: |
| 118 |
images.append(block["imageId"]) |
| 119 |
if "fileId" in block: |
| 120 |
files.append(block["fileId"]) |
| 121 |
|
| 122 |
post["content"] = "\n".join(content) |
| 123 |
|
| 124 |
self._sort_map(content_body, "imageMap", images) |
| 125 |
if file_map := self._sort_map(content_body, "fileMap", files): |
| 126 |
exts = util.EXTS_ARCHIVE |
| 127 |
post["archives"] = [ |
| 128 |
file |
| 129 |
for file in file_map.values() |
| 130 |
if file.get("extension", "").lower() in exts |
| 131 |
] |
| 132 |
|
| 133 |
try: |
| 134 |
post["date"] = self.parse_datetime_iso(post["publishedDatetime"]) |
| 135 |
except Exception: |
| 136 |
post["date"] = None |
| 137 |
post["text"] = content_body.get("text") if content_body else None |
| 138 |
post["isCoverImage"] = False |
| 139 |
|
| 140 |
cid = post.get("creatorId") |
| 141 |
if self._meta_user and cid is not None: |
| 142 |
post["user"] = self.cache(self._get_user_data, cid) |
| 143 |
if self._meta_plan and cid is not None: |
| 144 |
plans = self.cache(self._get_plan_data, cid) |
| 145 |
fee = post.get("feeRequired") or 0 |
| 146 |
try: |
| 147 |
post["plan"] = plans[fee] |
| 148 |
except KeyError: |
| 149 |
if fees := [f for f in plans if f >= fee]: |
| 150 |
plan = plans[min(fees)] |
| 151 |
else: |
| 152 |
plan = plans[0].copy() |
| 153 |
plan["fee"] = fee |
| 154 |
post["plan"] = plans[fee] = plan |
| 155 |
if self._meta_comments: |
| 156 |
if post.get("commentCount"): |
| 157 |
post["comments"] = self._get_comment_data(post["id"]) |
| 158 |
else: |
| 159 |
post["comments"] = () |
| 160 |
|
| 161 |
return content_body, post |
| 162 |
|
| 163 |
def _sort_map(self, body, key, ids): |
| 164 |
orig = body.get(key) |
| 165 |
if not orig: |
| 166 |
return {} if orig is None else orig |
| 167 |
|
| 168 |
body[key] = new = { |
| 169 |
id: orig[id] |
| 170 |
for id in ids |
| 171 |
if id in orig |
| 172 |
} |
| 173 |
|
| 174 |
return new |
| 175 |
|
| 176 |
def _get_user_data(self, creator_id): |
| 177 |
url = "https://api.fanbox.cc/creator.get" |
| 178 |
params = {"creatorId": creator_id} |
| 179 |
data = self.request_json(url, params=params, headers=self.headers) |
| 180 |
|
| 181 |
user = data["body"] |
| 182 |
user.update(user.pop("user")) |
| 183 |
|
| 184 |
return user |
| 185 |
|
| 186 |
def _get_plan_data(self, creator_id): |
| 187 |
url = "https://api.fanbox.cc/plan.listCreator" |
| 188 |
params = {"creatorId": creator_id} |
| 189 |
data = self.request_json(url, params=params, headers=self.headers) |
| 190 |
|
| 191 |
plans = {0: { |
| 192 |
"id" : "", |
| 193 |
"title" : "", |
| 194 |
"fee" : 0, |
| 195 |
"description" : "", |
| 196 |
"coverImageUrl" : "", |
| 197 |
"creatorId" : creator_id, |
| 198 |
"hasAdultContent": None, |
| 199 |
"paymentMethod" : None, |
| 200 |
}} |
| 201 |
for plan in data["body"]["plans"]: |
| 202 |
del plan["user"] |
| 203 |
plans[plan["fee"]] = plan |
| 204 |
|
| 205 |
return plans |
| 206 |
|
| 207 |
def _get_comment_data(self, post_id): |
| 208 |
url = ("https://api.fanbox.cc/post.getComments" |
| 209 |
"?limit=10&postId=" + post_id) |
| 210 |
|
| 211 |
comments = [] |
| 212 |
try: |
| 213 |
while url: |
| 214 |
comlist = self.request_json( |
| 215 |
text.ensure_http_scheme(url), headers=self.headers, |
| 216 |
)["body"]["commentList"] |
| 217 |
comments.extend(comlist["items"]) |
| 218 |
url = comlist["nextUrl"] |
| 219 |
except Exception as exc: |
| 220 |
self.log.debug("comments: %s: %s", exc.__class__.__name__, exc) |
| 221 |
return comments |
| 222 |
|
| 223 |
def _get_urls_from_post(self, content_body, post): |
| 224 |
num = 0 |
| 225 |
if cover_image := post.get("coverImageUrl"): |
| 226 |
cover_image = text.re("/c/[0-9a-z_]+").sub("", cover_image) |
| 227 |
final_post = post.copy() |
| 228 |
final_post["isCoverImage"] = True |
| 229 |
final_post["fileUrl"] = cover_image |
| 230 |
text.nameext_from_url(cover_image, final_post) |
| 231 |
final_post["num"] = num |
| 232 |
num += 1 |
| 233 |
yield Message.Url, cover_image, final_post |
| 234 |
|
| 235 |
if not content_body: |
| 236 |
return |
| 237 |
|
| 238 |
if "html" in content_body: |
| 239 |
html_urls = [] |
| 240 |
|
| 241 |
for href in text.extract_iter(content_body["html"], 'href="', '"'): |
| 242 |
if "fanbox.pixiv.net/images/entry" in href: |
| 243 |
html_urls.append(href) |
| 244 |
elif "downloads.fanbox.cc" in href: |
| 245 |
html_urls.append(href) |
| 246 |
for src in text.extract_iter(content_body["html"], |
| 247 |
'data-src-original="', '"'): |
| 248 |
html_urls.append(src) |
| 249 |
|
| 250 |
for url in html_urls: |
| 251 |
final_post = post.copy() |
| 252 |
text.nameext_from_url(url, final_post) |
| 253 |
final_post["fileUrl"] = url |
| 254 |
final_post["num"] = num |
| 255 |
num += 1 |
| 256 |
yield Message.Url, url, final_post |
| 257 |
|
| 258 |
for group in ("images", "imageMap"): |
| 259 |
if group in content_body: |
| 260 |
for item in content_body[group]: |
| 261 |
if group == "imageMap": |
| 262 |
|
| 263 |
item = content_body[group][item] |
| 264 |
|
| 265 |
final_post = post.copy() |
| 266 |
final_post["fileUrl"] = item["originalUrl"] |
| 267 |
text.nameext_from_url(item["originalUrl"], final_post) |
| 268 |
if "extension" in item: |
| 269 |
final_post["extension"] = item["extension"] |
| 270 |
final_post["fileId"] = item.get("id") |
| 271 |
final_post["width"] = item.get("width") |
| 272 |
final_post["height"] = item.get("height") |
| 273 |
final_post["num"] = num |
| 274 |
num += 1 |
| 275 |
yield Message.Url, item["originalUrl"], final_post |
| 276 |
|
| 277 |
for group in ("files", "fileMap"): |
| 278 |
if group in content_body: |
| 279 |
for item in content_body[group]: |
| 280 |
if group == "fileMap": |
| 281 |
|
| 282 |
item = content_body[group][item] |
| 283 |
|
| 284 |
final_post = post.copy() |
| 285 |
final_post["fileUrl"] = item["url"] |
| 286 |
text.nameext_from_url(item["url"], final_post) |
| 287 |
if "extension" in item: |
| 288 |
final_post["extension"] = item["extension"] |
| 289 |
if "name" in item: |
| 290 |
final_post["filename"] = item["name"] |
| 291 |
final_post["fileId"] = item.get("id") |
| 292 |
final_post["num"] = num |
| 293 |
num += 1 |
| 294 |
yield Message.Url, item["url"], final_post |
| 295 |
|
| 296 |
if self.embeds: |
| 297 |
embeds_found = [] |
| 298 |
if "video" in content_body: |
| 299 |
embeds_found.append(content_body["video"]) |
| 300 |
embeds_found.extend(content_body.get("embedMap", {}).values()) |
| 301 |
|
| 302 |
for embed in embeds_found: |
| 303 |
|
| 304 |
embed_result = self._process_embed(post, embed) |
| 305 |
if not embed_result: |
| 306 |
continue |
| 307 |
embed_result[2]["num"] = num |
| 308 |
num += 1 |
| 309 |
yield embed_result |
| 310 |
|
| 311 |
def _process_embed(self, post, embed): |
| 312 |
final_post = post.copy() |
| 313 |
provider = embed["serviceProvider"] |
| 314 |
content_id = embed.get("videoId") or embed.get("contentId") |
| 315 |
prefix = "ytdl:" if self.embeds == "ytdl" else "" |
| 316 |
url = None |
| 317 |
is_video = False |
| 318 |
|
| 319 |
if provider == "soundcloud": |
| 320 |
url = prefix+"https://soundcloud.com/"+content_id |
| 321 |
is_video = True |
| 322 |
elif provider == "youtube": |
| 323 |
url = prefix+"https://youtube.com/watch?v="+content_id |
| 324 |
is_video = True |
| 325 |
elif provider == "vimeo": |
| 326 |
url = prefix+"https://vimeo.com/"+content_id |
| 327 |
is_video = True |
| 328 |
elif provider == "fanbox": |
| 329 |
|
| 330 |
|
| 331 |
url = "https://www.pixiv.net/fanbox/"+content_id |
| 332 |
|
| 333 |
try: |
| 334 |
url = self.request_location(url) |
| 335 |
except Exception as exc: |
| 336 |
url = None |
| 337 |
self.log.warning("Unable to extract fanbox embed %s (%s: %s)", |
| 338 |
content_id, exc.__class__.__name__, exc) |
| 339 |
else: |
| 340 |
final_post["_extractor"] = FanboxPostExtractor |
| 341 |
elif provider == "twitter": |
| 342 |
url = "https://twitter.com/_/status/"+content_id |
| 343 |
elif provider == "google_forms": |
| 344 |
url = (f"https://docs.google.com/forms/d/e/" |
| 345 |
f"{content_id}/viewform?usp=sf_link") |
| 346 |
else: |
| 347 |
self.log.warning("service not recognized: %s", provider) |
| 348 |
|
| 349 |
if url: |
| 350 |
final_post["embed"] = embed |
| 351 |
final_post["embedUrl"] = url |
| 352 |
text.nameext_from_url(url, final_post) |
| 353 |
msg_type = Message.Queue |
| 354 |
if is_video and self.embeds == "ytdl": |
| 355 |
msg_type = Message.Url |
| 356 |
return msg_type, url, final_post |
| 357 |
|
| 358 |
|
| 359 |
class FanboxCreatorExtractor(FanboxExtractor): |
| 360 |
"""Extractor for a pixivFANBOX creator's works""" |
| 361 |
subcategory = "creator" |
| 362 |
pattern = USER_PATTERN + r"(?:/posts)?/?(?:\?([^#]+))?$" |
| 363 |
example = "https://USER.fanbox.cc/" |
| 364 |
_offset = 0 |
| 365 |
|
| 366 |
def skip_posts(self, num): |
| 367 |
self._offset += num |
| 368 |
return num |
| 369 |
|
| 370 |
def posts(self): |
| 371 |
c1, c2, qs = self.groups |
| 372 |
|
| 373 |
params = text.parse_query(qs) |
| 374 |
if "page" in params: |
| 375 |
self._offset += text.parse_int(params["page"]) * 10 |
| 376 |
elif offset := self.config("offset"): |
| 377 |
self._offset += offset |
| 378 |
|
| 379 |
url = "https://api.fanbox.cc/post.paginateCreator?creatorId=" |
| 380 |
return self._pagination_creator(url + (c1 or c2)) |
| 381 |
|
| 382 |
def _pagination_creator(self, url): |
| 383 |
urls = self.request_json( |
| 384 |
url, headers=self.headers)["body"]["pageUrls"] |
| 385 |
|
| 386 |
if self._offset: |
| 387 |
quotient, remainder = divmod(self._offset, 10) |
| 388 |
if quotient: |
| 389 |
urls = urls[quotient:] |
| 390 |
else: |
| 391 |
remainder = None |
| 392 |
|
| 393 |
for url in urls: |
| 394 |
url = text.ensure_http_scheme(url) |
| 395 |
posts = self.request_json( |
| 396 |
url, headers=self.headers)["body"]["posts"] |
| 397 |
if remainder: |
| 398 |
posts = posts[remainder:] |
| 399 |
remainder = None |
| 400 |
yield from posts |
| 401 |
|
| 402 |
|
| 403 |
class FanboxTagExtractor(FanboxExtractor): |
| 404 |
"""Extractor for a pixivFANBOX creator's tagged works""" |
| 405 |
subcategory = "tag" |
| 406 |
pattern = USER_PATTERN + r"/tags/([^/?#]+)" |
| 407 |
example = "https://USER.fanbox.cc/tags/TAG" |
| 408 |
|
| 409 |
def posts(self): |
| 410 |
cid, cid2, tag = self.groups |
| 411 |
self.kwdict["search_tags"] = text.unquote(tag) |
| 412 |
url = (f"https://api.fanbox.cc/post.listTagged" |
| 413 |
f"?creatorId={cid or cid2}&tag={tag}") |
| 414 |
return self._pagination(url) |
| 415 |
|
| 416 |
|
| 417 |
class FanboxPostExtractor(FanboxExtractor): |
| 418 |
"""Extractor for media from a single pixivFANBOX post""" |
| 419 |
subcategory = "post" |
| 420 |
pattern = USER_PATTERN + r"/posts/(\d+)" |
| 421 |
example = "https://USER.fanbox.cc/posts/12345" |
| 422 |
|
| 423 |
def posts(self): |
| 424 |
item = {"id": self.groups[2], "feeRequired": 0} |
| 425 |
post = self._request_post(item) |
| 426 |
if not self._check_fee(post): |
| 427 |
post = item |
| 428 |
self._check_fee = util.false |
| 429 |
return (post,) |
| 430 |
|
| 431 |
|
| 432 |
class FanboxHomeExtractor(FanboxExtractor): |
| 433 |
"""Extractor for your pixivFANBOX home feed""" |
| 434 |
subcategory = "home" |
| 435 |
pattern = BASE_PATTERN + r"/?$" |
| 436 |
example = "https://fanbox.cc/" |
| 437 |
|
| 438 |
def posts(self): |
| 439 |
url = "https://api.fanbox.cc/post.listHome?limit=10" |
| 440 |
return self._pagination(url) |
| 441 |
|
| 442 |
|
| 443 |
class FanboxSupportingExtractor(FanboxExtractor): |
| 444 |
"""Extractor for your supported pixivFANBOX users feed""" |
| 445 |
subcategory = "supporting" |
| 446 |
pattern = BASE_PATTERN + r"/home/supporting" |
| 447 |
example = "https://fanbox.cc/home/supporting" |
| 448 |
|
| 449 |
def posts(self): |
| 450 |
url = "https://api.fanbox.cc/post.listSupporting?limit=10" |
| 451 |
return self._pagination(url) |
| 452 |
|
| 453 |
|
| 454 |
class FanboxRedirectExtractor(Extractor): |
| 455 |
"""Extractor for pixiv redirects to fanbox.cc""" |
| 456 |
category = "fanbox" |
| 457 |
subcategory = "redirect" |
| 458 |
cookies_domain = None |
| 459 |
pattern = r"(?:https?://)?(?:www\.)?pixiv\.net/fanbox/creator/(\d+)" |
| 460 |
example = "https://www.pixiv.net/fanbox/creator/12345" |
| 461 |
|
| 462 |
def items(self): |
| 463 |
url = "https://www.pixiv.net/fanbox/creator/" + self.groups[0] |
| 464 |
location = self.request_location(url, notfound="user") |
| 465 |
yield Message.Queue, location, {"_extractor": FanboxCreatorExtractor} |