| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://furry34.com/""" |
| 10 |
|
| 11 |
from .booru import BooruExtractor |
| 12 |
from .. import text |
| 13 |
import collections |
| 14 |
|
| 15 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?furry34\.com" |
| 16 |
|
| 17 |
|
| 18 |
class Furry34Extractor(BooruExtractor): |
| 19 |
category = "furry34" |
| 20 |
root = "https://furry34.com" |
| 21 |
root_cdn = "https://furry34com.b-cdn.net" |
| 22 |
filename_fmt = "{category}_{id}.{extension}" |
| 23 |
per_page = 30 |
| 24 |
|
| 25 |
TAG_TYPES = { |
| 26 |
None: "general", |
| 27 |
1 : "general", |
| 28 |
2 : "copyright", |
| 29 |
4 : "character", |
| 30 |
8 : "artist", |
| 31 |
16 : "system", |
| 32 |
32 : "meta", |
| 33 |
} |
| 34 |
FORMATS = ( |
| 35 |
("100", "mov.mp4"), |
| 36 |
("101", "mov720.mp4"), |
| 37 |
("102", "mov480.mp4"), |
| 38 |
("10" , "pic.jpg"), |
| 39 |
) |
| 40 |
|
| 41 |
def _file_url(self, post): |
| 42 |
files = post["files"] |
| 43 |
for fmt, extension in self.FORMATS: |
| 44 |
if fmt in files: |
| 45 |
break |
| 46 |
else: |
| 47 |
fmt = next(iter(files)) |
| 48 |
|
| 49 |
post_id = post["id"] |
| 50 |
root = self.root_cdn if files[fmt][0] else self.root |
| 51 |
post["file_url"] = url = \ |
| 52 |
f"{root}/posts/{post_id // 1000}/{post_id}/{post_id}.{extension}" |
| 53 |
post["format_id"] = fmt |
| 54 |
post["format"] = extension.partition(".")[0] |
| 55 |
|
| 56 |
return url |
| 57 |
|
| 58 |
def _prepare(self, post): |
| 59 |
post.pop("files", None) |
| 60 |
post["date"] = self.parse_datetime_iso(post["created"]) |
| 61 |
post["filename"], _, post["format"] = post["filename"].rpartition(".") |
| 62 |
if "tags" in post: |
| 63 |
post["tags"] = [t["value"] for t in post["tags"]] |
| 64 |
|
| 65 |
def _tags(self, post, _): |
| 66 |
if "tags" not in post: |
| 67 |
post.update(self._fetch_post(post["id"])) |
| 68 |
|
| 69 |
tags = collections.defaultdict(list) |
| 70 |
for tag in post["tags"]: |
| 71 |
tags[tag["type"] or 1].append(tag["value"]) |
| 72 |
types = self.TAG_TYPES |
| 73 |
for type, values in tags.items(): |
| 74 |
post["tags_" + types[type]] = values |
| 75 |
|
| 76 |
def _fetch_post(self, post_id): |
| 77 |
url = f"{self.root}/api/v2/post/{post_id}" |
| 78 |
return self.request_json(url) |
| 79 |
|
| 80 |
def _pagination(self, endpoint, params=None): |
| 81 |
url = f"{self.root}/api{endpoint}" |
| 82 |
|
| 83 |
if params is None: |
| 84 |
params = {} |
| 85 |
params["sortBy"] = 0 |
| 86 |
params["take"] = self.per_page |
| 87 |
threshold = self.per_page |
| 88 |
|
| 89 |
while True: |
| 90 |
data = self.request_json(url, method="POST", json=params) |
| 91 |
|
| 92 |
yield from data["items"] |
| 93 |
|
| 94 |
if len(data["items"]) < threshold: |
| 95 |
return |
| 96 |
params["cursor"] = data.get("cursor") |
| 97 |
|
| 98 |
|
| 99 |
class Furry34PostExtractor(Furry34Extractor): |
| 100 |
subcategory = "post" |
| 101 |
archive_fmt = "{id}" |
| 102 |
pattern = BASE_PATTERN + r"/post/(\d+)" |
| 103 |
example = "https://furry34.com/post/12345" |
| 104 |
|
| 105 |
def posts(self): |
| 106 |
return (self._fetch_post(self.groups[0]),) |
| 107 |
|
| 108 |
|
| 109 |
class Furry34PlaylistExtractor(Furry34Extractor): |
| 110 |
subcategory = "playlist" |
| 111 |
directory_fmt = ("{category}", "{playlist_id}") |
| 112 |
archive_fmt = "p_{playlist_id}_{id}" |
| 113 |
pattern = BASE_PATTERN + r"/playlists/view/(\d+)" |
| 114 |
example = "https://furry34.com/playlists/view/12345" |
| 115 |
|
| 116 |
def metadata(self): |
| 117 |
return {"playlist_id": self.groups[0]} |
| 118 |
|
| 119 |
def posts(self): |
| 120 |
endpoint = "/v2/post/search/playlist/" + self.groups[0] |
| 121 |
return self._pagination(endpoint) |
| 122 |
|
| 123 |
|
| 124 |
class Furry34TagExtractor(Furry34Extractor): |
| 125 |
subcategory = "tag" |
| 126 |
directory_fmt = ("{category}", "{search_tags}") |
| 127 |
archive_fmt = "t_{search_tags}_{id}" |
| 128 |
pattern = BASE_PATTERN + r"/(?:([^/?#]+))?(?:/?\?([^#]+))?(?:$|#)" |
| 129 |
example = "https://furry34.com/TAG" |
| 130 |
|
| 131 |
def _init(self): |
| 132 |
tag, query = self.groups |
| 133 |
params = text.parse_query(query) |
| 134 |
|
| 135 |
self.tags = tags = [] |
| 136 |
if tag: |
| 137 |
tags.extend(text.unquote(text.unquote(tag)).split("|")) |
| 138 |
if "tags" in params: |
| 139 |
tags.extend(params["tags"].split("|")) |
| 140 |
|
| 141 |
type = params.get("type") |
| 142 |
if type == "video": |
| 143 |
self.type = 1 |
| 144 |
elif type == "image": |
| 145 |
self.type = 0 |
| 146 |
else: |
| 147 |
self.type = None |
| 148 |
|
| 149 |
def metadata(self): |
| 150 |
return {"search_tags": " ".join(self.tags)} |
| 151 |
|
| 152 |
def posts(self): |
| 153 |
endpoint = "/v2/post/search/root" |
| 154 |
params = {"includeTags": [t.replace("_", " ") for t in self.tags]} |
| 155 |
if self.type is not None: |
| 156 |
params["type"] = self.type |
| 157 |
return self._pagination(endpoint, params) |