Skip to content

Commit

Permalink
Fix issue #116: update login method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nandaka committed Jun 13, 2016
1 parent 666d252 commit 5dddde9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
71 changes: 37 additions & 34 deletions PixivBrowserFactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import httplib
import time
import sys
import json

import PixivHelper
from PixivException import PixivException
Expand Down Expand Up @@ -170,59 +171,55 @@ def loginUsingCookie(self, loginCookie=None):
return False


def loginHttps(self, username, password):
def login(self, username, password):
try:
PixivHelper.printAndLog('info', 'Log in using secure form.')
self.open(PixivConstant.PIXIV_URL_SSL)
PixivHelper.printAndLog('info', 'Logging in...')
url = "https://accounts.pixiv.net/login"
page = self.open(url)

#self.select_form(predicate=lambda f: f.attrs.get('action', None) == '/login.php')
#self['pixiv_id'] = username
#self['pass'] = password
#if self._config.keepSignedIn:
# self.find_control('skip').items[0].selected = True

#response = self.submit()
# get the post key
parsed = BeautifulSoup(page)
init_config = parsed.find('input', attrs={'id':'init-config'})
js_init_config = json.loads(init_config['value'])

data = {}
data['mode'] = 'login'
data['return_to'] = '/'
data['pixiv_id'] = username
data['pass'] = password
if self._config.keepSignedIn:
data['skip'] = '1'
else:
data['skip'] = '0'
response = self.open("https://www.pixiv.net/login.php", urllib.urlencode(data))

return self.processLoginResult(response, )
data['password'] = password
#data['captcha'] = ''
#data['g_recaptcha_response'] = ''
data['return_to'] = 'http://www.pixiv.net'
data['lang'] = 'en'
data['post_key'] = js_init_config["pixivAccount.postKey"]
data['source'] = "pc"

request = urllib2.Request("https://accounts.pixiv.net/api/login?lang=en", urllib.urlencode(data))
response = self.open(request)

return self.processLoginResult(response)
except:
PixivHelper.printAndLog('error', 'Error at loginHttps(): ' + str(sys.exc_info()))
PixivHelper.printAndLog('error', 'Error at login(): ' + str(sys.exc_info()))
raise

def processLoginResult(self, response):
PixivHelper.GetLogger().info('Logging in, return url: ' + response.geturl())

## failed login will return to either of these page:
## http://www.pixiv.net/login.php
## https://www.secure.pixiv.net/login.php
if response.geturl().find('pixiv.net/login.php') == -1:
PixivHelper.printAndLog('info','Logged in')
## write back the new cookie value
# check the returned json
js = response.read()
PixivHelper.GetLogger().info(str(js))
result = json.loads(js)
if result["body"] is not None and result["body"].has_key("successed"):
for cookie in self._ua_handlers['_cookies'].cookiejar:
if cookie.name == 'PHPSESSID':
PixivHelper.printAndLog('info', 'new cookie value: ' + str(cookie.value))
self._config.cookie = cookie.value
self._config.writeConfig(path=self._config.configFileLocation)
break
return True
else:
errors = self.parseLoginError(response)
if len(errors) > 0:
for error in errors:
if error.string is not None:
PixivHelper.printAndLog('error', 'Server Reply: ' + error.string)
else :
if result["body"] is not None and result["body"].has_key("validation_errors"):
PixivHelper.printAndLog('info', "Server reply: " + str(result["body"]["validation_errors"]))
else:
PixivHelper.printAndLog('info', 'Wrong username or password.')
PixivHelper.printAndLog('info', 'Unknown login issue, please use cookie login method.')
return False

def parseLoginError(self, res):
Expand Down Expand Up @@ -253,3 +250,9 @@ def getExistingBrowser():
raise PixivException("Browser is not initialized yet!", errorCode = PixivException.NOT_LOGGED_IN)
return _browser

def test():
from PixivConfig import PixivConfig
cfg = PixivConfig()
cfg.loadConfig("./config.ini")
b = getBrowser(cfg, None)
return b.login("nandaka", "***REMOVED***")
2 changes: 1 addition & 1 deletion PixivConstant.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# pylint: disable=I0011, C, C0302


PIXIVUTIL_VERSION = '20160513'
PIXIVUTIL_VERSION = '20160613'
PIXIVUTIL_LINK = 'https://github.com/Nandaka/PixivUtil2/releases'
PIXIVUTIL_DONATE = 'https://bit.ly/PixivUtilDonation'
PIXIV_URL = 'http://www.pixiv.net'
Expand Down
2 changes: 1 addition & 1 deletion PixivUtil2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ def doLogin(password, username):
result = PixivBrowserFactory.getBrowser(config=__config__).loginUsingCookie()

if not result:
result = PixivBrowserFactory.getBrowser(config=__config__).loginHttps(username, password)
result = PixivBrowserFactory.getBrowser(config=__config__).login(username, password)
except:
PixivHelper.printAndLog('error', 'Error at doLogin(): {0}'.format(str(sys.exc_info())))
raise PixivException("Cannot Login!", PixivException.CANNOT_LOGIN)
Expand Down
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
20160613
- Fix Issue #116: update login method.

20160513
- Add null check before dumping html.
- Fix Issue #115: update login method.
Expand Down

0 comments on commit 5dddde9

Please sign in to comment.