2015-05-04 20:40:53 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-30 17:09:32 +01:00
|
|
|
# Copyright 2015-2019 Mike Fährmann
|
2015-05-04 20:40:53 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
"""Extract images from https://nijie.info/"""
|
|
|
|
|
2015-10-03 15:43:02 +02:00
|
|
|
from .common import AsynchronousExtractor, Message
|
2018-04-20 14:53:21 +02:00
|
|
|
from .. import text, exception
|
2016-03-06 21:07:00 +01:00
|
|
|
from ..cache import cache
|
2015-05-04 20:40:53 +02:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2016-09-19 08:51:49 +02:00
|
|
|
class NijieExtractor(AsynchronousExtractor):
|
|
|
|
"""Base class for nijie extractors"""
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "nijie"
|
2018-03-31 18:48:41 +02:00
|
|
|
directory_fmt = ["{category}", "{user_id}"]
|
2017-09-10 22:20:47 +02:00
|
|
|
filename_fmt = "{category}_{artist_id}_{image_id}_p{index:>02}.{extension}"
|
2018-01-30 22:49:16 +01:00
|
|
|
archive_fmt = "{image_id}_{index}"
|
2017-07-22 15:43:35 +02:00
|
|
|
cookiedomain = "nijie.info"
|
2019-01-30 17:09:32 +01:00
|
|
|
cookienames = ("nemail", "nlogin")
|
2018-03-31 18:48:41 +02:00
|
|
|
root = "https://nijie.info"
|
2018-07-10 11:30:17 +02:00
|
|
|
view_url = "https://nijie.info/view.php?id="
|
2015-05-04 20:40:53 +02:00
|
|
|
popup_url = "https://nijie.info/view_popup.php?id="
|
|
|
|
|
2018-03-31 16:20:08 +02:00
|
|
|
def __init__(self, match=None):
|
2015-10-05 15:35:48 +02:00
|
|
|
AsynchronousExtractor.__init__(self)
|
2018-03-31 18:48:41 +02:00
|
|
|
self.session.headers["Referer"] = self.root + "/"
|
|
|
|
self.user_id = match.group(1) if match else None
|
2015-05-04 20:40:53 +02:00
|
|
|
|
|
|
|
def items(self):
|
2017-01-08 17:33:25 +01:00
|
|
|
self.login()
|
2015-05-04 20:40:53 +02:00
|
|
|
data = self.get_job_metadata()
|
2018-03-31 18:48:41 +02:00
|
|
|
|
2015-05-04 20:40:53 +02:00
|
|
|
yield Message.Version, 1
|
|
|
|
yield Message.Directory, data
|
2018-03-31 18:48:41 +02:00
|
|
|
|
|
|
|
for image_id in self.get_image_ids():
|
2015-05-04 20:40:53 +02:00
|
|
|
for image_url, image_data in self.get_image_data(image_id):
|
|
|
|
image_data.update(data)
|
2018-02-09 21:51:35 +01:00
|
|
|
if not image_data["extension"]:
|
|
|
|
image_data["extension"] = "jpg"
|
2015-05-04 20:40:53 +02:00
|
|
|
yield Message.Url, image_url, image_data
|
|
|
|
|
|
|
|
def get_job_metadata(self):
|
|
|
|
"""Collect metadata for extractor-job"""
|
2018-04-20 14:53:21 +02:00
|
|
|
return {"user_id": text.parse_int(self.user_id)}
|
2015-05-04 20:40:53 +02:00
|
|
|
|
|
|
|
def get_image_ids(self):
|
2018-03-31 18:48:41 +02:00
|
|
|
"""Collect all relevant image-ids"""
|
2015-05-04 20:40:53 +02:00
|
|
|
|
|
|
|
def get_image_data(self, image_id):
|
|
|
|
"""Get URL and metadata for images specified by 'image_id'"""
|
2018-07-10 11:30:17 +02:00
|
|
|
page = self.request(self.view_url + image_id).text
|
2016-09-19 08:51:49 +02:00
|
|
|
return self.extract_image_data(page, image_id)
|
|
|
|
|
2018-03-31 18:48:41 +02:00
|
|
|
def extract_image_data(self, page, image_id):
|
2016-09-19 08:51:49 +02:00
|
|
|
"""Get URL and metadata for images from 'page'"""
|
2018-03-31 18:48:41 +02:00
|
|
|
title, pos = text.extract(
|
|
|
|
page, '<meta property="og:title" content="', '"')
|
|
|
|
description, pos = text.extract(
|
|
|
|
page, '<meta property="og:description" content="', '"', pos)
|
2018-07-10 11:30:17 +02:00
|
|
|
artist_id, pos = text.extract(
|
|
|
|
page, '"sameAs": "https://nijie.info/members.php?id=', '"', pos)
|
|
|
|
images = list(text.extract_iter(
|
|
|
|
page, '<a href="./view_popup.php', '</a>', pos))
|
2018-03-31 18:48:41 +02:00
|
|
|
|
|
|
|
title = title.rpartition("|")[0].strip()
|
2018-04-20 14:53:21 +02:00
|
|
|
image_id = text.parse_int(image_id)
|
2018-07-10 11:30:17 +02:00
|
|
|
artist_id = text.parse_int(artist_id)
|
2018-03-31 18:48:41 +02:00
|
|
|
|
2018-07-10 11:30:17 +02:00
|
|
|
for index, image in enumerate(images):
|
|
|
|
url = "https:" + text.extract(image, 'src="', '"')[0]
|
|
|
|
url = url.replace("/__rs_l120x120/", "/", 1)
|
|
|
|
|
|
|
|
yield url, text.nameext_from_url(url, {
|
2015-05-04 20:40:53 +02:00
|
|
|
"index": index,
|
2018-03-31 18:48:41 +02:00
|
|
|
"count": len(images),
|
|
|
|
"title": title,
|
|
|
|
"description": description,
|
2017-09-10 22:20:47 +02:00
|
|
|
"image_id": image_id,
|
2018-03-31 18:48:41 +02:00
|
|
|
"artist_id": artist_id,
|
2015-11-16 17:32:26 +01:00
|
|
|
})
|
2015-12-11 00:07:21 +01:00
|
|
|
|
2017-01-08 17:33:25 +01:00
|
|
|
def login(self):
|
2019-01-30 17:09:32 +01:00
|
|
|
"""Login and obtain session cookies"""
|
|
|
|
if not self._check_cookies(self.cookienames):
|
2017-07-25 14:59:41 +02:00
|
|
|
username, password = self._get_auth_info()
|
2019-01-30 17:09:32 +01:00
|
|
|
self._update_cookies(self._login_impl(username, password))
|
2017-01-08 17:33:25 +01:00
|
|
|
|
2019-01-30 17:09:32 +01:00
|
|
|
@cache(maxage=150*24*60*60, keyarg=1)
|
2017-01-08 17:33:25 +01:00
|
|
|
def _login_impl(self, username, password):
|
2017-03-17 09:42:59 +01:00
|
|
|
self.log.info("Logging in as %s", username)
|
2019-01-30 17:09:32 +01:00
|
|
|
url = "{}/login_int.php".format(self.root)
|
|
|
|
data = {"email": username, "password": password, "save": "on"}
|
|
|
|
|
|
|
|
response = self.request(url, method="POST", data=data)
|
|
|
|
if "//nijie.info/login.php" in response.text:
|
2016-07-20 14:21:13 +02:00
|
|
|
raise exception.AuthenticationError()
|
2016-03-06 21:07:00 +01:00
|
|
|
return self.session.cookies
|
2016-09-19 08:51:49 +02:00
|
|
|
|
2018-03-31 16:20:08 +02:00
|
|
|
def _pagination(self, path):
|
2018-03-31 18:48:41 +02:00
|
|
|
url = "{}/{}.php".format(self.root, path)
|
|
|
|
params = {"id": self.user_id, "p": 1}
|
|
|
|
|
2018-03-31 16:20:08 +02:00
|
|
|
while True:
|
2018-06-17 21:49:13 +02:00
|
|
|
response = self.request(url, params=params, expect=(404,))
|
2018-03-31 16:20:08 +02:00
|
|
|
if response.status_code == 404:
|
|
|
|
raise exception.NotFoundError("artist")
|
2018-03-31 18:48:41 +02:00
|
|
|
|
|
|
|
page = response.text
|
|
|
|
ids = list(text.extract_iter(page, ' illust_id="', '"'))
|
2018-03-31 16:20:08 +02:00
|
|
|
yield from ids
|
2018-03-31 18:48:41 +02:00
|
|
|
|
|
|
|
if '<a rel="next"' not in page:
|
2018-03-31 16:20:08 +02:00
|
|
|
return
|
|
|
|
params["p"] += 1
|
|
|
|
|
2016-09-19 08:51:49 +02:00
|
|
|
|
|
|
|
class NijieUserExtractor(NijieExtractor):
|
|
|
|
"""Extractor for works of a nijie-user"""
|
|
|
|
subcategory = "user"
|
2018-03-31 18:48:41 +02:00
|
|
|
pattern = [(r"(?:https?://)?(?:www\.)?nijie\.info"
|
|
|
|
r"/members(?:_illust)?\.php\?id=(\d+)")]
|
2016-12-30 01:46:42 +01:00
|
|
|
test = [
|
|
|
|
("https://nijie.info/members_illust.php?id=44", {
|
|
|
|
"url": "585d821df4716b1098660a0be426d01db4b65f2a",
|
2018-03-31 18:48:41 +02:00
|
|
|
"keyword": "1eb3387196f1f30d6d74a41f4c77faaadd588e52",
|
2016-12-30 01:46:42 +01:00
|
|
|
}),
|
|
|
|
("https://nijie.info/members_illust.php?id=43", {
|
|
|
|
"exception": exception.NotFoundError,
|
|
|
|
}),
|
2018-03-31 18:48:41 +02:00
|
|
|
("https://nijie.info/members.php?id=44", None),
|
2016-12-30 01:46:42 +01:00
|
|
|
]
|
2016-09-19 08:51:49 +02:00
|
|
|
|
2018-03-31 16:20:08 +02:00
|
|
|
def get_image_ids(self):
|
2018-03-31 18:48:41 +02:00
|
|
|
return self._pagination("members_illust")
|
2018-03-31 16:20:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class NijieDoujinExtractor(NijieExtractor):
|
|
|
|
"""Extractor for doujin entries of a nijie-user"""
|
|
|
|
subcategory = "doujin"
|
|
|
|
pattern = [(r"(?:https?://)?(?:www\.)?nijie\.info/"
|
|
|
|
r"members_dojin\.php\?id=(\d+)")]
|
|
|
|
test = [
|
|
|
|
("https://nijie.info/members_dojin.php?id=6782", {
|
|
|
|
"count": ">= 18",
|
2018-03-31 18:48:41 +02:00
|
|
|
}),
|
2018-03-31 16:20:08 +02:00
|
|
|
]
|
2016-09-19 08:51:49 +02:00
|
|
|
|
|
|
|
def get_image_ids(self):
|
2018-03-31 18:48:41 +02:00
|
|
|
return self._pagination("members_dojin")
|
2016-09-19 08:51:49 +02:00
|
|
|
|
|
|
|
|
2018-03-31 18:54:25 +02:00
|
|
|
class NijieFavoriteExtractor(NijieExtractor):
|
|
|
|
"""Extractor for all favorites/bookmarks of a nijie-user"""
|
|
|
|
subcategory = "favorite"
|
|
|
|
directory_fmt = ["{category}", "bookmarks", "{user_id}"]
|
|
|
|
archive_fmt = "f_{user_id}_{image_id}_{index}"
|
|
|
|
pattern = [(r"(?:https?://)?(?:www\.)?nijie\.info"
|
|
|
|
r"/user_like_illust_view\.php\?id=(\d+)")]
|
|
|
|
test = [
|
|
|
|
("https://nijie.info/user_like_illust_view.php?id=44", {
|
|
|
|
"count": ">= 16",
|
|
|
|
}),
|
|
|
|
]
|
|
|
|
|
|
|
|
def get_image_ids(self):
|
|
|
|
return self._pagination("user_like_illust_view")
|
|
|
|
|
|
|
|
|
2016-09-19 08:51:49 +02:00
|
|
|
class NijieImageExtractor(NijieExtractor):
|
|
|
|
"""Extractor for a work/image from nijie.info"""
|
|
|
|
subcategory = "image"
|
2018-03-31 18:48:41 +02:00
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?nijie\.info"
|
|
|
|
r"/view(?:_popup)?\.php\?id=(\d+)"]
|
2016-12-30 01:46:42 +01:00
|
|
|
test = [
|
|
|
|
("https://nijie.info/view.php?id=70720", {
|
|
|
|
"url": "a10d4995645b5f260821e32c60a35f73546c2699",
|
2018-03-31 18:48:41 +02:00
|
|
|
"keyword": "0728fc3bbef1e192abfd59f88f07921d3d336804",
|
2016-12-30 01:46:42 +01:00
|
|
|
"content": "d85e3ea896ed5e4da0bca2390ad310a4df716ca6",
|
|
|
|
}),
|
|
|
|
("https://nijie.info/view.php?id=70724", {
|
|
|
|
"exception": exception.NotFoundError,
|
|
|
|
}),
|
2018-03-31 18:48:41 +02:00
|
|
|
("https://nijie.info/view_popup.php?id=70720", None),
|
2016-12-30 01:46:42 +01:00
|
|
|
]
|
2016-09-19 08:51:49 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
NijieExtractor.__init__(self)
|
|
|
|
self.image_id = match.group(1)
|
|
|
|
self.page = ""
|
|
|
|
|
|
|
|
def get_job_metadata(self):
|
2018-07-10 11:30:17 +02:00
|
|
|
response = self.request(self.view_url + self.image_id, expect=(404,))
|
|
|
|
if response.status_code == 404:
|
2016-09-19 08:51:49 +02:00
|
|
|
raise exception.NotFoundError("image")
|
|
|
|
self.page = response.text
|
2018-07-10 11:30:17 +02:00
|
|
|
self.user_id = text.extract(
|
|
|
|
self.page, '"sameAs": "https://nijie.info/members.php?id=', '"')[0]
|
2016-09-19 08:51:49 +02:00
|
|
|
return NijieExtractor.get_job_metadata(self)
|
|
|
|
|
|
|
|
def get_image_ids(self):
|
|
|
|
return (self.image_id,)
|
|
|
|
|
|
|
|
def get_image_data(self, _):
|
|
|
|
return self.extract_image_data(self.page, self.image_id)
|