2015-04-10 15:29:09 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-03-06 21:00:42 +01:00
|
|
|
# Copyright 2014-2016 Mike Fährmann
|
2015-04-10 15:29:09 +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 and ugoira from http://www.pixiv.net/"""
|
|
|
|
|
2015-10-05 17:15:31 +02:00
|
|
|
from .common import Extractor, Message
|
2015-10-05 15:35:48 +02:00
|
|
|
from .. import config, text
|
2016-03-06 21:00:42 +01:00
|
|
|
from ..cache import cache
|
2014-10-12 21:56:44 +02:00
|
|
|
import re
|
2015-05-14 19:08:20 +02:00
|
|
|
import json
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-11-22 02:21:02 +01:00
|
|
|
class PixivUserExtractor(Extractor):
|
|
|
|
"""Extract all works of a single pixiv-user"""
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "pixiv"
|
2015-11-30 01:11:13 +01:00
|
|
|
subcategory = "user"
|
2015-11-21 04:26:30 +01:00
|
|
|
directory_fmt = ["{category}", "{artist-id}-{artist-nick}"]
|
|
|
|
filename_fmt = "{category}_{artist-id}_{id}{num}.{extension}"
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?pixiv\.net/member(?:_illust)?\.php\?id=(\d+)"]
|
2015-12-14 03:00:58 +01:00
|
|
|
test = [("http://www.pixiv.net/member_illust.php?id=173530", {
|
|
|
|
"url": "8f2fc0437e2095ab750c4340a4eba33ec6269477",
|
|
|
|
})]
|
2014-10-12 21:56:44 +02:00
|
|
|
member_url = "http://www.pixiv.net/member_illust.php"
|
|
|
|
illust_url = "http://www.pixiv.net/member_illust.php?mode=medium"
|
|
|
|
|
2015-10-05 15:35:48 +02:00
|
|
|
def __init__(self, match):
|
2015-10-05 17:15:31 +02:00
|
|
|
Extractor.__init__(self)
|
2015-04-10 15:29:09 +02:00
|
|
|
self.artist_id = match.group(1)
|
2015-05-14 19:08:20 +02:00
|
|
|
self.api = PixivAPI(self.session)
|
2015-11-23 00:15:43 +01:00
|
|
|
self.api_call = self.api.user_works
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-04-10 15:29:09 +02:00
|
|
|
def items(self):
|
2015-05-14 19:08:20 +02:00
|
|
|
metadata = self.get_job_metadata()
|
2015-04-10 15:29:09 +02:00
|
|
|
yield Message.Version, 1
|
2015-04-10 17:32:36 +02:00
|
|
|
yield Message.Headers, self.session.headers
|
|
|
|
yield Message.Cookies, self.session.cookies
|
2015-05-14 19:08:20 +02:00
|
|
|
yield Message.Directory, metadata
|
|
|
|
|
|
|
|
for work in self.get_works():
|
2015-06-28 12:36:30 +02:00
|
|
|
pos = work["extension"].rfind("?", -18)
|
|
|
|
if pos != -1:
|
|
|
|
timestamp = work["extension"][pos:]
|
|
|
|
work["extension"] = work["extension"][:pos]
|
|
|
|
else:
|
|
|
|
timestamp = ""
|
|
|
|
|
2015-05-14 19:08:20 +02:00
|
|
|
if work["type"] == "ugoira":
|
2015-05-20 10:25:11 +02:00
|
|
|
url, framelist = self.parse_ugoira(work)
|
2015-05-14 19:08:20 +02:00
|
|
|
work["extension"] = "zip"
|
2015-11-23 00:15:43 +01:00
|
|
|
yield Message.Url, url, work
|
2015-05-14 19:08:20 +02:00
|
|
|
work["extension"] = "txt"
|
|
|
|
yield Message.Url, "text://"+framelist, work
|
|
|
|
|
|
|
|
elif work["page_count"] == 1:
|
|
|
|
yield Message.Url, work["url"], work
|
2014-10-12 21:56:44 +02:00
|
|
|
|
|
|
|
else:
|
2015-05-14 19:08:20 +02:00
|
|
|
url = work["url"]
|
|
|
|
ext = work["extension"]
|
2015-06-28 12:36:30 +02:00
|
|
|
off = url.rfind(".")
|
|
|
|
if url[off-2] == "p":
|
|
|
|
off -= 3
|
2015-05-14 19:08:20 +02:00
|
|
|
if work["id"] > 11319935 and "/img-original/" not in url:
|
|
|
|
big = "_big"
|
|
|
|
else:
|
|
|
|
big = ""
|
|
|
|
for i in range(work["page_count"]):
|
|
|
|
work["num"] = "_p{:02}".format(i)
|
2015-06-28 12:36:30 +02:00
|
|
|
url = "{}{}_p{}.{}{}".format(url[:off], big, i, ext, timestamp)
|
2015-11-23 00:15:43 +01:00
|
|
|
yield Message.Url, url, work
|
2015-05-14 19:08:20 +02:00
|
|
|
|
|
|
|
def get_works(self):
|
|
|
|
"""Yield all work-items for a pixiv-member"""
|
2015-10-03 15:43:02 +02:00
|
|
|
pagenum = 1
|
2014-10-12 21:56:44 +02:00
|
|
|
while True:
|
2015-11-23 00:15:43 +01:00
|
|
|
data = self.api_call(self.artist_id, pagenum)
|
2015-05-14 19:08:20 +02:00
|
|
|
for work in data["response"]:
|
2015-11-23 00:15:43 +01:00
|
|
|
yield self.prepare_work(work)
|
2015-05-14 19:08:20 +02:00
|
|
|
pinfo = data["pagination"]
|
|
|
|
if pinfo["current"] == pinfo["pages"]:
|
2014-10-12 21:56:44 +02:00
|
|
|
return
|
2015-10-03 15:43:02 +02:00
|
|
|
pagenum = pinfo["next"]
|
2015-04-10 15:29:09 +02:00
|
|
|
|
2015-11-23 00:15:43 +01:00
|
|
|
def prepare_work(self, work):
|
|
|
|
"""Prepare a work-dictionary with additional keywords"""
|
|
|
|
user = work["user"]
|
|
|
|
url = work["image_urls"]["large"]
|
|
|
|
work["category"] = self.category
|
|
|
|
work["artist-id"] = user["id"]
|
|
|
|
work["artist-name"] = user["name"]
|
|
|
|
work["artist-nick"] = user["account"]
|
|
|
|
work["num"] = ""
|
|
|
|
work["url"] = url
|
|
|
|
work["extension"] = url[url.rfind(".")+1:]
|
|
|
|
return work
|
|
|
|
|
2015-05-20 10:25:11 +02:00
|
|
|
def parse_ugoira(self, data):
|
2015-04-10 15:29:09 +02:00
|
|
|
"""Parse ugoira data"""
|
2014-10-12 21:56:44 +02:00
|
|
|
# get illust page
|
2015-10-03 15:43:02 +02:00
|
|
|
page = self.request(
|
2015-05-20 10:25:11 +02:00
|
|
|
self.illust_url, params={"illust_id": data["id"]},
|
2014-10-12 21:56:44 +02:00
|
|
|
).text
|
|
|
|
|
|
|
|
# parse page
|
2015-10-03 15:43:02 +02:00
|
|
|
frames, _ = text.extract(page, ',"frames":[', ']')
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-05-20 10:25:11 +02:00
|
|
|
# build url
|
|
|
|
url = re.sub(
|
|
|
|
r"/img-original/(.+/\d+)[^/]+",
|
|
|
|
r"/img-zip-ugoira/\g<1>_ugoira1920x1080.zip",
|
|
|
|
data["url"]
|
|
|
|
)
|
2014-10-12 21:56:44 +02:00
|
|
|
|
|
|
|
# build framelist
|
2015-05-14 19:08:20 +02:00
|
|
|
framelist = re.sub(
|
2014-10-12 21:56:44 +02:00
|
|
|
r'\{"file":"([^"]+)","delay":(\d+)\},?',
|
2015-05-14 19:08:20 +02:00
|
|
|
r'\1 \2\n', frames
|
2014-10-12 21:56:44 +02:00
|
|
|
)
|
|
|
|
return url, framelist
|
|
|
|
|
2015-11-22 03:58:31 +01:00
|
|
|
def get_job_metadata(self, user=None):
|
2015-04-10 15:29:09 +02:00
|
|
|
"""Collect metadata for extractor-job"""
|
2015-11-22 03:58:31 +01:00
|
|
|
if not user:
|
|
|
|
user = self.api.user(self.artist_id)["response"][0]
|
2015-04-10 15:29:09 +02:00
|
|
|
return {
|
2015-11-21 04:26:30 +01:00
|
|
|
"category": self.category,
|
2015-11-22 03:58:31 +01:00
|
|
|
"artist-id": user["id"],
|
|
|
|
"artist-name": user["name"],
|
|
|
|
"artist-nick": user["account"],
|
2015-04-10 15:29:09 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2015-11-22 02:21:02 +01:00
|
|
|
class PixivWorkExtractor(PixivUserExtractor):
|
|
|
|
"""Extract a single pixiv work/illustration"""
|
2015-11-30 01:11:13 +01:00
|
|
|
subcategory = "work"
|
2015-11-22 02:21:02 +01:00
|
|
|
pattern = [(r"(?:https?://)?(?:www\.)?pixiv\.net/member(?:_illust)?\.php"
|
|
|
|
r"\?(?:[^&]+&)*illust_id=(\d+)")]
|
2015-12-14 03:00:58 +01:00
|
|
|
test = [("http://www.pixiv.net/member_illust.php?mode=medium&illust_id=966412", {
|
|
|
|
"url": "efb622f065b0871e92195e7bee0b4d75bd687d8d",
|
|
|
|
})]
|
2015-11-22 02:21:02 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PixivUserExtractor.__init__(self, match)
|
|
|
|
self.illust_id = match.group(1)
|
|
|
|
self.work = None
|
|
|
|
|
|
|
|
def get_works(self):
|
2015-11-23 00:15:43 +01:00
|
|
|
return (self.prepare_work(self.work),)
|
2015-11-22 02:21:02 +01:00
|
|
|
|
2015-11-23 00:15:43 +01:00
|
|
|
def get_job_metadata(self, user=None):
|
2015-11-22 02:21:02 +01:00
|
|
|
"""Collect metadata for extractor-job"""
|
|
|
|
self.work = self.api.work(self.illust_id)["response"][0]
|
2015-11-22 03:58:31 +01:00
|
|
|
return PixivUserExtractor.get_job_metadata(self, self.work["user"])
|
2015-11-22 02:21:02 +01:00
|
|
|
|
|
|
|
|
2015-11-23 00:15:43 +01:00
|
|
|
class PixivFavoriteExtractor(PixivUserExtractor):
|
|
|
|
"""Extract all favorites/bookmarks of a single pixiv-user"""
|
2015-11-30 01:11:13 +01:00
|
|
|
subcategory = "favorite"
|
2015-11-23 00:15:43 +01:00
|
|
|
directory_fmt = ["{category}", "bookmarks", "{artist-id}-{artist-nick}"]
|
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?pixiv\.net/bookmark\.php\?id=(\d+)"]
|
2015-12-14 03:00:58 +01:00
|
|
|
test = [("http://www.pixiv.net/bookmark.php?id=173530", {
|
|
|
|
"url": "0110c5c2ee9612a0362e26f7481a8916b6f410fe",
|
|
|
|
})]
|
2015-11-23 00:15:43 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PixivUserExtractor.__init__(self, match)
|
|
|
|
self.api_call = self.api.user_favorite_works
|
|
|
|
|
|
|
|
def prepare_work(self, work):
|
|
|
|
return PixivUserExtractor.prepare_work(self, work["work"])
|
|
|
|
|
|
|
|
|
2015-11-23 02:58:31 +01:00
|
|
|
class PixivBookmarkExtractor(PixivFavoriteExtractor):
|
|
|
|
"""Extract all favorites/bookmarks of your own account"""
|
2015-11-30 01:11:13 +01:00
|
|
|
subcategory = "bookmark"
|
2015-11-23 02:58:31 +01:00
|
|
|
pattern = [r"(?:https?://)?(?:www\.)?pixiv\.net/bookmark\.php()$"]
|
2015-12-14 03:00:58 +01:00
|
|
|
test = []
|
2015-11-23 02:58:31 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
PixivFavoriteExtractor.__init__(self, match)
|
|
|
|
self.api.login()
|
|
|
|
self.artist_id = self.api.user_id
|
|
|
|
|
|
|
|
|
2015-10-06 17:19:54 +02:00
|
|
|
def require_login(func):
|
|
|
|
"""Decorator: auto-login before api-calls"""
|
|
|
|
def wrap(self, *args):
|
2016-03-06 21:00:42 +01:00
|
|
|
self.login()
|
2015-10-06 17:19:54 +02:00
|
|
|
return func(self, *args)
|
|
|
|
return wrap
|
|
|
|
|
2014-10-12 21:56:44 +02:00
|
|
|
class PixivAPI():
|
2015-05-14 19:08:20 +02:00
|
|
|
"""Minimal interface for the Pixiv Public-API for mobile devices
|
|
|
|
|
|
|
|
For a better and more complete implementation, see
|
|
|
|
- https://github.com/upbit/pixivpy
|
|
|
|
For in-depth information regarding the Pixiv Public-API, see
|
|
|
|
- http://blog.imaou.com/opensource/2014/10/09/pixiv_api_for_ios_update.html
|
|
|
|
"""
|
2015-06-28 12:36:30 +02:00
|
|
|
def __init__(self, session):
|
|
|
|
self.session = session
|
2015-05-14 19:08:20 +02:00
|
|
|
self.session.headers.update({
|
|
|
|
"Referer": "http://www.pixiv.net/",
|
2015-09-07 13:48:16 +02:00
|
|
|
"User-Agent": "PixivIOSApp/5.8.0",
|
2015-05-14 19:08:20 +02:00
|
|
|
})
|
2016-03-07 17:01:04 +01:00
|
|
|
self.user_id = -1
|
2016-03-06 21:00:42 +01:00
|
|
|
self.username = config.interpolate(("extractor", "pixiv", "username"))
|
|
|
|
self.password = config.interpolate(("extractor", "pixiv", "password"))
|
2015-05-14 19:08:20 +02:00
|
|
|
|
2015-10-07 01:09:11 +02:00
|
|
|
def login(self):
|
2015-05-14 19:08:20 +02:00
|
|
|
"""Login and gain a Pixiv Public-API access token"""
|
2016-03-06 21:00:42 +01:00
|
|
|
self.user_id, token = self._do_login(self.username, self.password)
|
|
|
|
self.session.headers["Authorization"] = "Bearer " + token
|
2015-05-14 19:08:20 +02:00
|
|
|
|
2015-10-06 17:19:54 +02:00
|
|
|
@require_login
|
2015-05-14 19:08:20 +02:00
|
|
|
def user(self, user_id):
|
|
|
|
"""Query information about a pixiv user"""
|
|
|
|
response = self.session.get(
|
|
|
|
"https://public-api.secure.pixiv.net/v1/users/"
|
|
|
|
"{user}.json".format(user=user_id)
|
|
|
|
)
|
|
|
|
return self._parse(response)
|
|
|
|
|
2015-11-22 02:21:02 +01:00
|
|
|
@require_login
|
|
|
|
def work(self, illust_id):
|
|
|
|
"""Query information about a single pixiv work/illustration"""
|
|
|
|
params = {
|
2015-11-23 00:15:43 +01:00
|
|
|
"image_sizes": "large",
|
2015-11-22 02:21:02 +01:00
|
|
|
}
|
|
|
|
response = self.session.get(
|
|
|
|
"https://public-api.secure.pixiv.net/v1/works/"
|
|
|
|
"{illust}.json".format(illust=illust_id), params=params
|
|
|
|
)
|
|
|
|
return self._parse(response)
|
|
|
|
|
2015-10-06 17:19:54 +02:00
|
|
|
@require_login
|
2015-05-14 19:08:20 +02:00
|
|
|
def user_works(self, user_id, page, per_page=20):
|
|
|
|
"""Query information about the works of a pixiv user"""
|
|
|
|
params = {
|
2015-11-23 00:15:43 +01:00
|
|
|
"page": page,
|
|
|
|
"per_page": per_page,
|
|
|
|
"image_sizes": "large",
|
2015-05-14 19:08:20 +02:00
|
|
|
}
|
|
|
|
response = self.session.get(
|
|
|
|
"https://public-api.secure.pixiv.net/v1/users/"
|
|
|
|
"{user}/works.json".format(user=user_id), params=params
|
|
|
|
)
|
|
|
|
return self._parse(response)
|
2015-04-10 15:29:09 +02:00
|
|
|
|
2015-11-23 00:15:43 +01:00
|
|
|
@require_login
|
|
|
|
def user_favorite_works(self, user_id, page, per_page=20):
|
|
|
|
"""Query information about the favorites works of a pixiv user"""
|
|
|
|
params = {
|
|
|
|
"page": page,
|
|
|
|
"per_page": per_page,
|
|
|
|
"include_stats": False,
|
|
|
|
"image_sizes": "large",
|
|
|
|
}
|
|
|
|
response = self.session.get(
|
|
|
|
"https://public-api.secure.pixiv.net/v1/users/"
|
|
|
|
"{user}/favorite_works.json".format(user=user_id), params=params
|
|
|
|
)
|
|
|
|
return self._parse(response)
|
|
|
|
|
2016-03-06 21:00:42 +01:00
|
|
|
@cache(maxage=50*60, keyarg=1)
|
|
|
|
def _do_login(self, username, password):
|
2016-03-07 17:01:04 +01:00
|
|
|
"""Actual login implementation"""
|
2016-03-06 21:00:42 +01:00
|
|
|
data = {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
"grant_type": "password",
|
|
|
|
"client_id": "bYGKuGVw91e0NMfPGp44euvGt59s",
|
|
|
|
"client_secret": "HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK",
|
|
|
|
}
|
|
|
|
response = self.session.post(
|
|
|
|
"https://oauth.secure.pixiv.net/auth/token", data=data
|
|
|
|
)
|
|
|
|
if response.status_code not in (200, 301, 302):
|
|
|
|
raise Exception("login() failed! check username and password.\n"
|
|
|
|
"HTTP %s: %s" % (response.status_code, response.text))
|
|
|
|
try:
|
|
|
|
response = self._parse(response)["response"]
|
|
|
|
token = response["access_token"]
|
|
|
|
user = response["user"]["id"]
|
|
|
|
except:
|
|
|
|
raise Exception("Get access_token error! Response: %s" % (token))
|
|
|
|
return user, token
|
|
|
|
|
2015-05-14 19:08:20 +02:00
|
|
|
@staticmethod
|
|
|
|
def _parse(response):
|
|
|
|
"""Parse a Pixiv Public-API response"""
|
|
|
|
return json.loads(response.text)
|