| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://booth.pm/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, util |
| 13 |
|
| 14 |
|
| 15 |
class BoothExtractor(Extractor): |
| 16 |
"""Base class for booth extractors""" |
| 17 |
category = "booth" |
| 18 |
root = "https://booth.pm" |
| 19 |
directory_fmt = ("{category}", "{shop[name]}", "{id} {name}") |
| 20 |
filename_fmt = "{num:>02} {filename}.{extension}" |
| 21 |
archive_fmt = "{id}_{filename}" |
| 22 |
request_interval = (0.5, 1.5) |
| 23 |
|
| 24 |
def _init(self): |
| 25 |
self.cookies.set("adult", "t", domain=".booth.pm") |
| 26 |
|
| 27 |
def _pagination(self, url, json=False): |
| 28 |
while True: |
| 29 |
page = self.request(url).text |
| 30 |
|
| 31 |
if json: |
| 32 |
for item in text.extract_iter(page, ' data-item="', '"'): |
| 33 |
yield util.json_loads(text.unescape(item)) |
| 34 |
else: |
| 35 |
for item in text.extract_iter( |
| 36 |
page, "item-card__title", "</div>"): |
| 37 |
yield text.unescape(text.extr(item, 'href="', '"')) |
| 38 |
|
| 39 |
next = text.extr(page, 'rel="next" class="nav-item" href="', '"') |
| 40 |
if not next: |
| 41 |
break |
| 42 |
url = self.root + text.unescape(next) |
| 43 |
|
| 44 |
|
| 45 |
class BoothItemExtractor(BoothExtractor): |
| 46 |
subcategory = "item" |
| 47 |
pattern = (r"(?:https?://)?(?:[\w-]+\.)?booth\.pm/" |
| 48 |
r"(?:[a-z]{2}(?:-[^/?#]+)?/)?items/(\d+)") |
| 49 |
example = "https://booth.pm/ja/items/12345" |
| 50 |
|
| 51 |
def items(self): |
| 52 |
url = f"{self.root}/ja/items/{self.groups[0]}" |
| 53 |
headers = { |
| 54 |
"Accept": "application/json", |
| 55 |
"Content-Type": "application/json", |
| 56 |
"X-CSRF-Token": None, |
| 57 |
"Priority": "u=4", |
| 58 |
} |
| 59 |
|
| 60 |
if self.config("strategy") == "fallback": |
| 61 |
page = None |
| 62 |
item = self.request_json(url + ".json", headers=headers) |
| 63 |
else: |
| 64 |
page = self.request(url).text |
| 65 |
headers["X-CSRF-Token"] = text.extr( |
| 66 |
page, 'name="csrf-token" content="', '"') |
| 67 |
item = self.request_json( |
| 68 |
url + ".json", headers=headers, interval=False) |
| 69 |
|
| 70 |
item["booth_category"] = item.pop("category", None) |
| 71 |
item["date"] = self.parse_datetime_iso(item["published_at"]) |
| 72 |
item["tags"] = [t["name"] for t in item["tags"]] |
| 73 |
|
| 74 |
shop = item["shop"] |
| 75 |
shop["id"] = text.parse_int(shop["thumbnail_url"].rsplit("/", 3)[1]) |
| 76 |
|
| 77 |
if files := self._extract_files(item, page): |
| 78 |
item["count"] = len(files) |
| 79 |
shop["uuid"] = files[0]["url"].split("/", 4)[3] |
| 80 |
else: |
| 81 |
item["count"] = 0 |
| 82 |
shop["uuid"] = util.NONE |
| 83 |
|
| 84 |
yield Message.Directory, "", item |
| 85 |
for num, file in enumerate(files, 1): |
| 86 |
url = file["url"] |
| 87 |
file["num"] = num |
| 88 |
text.nameext_from_url(url, file) |
| 89 |
yield Message.Url, url, {**item, **file} |
| 90 |
|
| 91 |
def _extract_files(self, item, page): |
| 92 |
if page is None: |
| 93 |
files = [] |
| 94 |
for image in item.pop("images"): |
| 95 |
url = image["original"].replace("_base_resized", "") |
| 96 |
files.append({ |
| 97 |
"url" : url, |
| 98 |
"_fallback": _fallback(url), |
| 99 |
}) |
| 100 |
return files |
| 101 |
|
| 102 |
del item["images"] |
| 103 |
return [{"url": url} |
| 104 |
for url in text.extract_iter(page, 'data-origin="', '"')] |
| 105 |
|
| 106 |
|
| 107 |
class BoothShopExtractor(BoothExtractor): |
| 108 |
subcategory = "shop" |
| 109 |
pattern = r"(?:https?://)?([\w-]+\.)booth\.pm/" |
| 110 |
example = "https://SHOP.booth.pm/" |
| 111 |
|
| 112 |
def __init__(self, match): |
| 113 |
self.root = text.root_from_url(match[0]) |
| 114 |
BoothExtractor.__init__(self, match) |
| 115 |
|
| 116 |
def items(self): |
| 117 |
for item in self._pagination(self.root + "/items", json=True): |
| 118 |
item["_extractor"] = BoothItemExtractor |
| 119 |
yield Message.Queue, item["shop_item_url"], item |
| 120 |
|
| 121 |
|
| 122 |
class BoothCategoryExtractor(BoothExtractor): |
| 123 |
subcategory = "category" |
| 124 |
pattern = r"(?:https?://)?booth\.pm(/[a-z]{2}(?:-[^/?#]+)?/browse/.+)" |
| 125 |
example = "https://booth.pm/ja/browse/CATEGORY" |
| 126 |
|
| 127 |
def items(self): |
| 128 |
data = {"_extractor": BoothItemExtractor} |
| 129 |
for url in self._pagination(self.root + self.groups[0]): |
| 130 |
yield Message.Queue, url, data |
| 131 |
|
| 132 |
|
| 133 |
def _fallback(url): |
| 134 |
base = url[:-3] |
| 135 |
yield base + "jpeg" |
| 136 |
yield base + "png" |
| 137 |
yield base + "webp" |