2021-04-25 19:39:13 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
"""Extractors for https://fantia.jp/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2023-02-07 23:14:53 +01:00
|
|
|
from .. import text, util
|
2021-04-25 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FantiaExtractor(Extractor):
|
|
|
|
"""Base class for Fantia extractors"""
|
|
|
|
category = "fantia"
|
|
|
|
root = "https://fantia.jp"
|
|
|
|
directory_fmt = ("{category}", "{fanclub_id}")
|
|
|
|
filename_fmt = "{post_id}_{file_id}.{extension}"
|
|
|
|
archive_fmt = "{post_id}_{file_id}"
|
|
|
|
_warning = True
|
|
|
|
|
|
|
|
def items(self):
|
2023-01-27 16:18:57 +01:00
|
|
|
self.headers = {
|
|
|
|
"Accept" : "application/json, text/plain, */*",
|
|
|
|
"Referer": self.root,
|
|
|
|
}
|
2021-04-25 19:39:13 +02:00
|
|
|
|
|
|
|
if self._warning:
|
2021-12-16 02:21:16 +01:00
|
|
|
if not self._check_cookies(("_session_id",)):
|
2021-04-25 19:39:13 +02:00
|
|
|
self.log.warning("no '_session_id' cookie set")
|
|
|
|
FantiaExtractor._warning = False
|
|
|
|
|
|
|
|
for post_id in self.posts():
|
|
|
|
full_response, post = self._get_post_data(post_id)
|
|
|
|
yield Message.Directory, post
|
2022-03-08 22:06:41 +01:00
|
|
|
post["num"] = 0
|
2021-04-25 19:39:13 +02:00
|
|
|
for url, url_data in self._get_urls_from_post(full_response, post):
|
2022-03-08 22:06:41 +01:00
|
|
|
post["num"] += 1
|
2021-04-25 19:39:13 +02:00
|
|
|
fname = url_data["content_filename"] or url
|
|
|
|
text.nameext_from_url(fname, url_data)
|
|
|
|
url_data["file_url"] = url
|
|
|
|
yield Message.Url, url, url_data
|
|
|
|
|
|
|
|
def posts(self):
|
|
|
|
"""Return post IDs"""
|
|
|
|
|
|
|
|
def _pagination(self, url):
|
|
|
|
params = {"page": 1}
|
2023-01-27 16:18:57 +01:00
|
|
|
headers = self.headers
|
2021-04-25 19:39:13 +02:00
|
|
|
|
|
|
|
while True:
|
|
|
|
page = self.request(url, params=params, headers=headers).text
|
2023-01-27 16:18:57 +01:00
|
|
|
self._csrf_token(page)
|
2021-04-25 19:39:13 +02:00
|
|
|
|
|
|
|
post_id = None
|
|
|
|
for post_id in text.extract_iter(
|
|
|
|
page, 'class="link-block" href="/posts/', '"'):
|
|
|
|
yield post_id
|
|
|
|
|
|
|
|
if not post_id:
|
|
|
|
return
|
|
|
|
params["page"] += 1
|
|
|
|
|
2023-01-27 16:18:57 +01:00
|
|
|
def _csrf_token(self, page=None):
|
|
|
|
if not page:
|
|
|
|
page = self.request(self.root + "/").text
|
|
|
|
self.headers["X-CSRF-Token"] = text.extr(
|
|
|
|
page, 'name="csrf-token" content="', '"')
|
|
|
|
|
2021-04-25 19:39:13 +02:00
|
|
|
def _get_post_data(self, post_id):
|
|
|
|
"""Fetch and process post data"""
|
|
|
|
url = self.root+"/api/v1/posts/"+post_id
|
2023-01-27 16:18:57 +01:00
|
|
|
resp = self.request(url, headers=self.headers).json()["post"]
|
2021-04-25 19:39:13 +02:00
|
|
|
post = {
|
|
|
|
"post_id": resp["id"],
|
|
|
|
"post_url": self.root + "/posts/" + str(resp["id"]),
|
|
|
|
"post_title": resp["title"],
|
|
|
|
"comment": resp["comment"],
|
|
|
|
"rating": resp["rating"],
|
|
|
|
"posted_at": resp["posted_at"],
|
2021-09-17 20:09:24 +02:00
|
|
|
"date": text.parse_datetime(
|
|
|
|
resp["posted_at"], "%a, %d %b %Y %H:%M:%S %z"),
|
2021-04-25 19:39:13 +02:00
|
|
|
"fanclub_id": resp["fanclub"]["id"],
|
|
|
|
"fanclub_user_id": resp["fanclub"]["user"]["id"],
|
|
|
|
"fanclub_user_name": resp["fanclub"]["user"]["name"],
|
|
|
|
"fanclub_name": resp["fanclub"]["name"],
|
|
|
|
"fanclub_url": self.root+"/fanclubs/"+str(resp["fanclub"]["id"]),
|
|
|
|
"tags": resp["tags"]
|
|
|
|
}
|
|
|
|
return resp, post
|
|
|
|
|
|
|
|
def _get_urls_from_post(self, resp, post):
|
|
|
|
"""Extract individual URL data from the response"""
|
|
|
|
if "thumb" in resp and resp["thumb"] and "original" in resp["thumb"]:
|
|
|
|
post["content_filename"] = ""
|
|
|
|
post["content_category"] = "thumb"
|
|
|
|
post["file_id"] = "thumb"
|
|
|
|
yield resp["thumb"]["original"], post
|
|
|
|
|
|
|
|
for content in resp["post_contents"]:
|
|
|
|
post["content_category"] = content["category"]
|
|
|
|
post["content_title"] = content["title"]
|
|
|
|
post["content_filename"] = content.get("filename", "")
|
|
|
|
post["content_id"] = content["id"]
|
2022-03-11 18:02:31 +01:00
|
|
|
|
|
|
|
if "comment" in content:
|
|
|
|
post["content_comment"] = content["comment"]
|
|
|
|
|
2021-04-25 19:39:13 +02:00
|
|
|
if "post_content_photos" in content:
|
|
|
|
for photo in content["post_content_photos"]:
|
|
|
|
post["file_id"] = photo["id"]
|
|
|
|
yield photo["url"]["original"], post
|
2022-03-11 18:02:31 +01:00
|
|
|
|
2021-04-25 19:39:13 +02:00
|
|
|
if "download_uri" in content:
|
|
|
|
post["file_id"] = content["id"]
|
|
|
|
yield self.root+"/"+content["download_uri"], post
|
|
|
|
|
2022-03-11 18:02:31 +01:00
|
|
|
if content["category"] == "blog" and "comment" in content:
|
2023-02-07 23:14:53 +01:00
|
|
|
comment_json = util.json_loads(content["comment"])
|
2022-03-11 18:02:31 +01:00
|
|
|
ops = comment_json.get("ops", ())
|
|
|
|
|
|
|
|
# collect blogpost text first
|
|
|
|
blog_text = ""
|
|
|
|
for op in ops:
|
|
|
|
insert = op.get("insert")
|
|
|
|
if isinstance(insert, str):
|
|
|
|
blog_text += insert
|
|
|
|
post["blogpost_text"] = blog_text
|
|
|
|
|
|
|
|
# collect images
|
|
|
|
for op in ops:
|
|
|
|
insert = op.get("insert")
|
|
|
|
if isinstance(insert, dict) and "fantiaImage" in insert:
|
|
|
|
img = insert["fantiaImage"]
|
|
|
|
post["file_id"] = img["id"]
|
|
|
|
yield "https://fantia.jp" + img["original_url"], post
|
|
|
|
|
2021-04-25 19:39:13 +02:00
|
|
|
|
|
|
|
class FantiaCreatorExtractor(FantiaExtractor):
|
|
|
|
"""Extractor for a Fantia creator's works"""
|
|
|
|
subcategory = "creator"
|
|
|
|
pattern = r"(?:https?://)?(?:www\.)?fantia\.jp/fanclubs/(\d+)"
|
|
|
|
test = (
|
|
|
|
("https://fantia.jp/fanclubs/6939", {
|
|
|
|
"range": "1-25",
|
|
|
|
"count": ">= 25",
|
|
|
|
"keyword": {
|
|
|
|
"fanclub_user_id" : 52152,
|
|
|
|
"tags" : list,
|
|
|
|
"title" : str,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
FantiaExtractor.__init__(self, match)
|
|
|
|
self.creator_id = match.group(1)
|
|
|
|
|
|
|
|
def posts(self):
|
|
|
|
url = "{}/fanclubs/{}/posts".format(self.root, self.creator_id)
|
|
|
|
return self._pagination(url)
|
|
|
|
|
|
|
|
|
|
|
|
class FantiaPostExtractor(FantiaExtractor):
|
|
|
|
"""Extractor for media from a single Fantia post"""
|
|
|
|
subcategory = "post"
|
|
|
|
pattern = r"(?:https?://)?(?:www\.)?fantia\.jp/posts/(\d+)"
|
|
|
|
test = (
|
|
|
|
("https://fantia.jp/posts/508363", {
|
|
|
|
"count": 6,
|
|
|
|
"keyword": {
|
|
|
|
"post_title": "zunda逆バニーでおしりコッショリ",
|
|
|
|
"tags": list,
|
|
|
|
"rating": "adult",
|
|
|
|
"post_id": 508363
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
FantiaExtractor.__init__(self, match)
|
|
|
|
self.post_id = match.group(1)
|
|
|
|
|
|
|
|
def posts(self):
|
2023-01-27 16:18:57 +01:00
|
|
|
self._csrf_token()
|
2021-04-25 19:39:13 +02:00
|
|
|
return (self.post_id,)
|