| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://poipiku.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?poipiku\.com" |
| 15 |
|
| 16 |
|
| 17 |
class PoipikuExtractor(Extractor): |
| 18 |
"""Base class for poipiku extractors""" |
| 19 |
category = "poipiku" |
| 20 |
root = "https://poipiku.com" |
| 21 |
directory_fmt = ("{category}", "{user_id} {user_name}") |
| 22 |
filename_fmt = "{post_id}_{num}.{extension}" |
| 23 |
archive_fmt = "{post_id}_{num}" |
| 24 |
cookies_names = ("POIPIKU_LK",) |
| 25 |
cookies_domain = "poipiku.com" |
| 26 |
cookies_warning = True |
| 27 |
request_interval = (0.5, 1.5) |
| 28 |
|
| 29 |
def _init(self): |
| 30 |
self.cookies.set( |
| 31 |
"LANG", "en", domain=self.cookies_domain) |
| 32 |
self.cookies.set( |
| 33 |
"POIPIKU_CONTENTS_VIEW_MODE", "1", domain=self.cookies_domain) |
| 34 |
self.headers = { |
| 35 |
"Accept" : "application/json, text/javascript, */*; q=0.01", |
| 36 |
"X-Requested-With": "XMLHttpRequest", |
| 37 |
"Origin" : self.root, |
| 38 |
"Referer": None, |
| 39 |
"Sec-Fetch-Dest": "empty", |
| 40 |
"Sec-Fetch-Mode": "cors", |
| 41 |
"Sec-Fetch-Site": "same-origin", |
| 42 |
} |
| 43 |
self.password = self.config("passwords", "") |
| 44 |
|
| 45 |
def items(self): |
| 46 |
logged_in = self.login() |
| 47 |
extract_files = (self._extract_files_auth if logged_in else |
| 48 |
self._extract_files_noauth) |
| 49 |
|
| 50 |
for post_url in self.posts(): |
| 51 |
if post_url[0] == "/": |
| 52 |
post_url = self.root + post_url |
| 53 |
page = self.request(post_url).text |
| 54 |
extr = text.extract_from(page) |
| 55 |
parts = post_url.rsplit("/", 2) |
| 56 |
post = { |
| 57 |
"post_category": extr("<title>[", "]"), |
| 58 |
"count" : text.parse_int(extr("(", " ")), |
| 59 |
"post_id" : parts[2].partition(".")[0], |
| 60 |
"user_id" : parts[1], |
| 61 |
"user_name" : text.unescape(extr( |
| 62 |
'<h2 class="UserInfoUserName">', '</').rpartition(">")[2]), |
| 63 |
"description": text.unescape(extr( |
| 64 |
'class="IllustItemDesc" >', '</h1>')), |
| 65 |
"warning" : False, |
| 66 |
"password" : False, |
| 67 |
"requires" : None, |
| 68 |
"original" : logged_in, |
| 69 |
"_http_headers": {"Referer": post_url}, |
| 70 |
} |
| 71 |
|
| 72 |
thumb = self._extract_thumb(post, extr) |
| 73 |
self.headers["Referer"] = post_url |
| 74 |
|
| 75 |
if post["requires"] and not post["password"] and extr( |
| 76 |
"PasswordIcon", ">"): |
| 77 |
post["password"] = True |
| 78 |
|
| 79 |
yield Message.Directory, "", post |
| 80 |
for post["num"], url in enumerate(extract_files( |
| 81 |
post, thumb, extr), 1): |
| 82 |
yield Message.Url, url, text.nameext_from_url(url, post) |
| 83 |
|
| 84 |
def login(self): |
| 85 |
if self.cookies_check(self.cookies_names): |
| 86 |
return True |
| 87 |
|
| 88 |
username, password = self._get_auth_info() |
| 89 |
if username: |
| 90 |
self.cookies_update(self.cache( |
| 91 |
self._login_impl, username, password, |
| 92 |
_exp=365*86400, _mem=False)) |
| 93 |
return True |
| 94 |
|
| 95 |
if self.cookies_warning: |
| 96 |
self.log.warning("no 'POIPIKU_LK' cookie set") |
| 97 |
PoipikuExtractor.cookies_warning = False |
| 98 |
return False |
| 99 |
|
| 100 |
def _login_impl(self, username, password): |
| 101 |
self.log.info("Logging in as %s", username) |
| 102 |
|
| 103 |
url = self.root + "/f/LoginUserF.jsp" |
| 104 |
headers = { |
| 105 |
"X-Requested-With": "XMLHttpRequest", |
| 106 |
"Origin": self.root, |
| 107 |
"Referer": url, |
| 108 |
} |
| 109 |
data = { |
| 110 |
"EM": username, |
| 111 |
"PW": password, |
| 112 |
} |
| 113 |
response = self.request(url, method="POST", headers=headers, data=data) |
| 114 |
|
| 115 |
if value := response.cookies.get("POIPIKU_LK"): |
| 116 |
return {"POIPIKU_LK": value} |
| 117 |
raise self.exc.AuthenticationError() |
| 118 |
|
| 119 |
def _extract_thumb(self, post, extr): |
| 120 |
thumb = "" |
| 121 |
|
| 122 |
while True: |
| 123 |
img = extr('class="IllustItemThumbImg" src="', '"') |
| 124 |
|
| 125 |
if not img: |
| 126 |
return thumb |
| 127 |
elif img.startswith("https://cdn.poipiku.com/img/"): |
| 128 |
self.log.debug("%s: %s", post["post_id"], img) |
| 129 |
type = text.rextr(img, "/", ".") |
| 130 |
if type == "warning": |
| 131 |
post["warning"] = True |
| 132 |
elif type == "publish_pass": |
| 133 |
post["password"] = True |
| 134 |
elif type == "publish_login": |
| 135 |
post["requires"] = "login" |
| 136 |
elif type == "publish_follower": |
| 137 |
post["requires"] = "follow" |
| 138 |
elif type == "publish_t_rt": |
| 139 |
post["requires"] = "retweet" |
| 140 |
elif img.startswith(( |
| 141 |
"https://img.poipiku.com/img/", |
| 142 |
"//img.poipiku.com/img/", |
| 143 |
"/img/", |
| 144 |
)): |
| 145 |
self.log.debug("%s: %s", post["post_id"], img) |
| 146 |
if "/warning" in img: |
| 147 |
post["warning"] = True |
| 148 |
else: |
| 149 |
thumb = img |
| 150 |
|
| 151 |
def _extract_files_auth(self, post, thumb, extr): |
| 152 |
data = self._show_illust_detail(post) |
| 153 |
|
| 154 |
if data.get("error_code"): |
| 155 |
data = self._show_append_file(post) |
| 156 |
html = data["html"] |
| 157 |
self.log.warning("%s: '%s'", |
| 158 |
post["post_id"], html.replace("<br/>", " ")) |
| 159 |
return () |
| 160 |
return text.extract_iter(data["html"], 'src="', '"') |
| 161 |
|
| 162 |
def _extract_files_noauth(self, post, thumb, extr): |
| 163 |
if thumb: |
| 164 |
if not extr('ShowAppendFile', '<'): |
| 165 |
return (thumb,) |
| 166 |
files = [thumb] |
| 167 |
else: |
| 168 |
files = [] |
| 169 |
|
| 170 |
data = self._show_append_file(post) |
| 171 |
html = data["html"] |
| 172 |
if (data.get("result_num") or 0) < 0: |
| 173 |
self.log.warning("%s: '%s'", |
| 174 |
post["post_id"], html.replace("<br/>", " ")) |
| 175 |
|
| 176 |
files.extend(text.extract_iter( |
| 177 |
html, 'class="IllustItemThumbImg" src="', '"')) |
| 178 |
return files |
| 179 |
|
| 180 |
def _show_illust_detail(self, post): |
| 181 |
url = self.root + "/f/ShowIllustDetailF.jsp" |
| 182 |
data = { |
| 183 |
"ID" : post["user_id"], |
| 184 |
"TD" : post["post_id"], |
| 185 |
"AD" : "-1", |
| 186 |
"PAS": self.password, |
| 187 |
} |
| 188 |
return self.request_json( |
| 189 |
url, method="POST", headers=self.headers, data=data, |
| 190 |
interval=False) |
| 191 |
|
| 192 |
def _show_append_file(self, post): |
| 193 |
url = self.root + "/f/ShowAppendFileF.jsp" |
| 194 |
data = { |
| 195 |
"UID": post["user_id"], |
| 196 |
"IID": post["post_id"], |
| 197 |
"PAS": self.password, |
| 198 |
"MD" : "0", |
| 199 |
"TWF": "-1", |
| 200 |
} |
| 201 |
return self.request_json( |
| 202 |
url, method="POST", headers=self.headers, data=data, |
| 203 |
interval=False) |
| 204 |
|
| 205 |
|
| 206 |
class PoipikuUserExtractor(PoipikuExtractor): |
| 207 |
"""Extractor for posts from a poipiku user""" |
| 208 |
subcategory = "user" |
| 209 |
pattern = (BASE_PATTERN + r"/(?:IllustListPcV\.jsp\?PG=(\d+)&ID=)?" |
| 210 |
r"(\d+)/?(?:$|[?&#])") |
| 211 |
example = "https://poipiku.com/12345/" |
| 212 |
|
| 213 |
def posts(self): |
| 214 |
pnum, user_id = self.groups |
| 215 |
|
| 216 |
url = self.root + "/IllustListPcV.jsp" |
| 217 |
params = { |
| 218 |
"PG" : text.parse_int(pnum, 0), |
| 219 |
"ID" : user_id, |
| 220 |
"KWD": "", |
| 221 |
} |
| 222 |
|
| 223 |
while True: |
| 224 |
page = self.request(url, params=params).text |
| 225 |
|
| 226 |
cnt = 0 |
| 227 |
for path in text.extract_iter( |
| 228 |
page, 'class="IllustInfo" href="', '"'): |
| 229 |
yield path |
| 230 |
cnt += 1 |
| 231 |
|
| 232 |
if cnt < 48: |
| 233 |
return |
| 234 |
params["PG"] += 1 |
| 235 |
|
| 236 |
|
| 237 |
class PoipikuPostExtractor(PoipikuExtractor): |
| 238 |
"""Extractor for a poipiku post""" |
| 239 |
subcategory = "post" |
| 240 |
pattern = BASE_PATTERN + r"/(\d+)/(\d+)" |
| 241 |
example = "https://poipiku.com/12345/12345.html" |
| 242 |
|
| 243 |
def posts(self): |
| 244 |
user_id, post_id = self.groups |
| 245 |
return (f"/{user_id}/{post_id}.html",) |