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/pixiv.py

328 lines
11 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2017-03-17 09:42:59 +01:00
# Copyright 2014-2017 Mike Fährmann
#
# 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.
2017-04-20 13:20:41 +02:00
"""Extract images and ugoira from https://www.pixiv.net/"""
2015-10-05 17:15:31 +02:00
from .common import Extractor, Message
from .. import text, exception
2016-03-06 21:00:42 +01:00
from ..cache import cache
2014-10-12 21:56:44 +02:00
import re
2017-02-01 00:53:19 +01:00
class PixivExtractor(Extractor):
"""Base class for pixiv extractors"""
2015-11-21 04:26:30 +01:00
category = "pixiv"
directory_fmt = ["{category}", "{user[id]} {user[account]}"]
filename_fmt = "{category}_{user[id]}_{id}{num}.{extension}"
2017-04-20 13:20:41 +02:00
illust_url = "https://www.pixiv.net/member_illust.php?mode=medium"
2014-10-12 21:56:44 +02:00
def __init__(self):
2015-10-05 17:15:31 +02:00
Extractor.__init__(self)
2017-03-17 09:42:59 +01:00
self.api = PixivAPI(self)
self.user_id = -1
self.load_ugoira = self.config("ugoira", True)
2014-10-12 21:56:44 +02:00
def items(self):
metadata = self.get_metadata()
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.works():
work = self.prepare_work(work)
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":
2016-07-20 14:19:46 +02:00
if not self.load_ugoira:
continue
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
2015-05-14 19:08:20 +02:00
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"]
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)
2017-02-01 00:53:19 +01: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 works(self):
"""Return all work-items for a pixiv-member"""
return []
2015-11-23 00:15:43 +01:00
def prepare_work(self, work):
"""Prepare a work-dictionary with additional keywords"""
url = work["image_urls"]["large"]
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):
"""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
frames = text.extract(page, ',"frames":[', ']')[0]
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
)
2014-10-12 21:56:44 +02:00
return url, framelist
def get_metadata(self, user=None):
"""Collect metadata for extractor-job"""
if not user:
user = self.api.user(self.user_id)[0]
return {"user": user}
class PixivUserExtractor(PixivExtractor):
"""Extractor for works of a pixiv-user"""
subcategory = "user"
pattern = [(r"(?:https?://)?(?:www\.)?pixiv\.net/"
r"member(?:_illust)?\.php\?id=(\d+)(?:.*&tag=([^&#]+))?"),
(r"(?:https?://)?(?:www\.)?pixiv\.net/u/(\d+)()")]
test = [
("http://www.pixiv.net/member_illust.php?id=173530", {
"url": "852c31ad83b6840bacbce824d85f2a997889efb7",
}),
("https://www.pixiv.net/member_illust.php?id=173530&tag=HITMAN", {
"url": "3ecb4970dd91ce1de0a9449671b42db5e3fe2b08",
}),
("http://www.pixiv.net/member_illust.php?id=173531", {
"exception": exception.NotFoundError,
}),
("https://www.pixiv.net/u/173530", None),
]
def __init__(self, match):
PixivExtractor.__init__(self)
self.user_id, tag = match.groups()
self.tag = tag.lower() if tag else None
def works(self):
for work in self.api.user_works(self.user_id):
if (not self.tag or
self.tag in [tag.lower() for tag in work["tags"]]):
yield work
2014-10-12 21:56:44 +02:00
class PixivWorkExtractor(PixivExtractor):
"""Extractor for a single pixiv work/illustration"""
2015-11-30 01:11:13 +01:00
subcategory = "work"
pattern = [(r"(?:https?://)?(?:www\.)?pixiv\.net/member(?:_illust)?\.php"
2016-08-25 20:10:02 +02:00
r"\?(?:[^&]+&)*illust_id=(\d+)"),
(r"(?:https?://)?i(?:\d+\.pixiv|\.pximg)\.net/"
r"(?:(?:.*/)?img-[^/]+/img/\d{4}(?:/\d\d){5}"
r"|img\d+/img/[^/]+)/(\d+)"),
(r"(?:https?://)?img\d*\.pixiv\.net/img/[^/]+/(\d+)"),
(r"(?:https?://)?(?:www\.)?pixiv\.net/i/(\d+)"),]
2016-08-25 20:10:02 +02:00
test = [
2017-02-01 00:53:19 +01:00
(("http://www.pixiv.net/member_illust.php"
"?mode=medium&illust_id=966412"), {
2017-04-14 14:40:36 +02:00
"url": "90c1715b07b0d1aad300bce256a0bc71f42540ba",
2016-08-25 20:10:02 +02:00
"content": "69a8edfb717400d1c2e146ab2b30d2c235440c5a",
}),
2017-02-01 00:53:19 +01:00
(("http://www.pixiv.net/member_illust.php"
"?mode=medium&illust_id=966411"), {
2016-12-30 01:46:42 +01:00
"exception": exception.NotFoundError,
}),
2017-02-01 00:53:19 +01:00
(("http://i1.pixiv.net/c/600x600/img-master/"
"img/2008/06/13/00/29/13/966412_p0_master1200.jpg"), {
2017-04-14 14:40:36 +02:00
"url": "90c1715b07b0d1aad300bce256a0bc71f42540ba",
2016-08-25 20:10:02 +02:00
}),
(("https://i.pximg.net/img-original/"
"img/2017/04/25/07/33/29/62568267_p0.png"), {
"url": "71b8bbd070d6b03a75ca4afb89f64d1445b2278d",
}),
("https://www.pixiv.net/i/966412", None),
("http://img.pixiv.net/img/soundcross/42626136.jpg", None),
("http://i2.pixiv.net/img76/img/snailrin/42672235.jpg", None),
2016-08-25 20:10:02 +02:00
]
def __init__(self, match):
PixivExtractor.__init__(self)
self.illust_id = match.group(1)
2016-07-20 14:19:46 +02:00
self.load_ugoira = True
self.work = None
def works(self):
return (self.work,)
def get_metadata(self, user=None):
self.work = self.api.work(self.illust_id)[0]
return PixivExtractor.get_metadata(self, self.work["user"])
class PixivFavoriteExtractor(PixivExtractor):
"""Extractor for all favorites/bookmarks of a pixiv-user"""
2015-11-30 01:11:13 +01:00
subcategory = "favorite"
directory_fmt = ["{category}", "bookmarks", "{user[id]} {user[account]}"]
2015-11-23 00:15:43 +01:00
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", {
2017-04-14 14:40:36 +02:00
"url": "e717eb511500f2fa3497aaee796a468ecf685cc4",
2015-12-14 03:00:58 +01:00
})]
2015-11-23 00:15:43 +01:00
def __init__(self, match):
PixivExtractor.__init__(self)
self.user_id = match.group(1)
def works(self):
return self.api.user_favorite_works(self.user_id)
2015-11-23 00:15:43 +01:00
def prepare_work(self, work):
return PixivExtractor.prepare_work(self, work["work"])
2015-11-23 00:15:43 +01:00
2015-11-23 02:58:31 +01:00
class PixivBookmarkExtractor(PixivFavoriteExtractor):
"""Extractor for 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 get_metadata(self, user=None):
2015-11-23 02:58:31 +01:00
self.api.login()
user = self.api.user_info
self.user_id = user["id"]
return PixivExtractor.get_metadata(self, user)
2017-02-01 00:53:19 +01:00
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
"""
2017-03-17 09:42:59 +01:00
def __init__(self, extractor):
self.session = extractor.session
self.log = extractor.log
self.username, self.password = extractor.auth_info()
self.user_info = None
2015-05-14 19:08:20 +02:00
self.session.headers.update({
2017-05-05 10:38:22 +02:00
"Referer": "https://www.pixiv.net/",
'App-OS': 'ios',
'App-OS-Version': '10.3.1',
'App-Version': '6.7.1',
'User-Agent': 'PixivIOSApp/6.7.1 (iOS 10.3.1; iPhone8,1)',
2015-05-14 19:08:20 +02:00
})
def user(self, user_id):
"""Query information about a pixiv user"""
endpoint = "users/" + user_id
return self._call(endpoint, {})["response"]
2015-05-14 19:08:20 +02:00
def work(self, illust_id):
"""Query information about a single pixiv work/illustration"""
endpoint = "works/" + illust_id
params = {"image_sizes": "large"}
return self._call(endpoint, params)["response"]
def user_works(self, user_id):
2015-05-14 19:08:20 +02:00
"""Query information about the works of a pixiv user"""
endpoint = "users/{user}/works".format(user=user_id)
params = {"image_sizes": "large"}
return self._pagination(endpoint, params)
def user_favorite_works(self, user_id):
"""Query information about the favorite works of a pixiv user"""
endpoint = "users/{user}/favorite_works".format(user=user_id)
params = {"image_sizes": "large", "include_stats": False}
return self._pagination(endpoint, params)
2015-11-23 00:15:43 +01:00
def login(self):
"""Login and gain a Pixiv Public-API access token"""
self.user_info, access_token = self._login_impl(
self.username, self.password)
self.session.headers["Authorization"] = access_token
2016-03-06 21:00:42 +01:00
@cache(maxage=50*60, keyarg=1)
def _login_impl(self, username, password):
2016-03-07 17:01:04 +01:00
"""Actual login implementation"""
2017-03-17 09:42:59 +01:00
self.log.info("Logging in as %s", username)
2016-03-06 21:00:42 +01:00
data = {
"username": username,
"password": password,
"grant_type": "password",
"client_id": "bYGKuGVw91e0NMfPGp44euvGt59s",
"client_secret": "HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK",
"get_secure_url": 1,
2016-03-06 21:00:42 +01:00
}
response = self.session.post(
"https://oauth.secure.pixiv.net/auth/token", data=data
)
if response.status_code != 200:
2016-07-14 14:57:42 +02:00
raise exception.AuthenticationError()
2016-03-06 21:00:42 +01:00
try:
response = response.json()["response"]
2016-03-06 21:00:42 +01:00
token = response["access_token"]
user = response["user"]
except KeyError:
raise Exception("Get token error! Response: %s" % (response))
2016-07-14 14:57:42 +02:00
return user, "Bearer " + token
2016-03-06 21:00:42 +01:00
def _call(self, endpoint, params, _empty=[None]):
url = "https://public-api.secure.pixiv.net/v1/" + endpoint + ".json"
self.login()
data = self.session.get(url, params=params).json()
2017-02-01 00:53:19 +01:00
status = data.get("status")
response = data.get("response", _empty)
if status == "failure" or response == _empty:
raise exception.NotFoundError()
return data
def _pagination(self, endpoint, params):
while True:
data = self._call(endpoint, params)
yield from data["response"]
pinfo = data["pagination"]
if pinfo["current"] == pinfo["pages"]:
return
params["page"] = pinfo["next"]