| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
"""Extractors for https://fantia.jp/""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text, util |
| 11 |
|
| 12 |
|
| 13 |
class FantiaExtractor(Extractor): |
| 14 |
"""Base class for Fantia extractors""" |
| 15 |
category = "fantia" |
| 16 |
root = "https://fantia.jp" |
| 17 |
directory_fmt = ("{category}", "{fanclub_id}") |
| 18 |
filename_fmt = "{post_id}_{file_id}.{extension}" |
| 19 |
archive_fmt = "{post_id}_{file_id}" |
| 20 |
_warning = True |
| 21 |
|
| 22 |
def _init(self): |
| 23 |
self.headers = { |
| 24 |
"Accept" : "application/json, text/plain, */*", |
| 25 |
"X-Requested-With": "XMLHttpRequest", |
| 26 |
} |
| 27 |
self._empty_plan = { |
| 28 |
"id" : 0, |
| 29 |
"price": 0, |
| 30 |
"limit": 0, |
| 31 |
"name" : "", |
| 32 |
"description": "", |
| 33 |
"thumb": self.root + "/images/fallback/plan/thumb_default.png", |
| 34 |
} |
| 35 |
if self._warning: |
| 36 |
if not self.cookies_check(("_session_id",)): |
| 37 |
self.log.warning("no '_session_id' cookie set") |
| 38 |
FantiaExtractor._warning = False |
| 39 |
|
| 40 |
def items(self): |
| 41 |
for post_id in self.posts(): |
| 42 |
post = self._get_post_data(post_id) |
| 43 |
post["num"] = 0 |
| 44 |
|
| 45 |
contents = self._get_post_contents(post) |
| 46 |
post["content_count"] = len(contents) |
| 47 |
post["content_num"] = 0 |
| 48 |
|
| 49 |
for content in contents: |
| 50 |
files = self._process_content(post, content) |
| 51 |
yield Message.Directory, "", post |
| 52 |
|
| 53 |
if content["visible_status"] != "visible": |
| 54 |
self.log.warning( |
| 55 |
"Unable to download '%s' files from " |
| 56 |
"%s#post-content-id-%s", content["visible_status"], |
| 57 |
post["post_url"], content["id"]) |
| 58 |
|
| 59 |
for file in files: |
| 60 |
post.update(file) |
| 61 |
post["num"] += 1 |
| 62 |
text.nameext_from_url( |
| 63 |
post["content_filename"] or file["file_url"], post) |
| 64 |
yield Message.Url, file["file_url"], post |
| 65 |
|
| 66 |
post["content_num"] += 1 |
| 67 |
|
| 68 |
def posts(self): |
| 69 |
"""Return post IDs""" |
| 70 |
|
| 71 |
def _pagination(self, url, params, needle=""): |
| 72 |
params["page"] = 1 |
| 73 |
|
| 74 |
while True: |
| 75 |
page = self.request(url, params=params).text |
| 76 |
self._csrf_token(page) |
| 77 |
|
| 78 |
target_id = None |
| 79 |
for target_id in text.extract_iter(page, needle, '"'): |
| 80 |
yield target_id |
| 81 |
|
| 82 |
if not target_id: |
| 83 |
return |
| 84 |
params["page"] += 1 |
| 85 |
|
| 86 |
def _csrf_token(self, page=None): |
| 87 |
if not page: |
| 88 |
page = self.request(self.root + "/").text |
| 89 |
self.headers["X-CSRF-Token"] = text.extr( |
| 90 |
page, 'name="csrf-token" content="', '"') |
| 91 |
|
| 92 |
def _get_post_data(self, post_id): |
| 93 |
"""Fetch and process post data""" |
| 94 |
url = self.root+"/api/v1/posts/"+post_id |
| 95 |
resp = self.request_json(url, headers=self.headers)["post"] |
| 96 |
return { |
| 97 |
"post_id": resp["id"], |
| 98 |
"post_url": self.root + "/posts/" + str(resp["id"]), |
| 99 |
"post_title": resp["title"], |
| 100 |
"comment": resp["comment"], |
| 101 |
"rating": resp["rating"], |
| 102 |
"posted_at": resp["posted_at"], |
| 103 |
"date": self.parse_datetime( |
| 104 |
resp["posted_at"], "%a, %d %b %Y %H:%M:%S %z"), |
| 105 |
"fanclub_id": resp["fanclub"]["id"], |
| 106 |
"fanclub_user_id": resp["fanclub"]["user"]["id"], |
| 107 |
"fanclub_user_name": resp["fanclub"]["user"]["name"], |
| 108 |
"fanclub_name": resp["fanclub"]["name"], |
| 109 |
"fanclub_url": self.root+"/fanclubs/"+str(resp["fanclub"]["id"]), |
| 110 |
"tags": [t["name"] for t in resp["tags"]], |
| 111 |
"_data": resp, |
| 112 |
} |
| 113 |
|
| 114 |
def _get_post_contents(self, post): |
| 115 |
contents = post["_data"]["post_contents"] |
| 116 |
|
| 117 |
try: |
| 118 |
url = post["_data"]["thumb"]["original"] |
| 119 |
except Exception: |
| 120 |
pass |
| 121 |
else: |
| 122 |
contents.insert(0, { |
| 123 |
"id": "thumb", |
| 124 |
"title": "thumb", |
| 125 |
"category": "thumb", |
| 126 |
"download_uri": url, |
| 127 |
"visible_status": "visible", |
| 128 |
"plan": None, |
| 129 |
}) |
| 130 |
|
| 131 |
return contents |
| 132 |
|
| 133 |
def _process_content(self, post, content): |
| 134 |
post["content_category"] = content["category"] |
| 135 |
post["content_title"] = content["title"] |
| 136 |
post["content_filename"] = content.get("filename") or "" |
| 137 |
post["content_id"] = content["id"] |
| 138 |
post["content_comment"] = content.get("comment") or "" |
| 139 |
post["content_num"] += 1 |
| 140 |
post["plan"] = content["plan"] or self._empty_plan |
| 141 |
|
| 142 |
files = [] |
| 143 |
|
| 144 |
if "post_content_photos" in content: |
| 145 |
for photo in content["post_content_photos"]: |
| 146 |
files.append({"file_id" : photo["id"], |
| 147 |
"file_url": photo["url"]["original"]}) |
| 148 |
|
| 149 |
if "download_uri" in content: |
| 150 |
url = content["download_uri"] |
| 151 |
if url[0] == "/": |
| 152 |
url = self.root + url |
| 153 |
files.append({"file_id" : content["id"], |
| 154 |
"file_url": url}) |
| 155 |
|
| 156 |
if content["category"] == "blog" and "comment" in content: |
| 157 |
comment_json = util.json_loads(content["comment"]) |
| 158 |
|
| 159 |
blog_text = "" |
| 160 |
for op in comment_json.get("ops") or (): |
| 161 |
insert = op.get("insert") |
| 162 |
if isinstance(insert, str): |
| 163 |
blog_text += insert |
| 164 |
elif isinstance(insert, dict) and "fantiaImage" in insert: |
| 165 |
img = insert["fantiaImage"] |
| 166 |
files.append({"file_id" : img["id"], |
| 167 |
"file_url": self.root + img["original_url"]}) |
| 168 |
post["blogpost_text"] = blog_text |
| 169 |
else: |
| 170 |
post["blogpost_text"] = "" |
| 171 |
|
| 172 |
return files |
| 173 |
|
| 174 |
|
| 175 |
class FantiaCreatorExtractor(FantiaExtractor): |
| 176 |
"""Extractor for a Fantia creator's works""" |
| 177 |
subcategory = "creator" |
| 178 |
pattern = r"(?:https?://)?(?:www\.)?fantia\.jp/fanclubs/(\d+)" |
| 179 |
example = "https://fantia.jp/fanclubs/12345" |
| 180 |
|
| 181 |
def posts(self): |
| 182 |
url = f"{self.root}/fanclubs/{self.groups[0]}/posts" |
| 183 |
return self._pagination(url, {}, 'class="link-block" href="/posts/') |
| 184 |
|
| 185 |
|
| 186 |
class FantiaPostExtractor(FantiaExtractor): |
| 187 |
"""Extractor for media from a single Fantia post""" |
| 188 |
subcategory = "post" |
| 189 |
pattern = r"(?:https?://)?(?:www\.)?fantia\.jp/posts/(\d+)" |
| 190 |
example = "https://fantia.jp/posts/12345" |
| 191 |
|
| 192 |
def posts(self): |
| 193 |
self._csrf_token() |
| 194 |
return (self.groups[0],) |
| 195 |
|
| 196 |
|
| 197 |
class FantiaSupportingExtractor(FantiaExtractor): |
| 198 |
"""Extractor for free and paid supporting fanclubs for the current user""" |
| 199 |
subcategory = "supporting" |
| 200 |
pattern = (r"(?:https?://)?(?:www\.)?fantia\.jp/mypage/users/plans" |
| 201 |
r"(?:\?type=((?:not_)?free))?") |
| 202 |
example = "https://fantia.jp/mypage/users/plans" |
| 203 |
|
| 204 |
def items(self): |
| 205 |
url = self.root + "/mypage/users/plans" |
| 206 |
base = self.root + "/fanclubs/" |
| 207 |
data = {"_extractor": FantiaCreatorExtractor} |
| 208 |
|
| 209 |
type = self.groups[0] |
| 210 |
for plan_type in ("not_free", "free") if type is None else (type,): |
| 211 |
params = {"type": plan_type} |
| 212 |
for fanclub_id in self._pagination( |
| 213 |
url, params, 'class="user-avatar" href="/fanclubs/'): |
| 214 |
yield Message.Queue, base + fanclub_id, data |