1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 11:12:40 +01:00
gallery-dl/gallery_dl/extractor/nijie.py

80 lines
3.1 KiB
Python
Raw Normal View History

2015-05-04 20:40:53 +02:00
# -*- coding: utf-8 -*-
2016-03-06 21:07:00 +01:00
# Copyright 2015, 2016 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
2016-07-20 14:21:13 +02:00
from .. import config, text, exception
2016-03-06 21:07:00 +01:00
from ..cache import cache
2015-05-04 20:40:53 +02:00
class NijieUserExtractor(AsynchronousExtractor):
"""Extract all works of a single nijie-user"""
2015-11-21 04:26:30 +01:00
category = "nijie"
directory_fmt = ["{category}", "{artist-id}"]
filename_fmt = "{category}_{artist-id}_{image-id}_p{index:>02}.{extension}"
pattern = [r"(?:https?://)?(?:www\.)?nijie\.info/members(?:_illust)?\.php\?id=(\d+)"]
2015-12-13 04:36:44 +01:00
test = [("https://nijie.info/members_illust.php?id=44", {
"url": "585d821df4716b1098660a0be426d01db4b65f2a",
"keyword": "30c981b9d7351ec275b9840d8bc2b4ef3da8c4b4",
})]
2015-05-04 20:40:53 +02:00
popup_url = "https://nijie.info/view_popup.php?id="
def __init__(self, match):
AsynchronousExtractor.__init__(self)
2015-05-04 20:40:53 +02:00
self.artist_id = match.group(1)
self.artist_url = (
"https://nijie.info/members_illust.php?id="
+ self.artist_id
)
self.session.headers["Referer"] = self.artist_url
def items(self):
2016-03-06 21:07:00 +01:00
self.session.cookies = self.login(
config.interpolate(("extractor", self.category, "username")),
2016-03-06 21:07:00 +01:00
config.interpolate(("extractor", self.category, "password"))
)
2015-05-04 20:40:53 +02:00
data = self.get_job_metadata()
yield Message.Version, 1
yield Message.Directory, data
for image_id in self.get_image_ids():
for image_url, image_data in self.get_image_data(image_id):
image_data.update(data)
yield Message.Url, image_url, image_data
def get_job_metadata(self):
"""Collect metadata for extractor-job"""
return {
2015-11-21 04:26:30 +01:00
"category": self.category,
2015-05-04 20:40:53 +02:00
"artist-id": self.artist_id,
}
def get_image_ids(self):
2015-10-03 15:43:02 +02:00
"""Collect all image-ids for a specific artist"""
page = self.request(self.artist_url).text
return list(text.extract_iter(page, ' illust_id="', '"'))
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'"""
page = self.request(self.popup_url + image_id).text
images = list(text.extract_iter(page, '<img src="//pic', '"'))
for index, url in enumerate(images):
yield "https://pic" + url, text.nameext_from_url(url, {
"count": len(images),
2015-05-04 20:40:53 +02:00
"index": index,
"image-id": image_id,
2015-11-16 17:32:26 +01:00
})
2016-03-06 21:07:00 +01:00
@cache(maxage=30*24*60*60, keyarg=1)
def login(self, username, password):
"""Login and obtain session cookie"""
params = {"email": username, "password": password}
page = self.session.post("https://nijie.info/login_int.php", data=params).text
if "//nijie.info/login.php" in page:
2016-07-20 14:21:13 +02:00
raise exception.AuthenticationError()
2016-03-06 21:07:00 +01:00
return self.session.cookies