| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Extractors for https://rule34vault.com/""" |
| 8 |
|
| 9 |
from .booru import BooruExtractor |
| 10 |
from .. import text |
| 11 |
import collections |
| 12 |
|
| 13 |
BASE_PATTERN = r"(?:https?://)?rule34vault\.com" |
| 14 |
|
| 15 |
|
| 16 |
class Rule34vaultExtractor(BooruExtractor): |
| 17 |
category = "rule34vault" |
| 18 |
root = "https://rule34vault.com" |
| 19 |
root_cdn = "https://r34xyz.b-cdn.net" |
| 20 |
filename_fmt = "{category}_{id}.{extension}" |
| 21 |
per_page = 100 |
| 22 |
|
| 23 |
TAG_TYPES = { |
| 24 |
None: "general", |
| 25 |
1 : "general", |
| 26 |
2 : "copyright", |
| 27 |
4 : "character", |
| 28 |
8 : "artist", |
| 29 |
16 : "system", |
| 30 |
32 : "meta", |
| 31 |
} |
| 32 |
|
| 33 |
def _file_url(self, post): |
| 34 |
post_id = post["id"] |
| 35 |
extension = "jpg" if post["type"] == 0 else "mp4" |
| 36 |
post["file_url"] = url = (f"{self.root_cdn}/posts/{post_id // 1000}/" |
| 37 |
f"{post_id}/{post_id}.{extension}") |
| 38 |
return url |
| 39 |
|
| 40 |
def _prepare(self, post): |
| 41 |
post.pop("files", None) |
| 42 |
post["date"] = self.parse_datetime_iso(post["created"]) |
| 43 |
if "tags" in post: |
| 44 |
post["tags"] = [t["value"] for t in post["tags"]] |
| 45 |
|
| 46 |
def _tags(self, post, _): |
| 47 |
if "tags" not in post: |
| 48 |
post.update(self._fetch_post(post["id"])) |
| 49 |
|
| 50 |
tags = collections.defaultdict(list) |
| 51 |
for tag in post["tags"]: |
| 52 |
tags[tag["type"]].append(tag["value"]) |
| 53 |
types = self.TAG_TYPES |
| 54 |
for type, values in tags.items(): |
| 55 |
post["tags_" + types[type]] = values |
| 56 |
|
| 57 |
def _fetch_post(self, post_id): |
| 58 |
url = f"{self.root}/api/v2/post/{post_id}" |
| 59 |
return self.request_json(url) |
| 60 |
|
| 61 |
def _pagination(self, endpoint, params=None): |
| 62 |
url = f"{self.root}/api{endpoint}" |
| 63 |
|
| 64 |
if params is None: |
| 65 |
params = {} |
| 66 |
params["CountTotal"] = False |
| 67 |
params["Skip"] = self.page_start * self.per_page |
| 68 |
params["take"] = self.per_page |
| 69 |
threshold = self.per_page |
| 70 |
|
| 71 |
while True: |
| 72 |
data = self.request_json(url, method="POST", json=params) |
| 73 |
|
| 74 |
yield from data["items"] |
| 75 |
|
| 76 |
if len(data["items"]) < threshold: |
| 77 |
return |
| 78 |
params["cursor"] = data.get("cursor") |
| 79 |
params["Skip"] += params["take"] |
| 80 |
|
| 81 |
|
| 82 |
class Rule34vaultPostExtractor(Rule34vaultExtractor): |
| 83 |
subcategory = "post" |
| 84 |
archive_fmt = "{id}" |
| 85 |
pattern = BASE_PATTERN + r"/post/(\d+)" |
| 86 |
example = "https://rule34vault.com/post/12345" |
| 87 |
|
| 88 |
def posts(self): |
| 89 |
return (self._fetch_post(self.groups[0]),) |
| 90 |
|
| 91 |
|
| 92 |
class Rule34vaultPlaylistExtractor(Rule34vaultExtractor): |
| 93 |
subcategory = "playlist" |
| 94 |
directory_fmt = ("{category}", "{playlist_id}") |
| 95 |
archive_fmt = "p_{playlist_id}_{id}" |
| 96 |
pattern = BASE_PATTERN + r"/playlists/view/(\d+)" |
| 97 |
example = "https://rule34vault.com/playlists/view/12345" |
| 98 |
|
| 99 |
def metadata(self): |
| 100 |
return {"playlist_id": self.groups[0]} |
| 101 |
|
| 102 |
def posts(self): |
| 103 |
endpoint = "/v2/post/search/playlist/" + self.groups[0] |
| 104 |
return self._pagination(endpoint) |
| 105 |
|
| 106 |
|
| 107 |
class Rule34vaultTagExtractor(Rule34vaultExtractor): |
| 108 |
subcategory = "tag" |
| 109 |
directory_fmt = ("{category}", "{search_tags}") |
| 110 |
archive_fmt = "t_{search_tags}_{id}" |
| 111 |
pattern = BASE_PATTERN + r"/(?!p(?:ost|laylists)/)([^/?#]+)" |
| 112 |
example = "https://rule34vault.com/TAG" |
| 113 |
|
| 114 |
def metadata(self): |
| 115 |
self.tags = text.unquote(self.groups[0]).split("|") |
| 116 |
return {"search_tags": " ".join(self.tags)} |
| 117 |
|
| 118 |
def posts(self): |
| 119 |
endpoint = "/v2/post/search/root" |
| 120 |
params = {"includeTags": [t.replace("_", " ") for t in self.tags]} |
| 121 |
return self._pagination(endpoint, params) |