| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for szurubooru instances""" |
| 10 |
|
| 11 |
from . import booru |
| 12 |
from .. import text, util |
| 13 |
import collections |
| 14 |
|
| 15 |
|
| 16 |
class SzurubooruExtractor(booru.BooruExtractor): |
| 17 |
basecategory = "szurubooru" |
| 18 |
filename_fmt = "{id}_{version}_{checksumMD5}.{extension}" |
| 19 |
per_page = 100 |
| 20 |
|
| 21 |
def _init(self): |
| 22 |
self.headers = { |
| 23 |
"Accept": "application/json", |
| 24 |
"Content-Type": "application/json", |
| 25 |
} |
| 26 |
|
| 27 |
if username := self.config("username"): |
| 28 |
self.log.debug("Using HTTP Basic Auth for account '%s'", username) |
| 29 |
self.session.auth = util.HTTPBasicAuth( |
| 30 |
username, self.config("token"), b"Token") |
| 31 |
|
| 32 |
def request(self, url, **kwargs): |
| 33 |
response = booru.BooruExtractor.request(self, url, **kwargs) |
| 34 |
if response.headers["Content-Type"].startswith("text/html") and \ |
| 35 |
b"onsent to viewing adult content<" in response.content: |
| 36 |
url = self.root + "/.gatekeeper/cmd/accept18plusUnverified" |
| 37 |
kwargs["method"] = "POST" |
| 38 |
kwargs["headers"].pop("Content-Type", None) |
| 39 |
kwargs["data"] = { |
| 40 |
"originalUrl": response.url[len(self.root):], |
| 41 |
"consent" : "on", |
| 42 |
} |
| 43 |
response = booru.BooruExtractor.request(self, url, **kwargs) |
| 44 |
return response |
| 45 |
|
| 46 |
def request_api(self, endpoint, params=None): |
| 47 |
url = f"{self.root}/api{endpoint}" |
| 48 |
return self.request_json(url, headers=self.headers, params=params) |
| 49 |
|
| 50 |
def _pagination(self, endpoint, params): |
| 51 |
params["offset"] = 0 |
| 52 |
params["limit"] = self.per_page |
| 53 |
|
| 54 |
while True: |
| 55 |
data = self.request_api(endpoint, params) |
| 56 |
results = data["results"] |
| 57 |
|
| 58 |
yield from results |
| 59 |
|
| 60 |
if len(results) < self.per_page: |
| 61 |
return |
| 62 |
params["offset"] += len(results) |
| 63 |
|
| 64 |
def _file_url(self, post): |
| 65 |
url = post["contentUrl"] |
| 66 |
if not url.startswith("http"): |
| 67 |
url = f"{self.root}/{url}" |
| 68 |
return url |
| 69 |
|
| 70 |
def _prepare(self, post): |
| 71 |
post["date"] = self.parse_datetime_iso(post["creationTime"]) |
| 72 |
|
| 73 |
tags = [] |
| 74 |
tags_categories = collections.defaultdict(list) |
| 75 |
for tag in post["tags"]: |
| 76 |
tag_type = tag["category"].rpartition("_")[2] |
| 77 |
tag_name = tag["names"][0] |
| 78 |
tags_categories[tag_type].append(tag_name) |
| 79 |
tags.append(tag_name) |
| 80 |
|
| 81 |
post["tags"] = tags |
| 82 |
for category, tags in tags_categories.items(): |
| 83 |
post["tags_" + category] = tags |
| 84 |
|
| 85 |
|
| 86 |
BASE_PATTERN = SzurubooruExtractor.update({ |
| 87 |
"bcbnsfw": { |
| 88 |
"root": "https://booru.bcbnsfw.space", |
| 89 |
"pattern": r"booru\.bcbnsfw\.space", |
| 90 |
"query-all": "*", |
| 91 |
}, |
| 92 |
"snootbooru": { |
| 93 |
"root": "https://snootbooru.com", |
| 94 |
"pattern": r"snootbooru\.com", |
| 95 |
}, |
| 96 |
"visuabusters": { |
| 97 |
"root": "https://www.visuabusters.com/booru", |
| 98 |
"pattern": r"(?:www\.)?visuabusters\.com/booru", |
| 99 |
}, |
| 100 |
"ricardoadacor": { |
| 101 |
"root": "https://ricardo.adacor.org", |
| 102 |
"pattern": r"ricardo\.adacor\.org", |
| 103 |
}, |
| 104 |
}) |
| 105 |
|
| 106 |
|
| 107 |
class SzurubooruTagExtractor(SzurubooruExtractor): |
| 108 |
subcategory = "tag" |
| 109 |
directory_fmt = ("{category}", "{search_tags}") |
| 110 |
archive_fmt = "t_{search_tags}_{id}_{version}" |
| 111 |
pattern = BASE_PATTERN + r"/posts(?:/query=([^/?#]*))?" |
| 112 |
example = "https://booru.bcbnsfw.space/posts/query=TAG" |
| 113 |
|
| 114 |
def metadata(self): |
| 115 |
query = self.groups[-1] |
| 116 |
self.query = text.unquote(query.replace("+", " ")) if query else "" |
| 117 |
return {"search_tags": self.query} |
| 118 |
|
| 119 |
def posts(self): |
| 120 |
if self.query.strip(): |
| 121 |
query = self.query |
| 122 |
else: |
| 123 |
query = self.config_instance("query-all") |
| 124 |
|
| 125 |
return self._pagination("/posts/", {"query": query}) |
| 126 |
|
| 127 |
|
| 128 |
class SzurubooruPostExtractor(SzurubooruExtractor): |
| 129 |
subcategory = "post" |
| 130 |
archive_fmt = "{id}_{version}" |
| 131 |
pattern = BASE_PATTERN + r"/post/(\d+)" |
| 132 |
example = "https://booru.bcbnsfw.space/post/12345" |
| 133 |
|
| 134 |
def posts(self): |
| 135 |
return (self.request_api("/post/" + self.groups[-1]),) |