add authentication-exception

pull/13/head
Mike Fährmann 8 years ago
parent 6f7f29d684
commit d6c06f9efd
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -87,6 +87,12 @@ def main():
else: else:
jobtype = job.DownloadJob jobtype = job.DownloadJob
for url in args.urls: for url in args.urls:
jobtype(url).run() try:
jobtype(url).run()
except exception.NoExtractorError:
print("No suitable extractor found for URL '", url, "'", sep="")
except exception.AuthenticationError:
print("Authentication failed. Please provide a valid "
"username/password pair.")
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nKeyboardInterrupt") print("\nKeyboardInterrupt")

@ -8,3 +8,6 @@
class NoExtractorError(Exception): class NoExtractorError(Exception):
pass pass
class AuthenticationError(Exception):
pass

@ -9,7 +9,7 @@
"""Extract images and ugoira from http://www.pixiv.net/""" """Extract images and ugoira from http://www.pixiv.net/"""
from .common import Extractor, Message from .common import Extractor, Message
from .. import config, text from .. import config, text, exception
from ..cache import cache from ..cache import cache
import re import re
import json import json
@ -213,8 +213,8 @@ class PixivAPI():
def login(self): def login(self):
"""Login and gain a Pixiv Public-API access token""" """Login and gain a Pixiv Public-API access token"""
self.user_id, token = self._do_login(self.username, self.password) self.user_id, auth_header = self._do_login(self.username, self.password)
self.session.headers["Authorization"] = "Bearer " + token self.session.headers["Authorization"] = auth_header
@require_login @require_login
def user(self, user_id): def user(self, user_id):
@ -280,15 +280,14 @@ class PixivAPI():
"https://oauth.secure.pixiv.net/auth/token", data=data "https://oauth.secure.pixiv.net/auth/token", data=data
) )
if response.status_code not in (200, 301, 302): if response.status_code not in (200, 301, 302):
raise Exception("login() failed! check username and password.\n" raise exception.AuthenticationError()
"HTTP %s: %s" % (response.status_code, response.text))
try: try:
response = self._parse(response)["response"] response = self._parse(response)["response"]
token = response["access_token"] token = response["access_token"]
user = response["user"]["id"] user = response["user"]["id"]
except: except:
raise Exception("Get access_token error! Response: %s" % (token)) raise Exception("Get access_token error! Response: %s" % (token))
return user, token return user, "Bearer " + token
@staticmethod @staticmethod
def _parse(response): def _parse(response):

Loading…
Cancel
Save