| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2015-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Extractors for nijie instances""" |
| 10 |
|
| 11 |
from .common import BaseExtractor, Message, Dispatch |
| 12 |
from .. import text, util, dt |
| 13 |
|
| 14 |
|
| 15 |
class NijieExtractor(BaseExtractor): |
| 16 |
"""Base class for nijie extractors""" |
| 17 |
basecategory = "Nijie" |
| 18 |
directory_fmt = ("{category}", "{user_id}") |
| 19 |
filename_fmt = "{image_id}_p{num}.{extension}" |
| 20 |
archive_fmt = "{image_id}_{num}" |
| 21 |
request_interval = (2.0, 4.0) |
| 22 |
async_mode = True |
| 23 |
|
| 24 |
def __init__(self, match): |
| 25 |
BaseExtractor.__init__(self, match) |
| 26 |
self.user_id = text.parse_int(self.groups[-1]) |
| 27 |
|
| 28 |
def initialize(self): |
| 29 |
self.cookies_domain = "." + self.root.rpartition("/")[2] |
| 30 |
self.cookies_names = (self.category + "_tok",) |
| 31 |
|
| 32 |
BaseExtractor.initialize(self) |
| 33 |
|
| 34 |
self.user_name = None |
| 35 |
if self.category == "horne": |
| 36 |
self._extract_data = self._extract_data_horne |
| 37 |
|
| 38 |
def items(self): |
| 39 |
self.login() |
| 40 |
|
| 41 |
for image_id in self.image_ids(): |
| 42 |
|
| 43 |
url = f"{self.root}/view.php?id={image_id}" |
| 44 |
response = self.request(url, fatal=False) |
| 45 |
if response.status_code >= 400: |
| 46 |
continue |
| 47 |
page = response.text |
| 48 |
|
| 49 |
data = self._extract_data(page) |
| 50 |
data["image_id"] = text.parse_int(image_id) |
| 51 |
|
| 52 |
if self.user_name: |
| 53 |
data["user_id"] = self.user_id |
| 54 |
data["user_name"] = self.user_name |
| 55 |
else: |
| 56 |
data["user_id"] = data["artist_id"] |
| 57 |
data["user_name"] = data["artist_name"] |
| 58 |
|
| 59 |
urls = self._extract_images(image_id, page) |
| 60 |
data["count"] = len(urls) |
| 61 |
|
| 62 |
yield Message.Directory, "", data |
| 63 |
for num, url in enumerate(urls): |
| 64 |
image = text.nameext_from_url(url, { |
| 65 |
"num": num, |
| 66 |
"url": "https:" + url, |
| 67 |
}) |
| 68 |
image.update(data) |
| 69 |
if not image["extension"]: |
| 70 |
image["extension"] = "jpg" |
| 71 |
yield Message.Url, image["url"], image |
| 72 |
|
| 73 |
def image_ids(self): |
| 74 |
"""Collect all relevant image-ids""" |
| 75 |
|
| 76 |
def _extract_data(self, page): |
| 77 |
"""Extract image metadata from 'page'""" |
| 78 |
extr = text.extract_from(page) |
| 79 |
keywords = text.unescape(extr( |
| 80 |
'name="keywords" content="', '" />')).split(",") |
| 81 |
return { |
| 82 |
"title" : keywords[0].strip(), |
| 83 |
"description": text.unescape(extr( |
| 84 |
'"description": "', '"').replace("&", "&")), |
| 85 |
"date" : dt.parse(extr( |
| 86 |
'"datePublished": "', '"'), "%a %b %d %H:%M:%S %Y" |
| 87 |
) - dt.timedelta(hours=9), |
| 88 |
"artist_id" : text.parse_int(extr('/members.php?id=', '"')), |
| 89 |
"artist_name": keywords[1], |
| 90 |
"tags" : keywords[2:-1], |
| 91 |
} |
| 92 |
|
| 93 |
def _extract_data_horne(self, page): |
| 94 |
"""Extract image metadata from 'page'""" |
| 95 |
extr = text.extract_from(page) |
| 96 |
keywords = text.unescape(extr( |
| 97 |
'name="keywords" content="', '" />')).split(",") |
| 98 |
return { |
| 99 |
"title" : keywords[0].strip(), |
| 100 |
"description": text.unescape(extr( |
| 101 |
'property="og:description" content="', '"')), |
| 102 |
"artist_id" : text.parse_int(extr('members.php?id=', '"')), |
| 103 |
"artist_name": keywords[1], |
| 104 |
"tags" : keywords[2:-1], |
| 105 |
"date" : dt.parse_iso(extr( |
| 106 |
"itemprop='datePublished' content=", "<").rpartition(">")[2] |
| 107 |
) - dt.timedelta(hours=9), |
| 108 |
} |
| 109 |
|
| 110 |
def _extract_images(self, image_id, page): |
| 111 |
if '&#diff_1" ' in page: |
| 112 |
# multiple images |
| 113 |
url = f"{self.root}/view_popup.php?id={image_id}" |
| 114 |
page = self.request(url).text |
| 115 |
return [ |
| 116 |
text.extr(media, ' src="', '"') |
| 117 |
for media in text.extract_iter( |
| 118 |
page, 'href="javascript:void(0);"><', '>') |
| 119 |
if ' src="' in media |
| 120 |
] |
| 121 |
else: |
| 122 |
pos = page.find('id="view-center"') + 1 |
| 123 |
# do NOT use text.extr() here, as it doesn't support a pos argument |
| 124 |
return (text.extract(page, 'itemprop="image" src="', '"', pos)[0],) |
| 125 |
|
| 126 |
def _extract_user_name(self, page): |
| 127 |
return text.unescape(text.extr(page, "<br />", "<")) |
| 128 |
|
| 129 |
def login(self): |
| 130 |
if self.cookies_check(self.cookies_names): |
| 131 |
return |
| 132 |
|
| 133 |
username, password = self._get_auth_info() |
| 134 |
if username: |
| 135 |
return self.cookies_update(self.cache( |
| 136 |
self._login_impl, username, password, |
| 137 |
_exp=90*86400, _mem=False)) |
| 138 |
|
| 139 |
raise self.exc.AuthenticationError("Username and password required") |
| 140 |
|
| 141 |
def _login_impl(self, username, password): |
| 142 |
self.log.info("Logging in as %s", username) |
| 143 |
|
| 144 |
url = self.root + "/login_int.php" |
| 145 |
data = {"email": username, "password": password, "save": "on"} |
| 146 |
|
| 147 |
response = self.request(url, method="POST", data=data) |
| 148 |
if "/login.php" in response.text: |
| 149 |
raise self.exc.AuthenticationError() |
| 150 |
return self.cookies |
| 151 |
|
| 152 |
def _pagination(self, path): |
| 153 |
url = f"{self.root}/{path}.php" |
| 154 |
params = {"id": self.user_id, "p": 1} |
| 155 |
|
| 156 |
while True: |
| 157 |
page = self.request(url, params=params, notfound="artist").text |
| 158 |
|
| 159 |
if self.user_name is None: |
| 160 |
self.user_name = self._extract_user_name(page) |
| 161 |
yield from util.unique_sequence(text.extract_iter( |
| 162 |
page, 'illust_id="', '"')) |
| 163 |
|
| 164 |
if '<a rel="next"' not in page: |
| 165 |
return |
| 166 |
params["p"] += 1 |
| 167 |
|
| 168 |
|
| 169 |
BASE_PATTERN = NijieExtractor.update({ |
| 170 |
"nijie": { |
| 171 |
"root": "https://nijie.info", |
| 172 |
"pattern": r"(?:www\.)?nijie\.info", |
| 173 |
}, |
| 174 |
"horne": { |
| 175 |
"root": "https://horne.red", |
| 176 |
"pattern": r"(?:www\.)?horne\.red", |
| 177 |
}, |
| 178 |
}) |
| 179 |
|
| 180 |
|
| 181 |
class NijieUserExtractor(Dispatch, NijieExtractor): |
| 182 |
"""Extractor for nijie user profiles""" |
| 183 |
pattern = BASE_PATTERN + r"/members\.php\?id=(\d+)" |
| 184 |
example = "https://nijie.info/members.php?id=12345" |
| 185 |
|
| 186 |
def items(self): |
| 187 |
fmt = f"{self.root}/{{}}.php?id={self.user_id}".format |
| 188 |
return self._dispatch_extractors(( |
| 189 |
(NijieIllustrationExtractor, fmt("members_illust")), |
| 190 |
(NijieDoujinExtractor , fmt("members_dojin")), |
| 191 |
(NijieFavoriteExtractor , fmt("user_like_illust_view")), |
| 192 |
(NijieNuitaExtractor , fmt("history_nuita")), |
| 193 |
), ("illustration", "doujin")) |
| 194 |
|
| 195 |
|
| 196 |
class NijieIllustrationExtractor(NijieExtractor): |
| 197 |
"""Extractor for all illustrations of a nijie-user""" |
| 198 |
subcategory = "illustration" |
| 199 |
pattern = BASE_PATTERN + r"/members_illust\.php\?id=(\d+)" |
| 200 |
example = "https://nijie.info/members_illust.php?id=12345" |
| 201 |
|
| 202 |
def image_ids(self): |
| 203 |
return self._pagination("members_illust") |
| 204 |
|
| 205 |
|
| 206 |
class NijieDoujinExtractor(NijieExtractor): |
| 207 |
"""Extractor for doujin entries of a nijie user""" |
| 208 |
subcategory = "doujin" |
| 209 |
pattern = BASE_PATTERN + r"/members_dojin\.php\?id=(\d+)" |
| 210 |
example = "https://nijie.info/members_dojin.php?id=12345" |
| 211 |
|
| 212 |
def image_ids(self): |
| 213 |
return self._pagination("members_dojin") |
| 214 |
|
| 215 |
|
| 216 |
class NijieFavoriteExtractor(NijieExtractor): |
| 217 |
"""Extractor for all favorites/bookmarks of a nijie user""" |
| 218 |
subcategory = "favorite" |
| 219 |
directory_fmt = ("{category}", "bookmarks", "{user_id}") |
| 220 |
archive_fmt = "f_{user_id}_{image_id}_{num}" |
| 221 |
pattern = BASE_PATTERN + r"/user_like_illust_view\.php\?id=(\d+)" |
| 222 |
example = "https://nijie.info/user_like_illust_view.php?id=12345" |
| 223 |
|
| 224 |
def image_ids(self): |
| 225 |
return self._pagination("user_like_illust_view") |
| 226 |
|
| 227 |
def _extract_data(self, page): |
| 228 |
data = NijieExtractor._extract_data(page) |
| 229 |
data["user_id"] = self.user_id |
| 230 |
data["user_name"] = self.user_name |
| 231 |
return data |
| 232 |
|
| 233 |
|
| 234 |
class NijieNuitaExtractor(NijieExtractor): |
| 235 |
"""Extractor for a nijie user's 抜いた list""" |
| 236 |
subcategory = "nuita" |
| 237 |
directory_fmt = ("{category}", "nuita", "{user_id}") |
| 238 |
archive_fmt = "n_{user_id}_{image_id}_{num}" |
| 239 |
pattern = BASE_PATTERN + r"/history_nuita\.php\?id=(\d+)" |
| 240 |
example = "https://nijie.info/history_nuita.php?id=12345" |
| 241 |
|
| 242 |
def image_ids(self): |
| 243 |
return self._pagination("history_nuita") |
| 244 |
|
| 245 |
def _extract_data(self, page): |
| 246 |
data = NijieExtractor._extract_data(page) |
| 247 |
data["user_id"] = self.user_id |
| 248 |
data["user_name"] = self.user_name |
| 249 |
return data |
| 250 |
|
| 251 |
def _extract_user_name(self, page): |
| 252 |
return text.unescape(text.extr(page, "<title>", "さんの抜いた")) |
| 253 |
|
| 254 |
|
| 255 |
class NijieFeedExtractor(NijieExtractor): |
| 256 |
"""Extractor for nijie liked user feed""" |
| 257 |
subcategory = "feed" |
| 258 |
pattern = BASE_PATTERN + r"/like_user_view\.php" |
| 259 |
example = "https://nijie.info/like_user_view.php" |
| 260 |
|
| 261 |
def image_ids(self): |
| 262 |
return self._pagination("like_user_view") |
| 263 |
|
| 264 |
def _extract_user_name(self, page): |
| 265 |
return "" |
| 266 |
|
| 267 |
|
| 268 |
class NijieFollowedExtractor(NijieExtractor): |
| 269 |
"""Extractor for followed nijie users""" |
| 270 |
subcategory = "followed" |
| 271 |
pattern = BASE_PATTERN + r"/like_my\.php" |
| 272 |
example = "https://nijie.info/like_my.php" |
| 273 |
|
| 274 |
def items(self): |
| 275 |
self.login() |
| 276 |
|
| 277 |
url = self.root + "/like_my.php" |
| 278 |
params = {"p": 1} |
| 279 |
data = {"_extractor": NijieUserExtractor} |
| 280 |
|
| 281 |
while True: |
| 282 |
page = self.request(url, params=params).text |
| 283 |
|
| 284 |
for user_id in text.extract_iter( |
| 285 |
page, '"><a href="/members.php?id=', '"'): |
| 286 |
user_url = f"{self.root}/members.php?id={user_id}" |
| 287 |
yield Message.Queue, user_url, data |
| 288 |
|
| 289 |
if '<a rel="next"' not in page: |
| 290 |
return |
| 291 |
params["p"] += 1 |
| 292 |
|
| 293 |
|
| 294 |
class NijieImageExtractor(NijieExtractor): |
| 295 |
"""Extractor for a nijie work/image""" |
| 296 |
subcategory = "image" |
| 297 |
pattern = BASE_PATTERN + r"/view(?:_popup)?\.php\?id=(\d+)" |
| 298 |
example = "https://nijie.info/view.php?id=12345" |
| 299 |
|
| 300 |
def image_ids(self): |
| 301 |
return (self.groups[-1],) |