| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://whitekitten.art/""" |
| 8 |
|
| 9 |
from .booru import BooruExtractor |
| 10 |
from .common import Message |
| 11 |
from .. import text |
| 12 |
import collections |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?whitekitten\.art" |
| 15 |
|
| 16 |
|
| 17 |
class WhitekittenExtractor(BooruExtractor): |
| 18 |
"""Base class for whitekitten extractors""" |
| 19 |
category = "whitekitten" |
| 20 |
root = "https://whitekitten.art" |
| 21 |
filename_fmt = "{category}_{id}_{file[fileOrder]}.{extension}" |
| 22 |
archive_fmt = "{id}_{file[id]}" |
| 23 |
per_page = 100 |
| 24 |
|
| 25 |
def _init(self): |
| 26 |
self.params = {"maxRating": self.config("max-rating", "explicit")} |
| 27 |
|
| 28 |
def items(self): |
| 29 |
for post in self.posts(): |
| 30 |
self._prepare_post(post) |
| 31 |
files = post.pop("files", ()) |
| 32 |
post["count"] = len(files) |
| 33 |
|
| 34 |
yield Message.Directory, "", post |
| 35 |
for post["num"], file in enumerate(files, 1): |
| 36 |
post["file"] = self._prepare_file(file) |
| 37 |
post["_fallback"] = \ |
| 38 |
(png,) if (png := file.get("pngUrl")) else () |
| 39 |
url = file["mediaUrl"] |
| 40 |
text.nameext_from_url(url, post) |
| 41 |
yield Message.Url, url, post |
| 42 |
|
| 43 |
def request_post(self, post_id): |
| 44 |
return self.request_json( |
| 45 |
f"{self.root}/api/posts/{post_id}", params=self.params, |
| 46 |
)["data"]["post"] |
| 47 |
|
| 48 |
def _pagination(self, params): |
| 49 |
url = self.root + "/api/posts" |
| 50 |
params = {"filterMode": "hidden", **self.params, **params} |
| 51 |
|
| 52 |
while True: |
| 53 |
data = self.request_json(url, params=params)["data"] |
| 54 |
|
| 55 |
yield from (data.get("posts") or ()) |
| 56 |
|
| 57 |
params["cursor"] = cursor = data.get("cursor") |
| 58 |
if not cursor: |
| 59 |
break |
| 60 |
|
| 61 |
def _prepare_post(self, post): |
| 62 |
# post-list endpoint returns summary rows without 'files'/'tags'; |
| 63 |
# fetch detail to fill those in |
| 64 |
if "files" not in post: |
| 65 |
post.update(self.request_post(post["id"])) |
| 66 |
post["date"] = self.parse_datetime_iso(post["createdAt"]) |
| 67 |
post["date_updated"] = self.parse_datetime_iso(post["updatedAt"]) |
| 68 |
return self._split_tags(post, post.get("tags") or ()) |
| 69 |
|
| 70 |
def _prepare_file(self, file): |
| 71 |
tags = file.get("tags") or [] |
| 72 |
if extra := file.get("inheritedTags"): |
| 73 |
tags.extend(extra) |
| 74 |
if extra := file.get("impliedTags"): |
| 75 |
tags.extend(extra) |
| 76 |
|
| 77 |
seen = set() |
| 78 |
unique = [] |
| 79 |
for tag in tags: |
| 80 |
tid = tag.get("id") |
| 81 |
if tid not in seen: |
| 82 |
seen.add(tid) |
| 83 |
unique.append(tag) |
| 84 |
|
| 85 |
return self._split_tags(file, unique) |
| 86 |
|
| 87 |
def _split_tags(self, obj, tags): |
| 88 |
names = [] |
| 89 |
grouped = collections.defaultdict(list) |
| 90 |
|
| 91 |
for tag in tags: |
| 92 |
name = (tag.get("name") or "").rpartition(":")[2] |
| 93 |
names.append(name) |
| 94 |
grouped[tag.get("category") or "uncategorized"].append(name) |
| 95 |
|
| 96 |
names.sort() |
| 97 |
obj["tags"] = names |
| 98 |
for cat, tlist in grouped.items(): |
| 99 |
tlist.sort() |
| 100 |
obj["tags_" + cat] = tlist |
| 101 |
return obj |
| 102 |
|
| 103 |
|
| 104 |
class WhitekittenTagExtractor(WhitekittenExtractor): |
| 105 |
"""Extractor for whitekitten tag searches""" |
| 106 |
subcategory = "tag" |
| 107 |
directory_fmt = ("{category}", "{search_tags}") |
| 108 |
pattern = BASE_PATTERN + r"/?(?:\?([^#]*))?$" |
| 109 |
example = "https://whitekitten.art/?q=TAG" |
| 110 |
|
| 111 |
def posts(self): |
| 112 |
params = text.parse_query(self.groups[0]) |
| 113 |
self.kwdict["search_tags"] = params.get("q", "") |
| 114 |
return self._pagination(params) |
| 115 |
|
| 116 |
|
| 117 |
class WhitekittenPostExtractor(WhitekittenExtractor): |
| 118 |
"""Extractor for a single whitekitten post""" |
| 119 |
subcategory = "post" |
| 120 |
pattern = BASE_PATTERN + r"/posts/(\d+)" |
| 121 |
example = "https://whitekitten.art/posts/12345" |
| 122 |
|
| 123 |
def posts(self): |
| 124 |
return (self.request_post(self.groups[0]),) |