mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 10:42:34 +01:00
[pixiv] reuse api-tokens
This commit is contained in:
parent
bea33ae9cb
commit
f5d5684119
@ -132,7 +132,7 @@ def require_login(func):
|
|||||||
"""Decorator: auto-login before api-calls"""
|
"""Decorator: auto-login before api-calls"""
|
||||||
def wrap(self, *args):
|
def wrap(self, *args):
|
||||||
now = time.time()
|
now = time.time()
|
||||||
if now - self.last_login > 3000:
|
if now - self.last_login > PixivAPI.token_timeout:
|
||||||
self.login()
|
self.login()
|
||||||
self.last_login = now
|
self.last_login = now
|
||||||
return func(self, *args)
|
return func(self, *args)
|
||||||
@ -147,6 +147,8 @@ class PixivAPI():
|
|||||||
- http://blog.imaou.com/opensource/2014/10/09/pixiv_api_for_ios_update.html
|
- http://blog.imaou.com/opensource/2014/10/09/pixiv_api_for_ios_update.html
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
token_timeout = 50*60 # 50 minutes
|
||||||
|
|
||||||
def __init__(self, session):
|
def __init__(self, session):
|
||||||
self.last_login = 0
|
self.last_login = 0
|
||||||
self.session = session
|
self.session = session
|
||||||
@ -154,34 +156,40 @@ class PixivAPI():
|
|||||||
"Referer": "http://www.pixiv.net/",
|
"Referer": "http://www.pixiv.net/",
|
||||||
"User-Agent": "PixivIOSApp/5.8.0",
|
"User-Agent": "PixivIOSApp/5.8.0",
|
||||||
})
|
})
|
||||||
|
config.setdefault(("extractor", "pixiv"), {})
|
||||||
|
|
||||||
def login(self, auth=None):
|
def login(self):
|
||||||
"""Login and gain a Pixiv Public-API access token"""
|
"""Login and gain a Pixiv Public-API access token"""
|
||||||
if auth:
|
pconf = config.get(("extractor", "pixiv"))
|
||||||
username, password = auth
|
token = pconf.get("access-token")
|
||||||
else:
|
now = time.time()
|
||||||
username = config.get(("extractor", "pixiv", "username"))
|
if token:
|
||||||
password = config.get(("extractor", "pixiv", "password"))
|
timestamp = pconf.get("access-token-timestamp", 0)
|
||||||
data = {
|
if now - timestamp > PixivAPI.token_timeout:
|
||||||
"username": username,
|
token = None
|
||||||
"password": password,
|
if not token:
|
||||||
"grant_type": "password",
|
data = {
|
||||||
"client_id": "bYGKuGVw91e0NMfPGp44euvGt59s",
|
"username": pconf.get("username"),
|
||||||
"client_secret": "HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK",
|
"password": pconf.get("password"),
|
||||||
}
|
"grant_type": "password",
|
||||||
response = self.session.post(
|
"client_id": "bYGKuGVw91e0NMfPGp44euvGt59s",
|
||||||
"https://oauth.secure.pixiv.net/auth/token", data=data
|
"client_secret": "HP3RmkgAmEGro0gn1x9ioawQE8WMfvLXDz3ZqxpK",
|
||||||
)
|
}
|
||||||
if response.status_code not in (200, 301, 302):
|
response = self.session.post(
|
||||||
raise Exception("login() failed! check username and password.\n"
|
"https://oauth.secure.pixiv.net/auth/token", data=data
|
||||||
"HTTP %s: %s" % (response.status_code, response.text))
|
|
||||||
try:
|
|
||||||
token = self._parse(response)
|
|
||||||
self.session.headers["Authorization"] = (
|
|
||||||
"Bearer " + token["response"]["access_token"]
|
|
||||||
)
|
)
|
||||||
except:
|
if response.status_code not in (200, 301, 302):
|
||||||
raise Exception("Get access_token error! Response: %s" % (token))
|
raise Exception("login() failed! check username and password.\n"
|
||||||
|
"HTTP %s: %s" % (response.status_code, response.text))
|
||||||
|
try:
|
||||||
|
token = self._parse(response)["response"]["access_token"]
|
||||||
|
except:
|
||||||
|
raise Exception("Get access_token error! Response: %s" % (token))
|
||||||
|
pconf["access-token"] = token
|
||||||
|
pconf["access-token-timestamp"] = now - 1
|
||||||
|
self.session.headers["Authorization"] = (
|
||||||
|
"Bearer " + token
|
||||||
|
)
|
||||||
|
|
||||||
@require_login
|
@require_login
|
||||||
def user(self, user_id):
|
def user(self, user_id):
|
||||||
|
Loading…
Reference in New Issue
Block a user