Commit 3fb4f8de authored by Romain Bignon's avatar Romain Bignon

factorized login algorithm into LoginPage class

parent 524a0bf6
......@@ -19,61 +19,58 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import mechanize
import ClientForm
import html5lib
import re
import pages
from .captcha import Captcha, TileError
from bnporc.error import LoginError
class BNPorc(mechanize.Browser):
PAGES = {'DOSSIER_Releves_D_Operation': pages.ComptesList,
'DSP_HISTOCPT': pages.CompteHistory,
'DSP_ET': pages.CompteCurrent
PAGES = {'identifiant=DOSSIER_Releves_D_Operation': pages.ComptesList,
'identifiant=DSP_HISTOCPT': pages.CompteHistory,
'identifiant=DSP_ET': pages.CompteCurrent,
'Action=DSP_VGLOBALE': pages.LoginPage,
'type=homeconnex': pages.LoginPage,
}
def __init__(self, login, password):
def __init__(self):
mechanize.Browser.__init__(self)
self.page = None
self.parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder("dom"))
self.location('https://www.secure.bnpparibas.net/banque/portail/particulier/HomeConnexion?type=homeconnex')
def isOnPage(self, pageCls):
return isinstance(self.page, pageCls)
def login(self, login, password):
assert isinstance(login, (str,unicode))
assert isinstance(password, (str,unicode))
assert password.isdigit()
self.parser = html5lib.HTMLParser(tree=html5lib.treebuilders.getTreeBuilder("dom"))
if not self.isOnPage(pages.LoginPage):
self.location('https://www.secure.bnpparibas.net/banque/portail/particulier/HomeConnexion?type=homeconnex')
self.open('https://www.secure.bnpparibas.net/banque/portail/particulier/HomeConnexion?type=homeconnex')
img = Captcha(self.open('/NSImgGrille'))
self.back()
self.page.login(login, password)
try:
img.build_tiles()
except TileError, err:
print >>sys.stderr, "Error: %s" % err
if err.tile:
err.tile.display()
self.location('/NSFR?Action=DSP_VGLOBALE')
self.select_form('logincanalnet')
# HACK because of fucking malformed HTML, the field isn't recognized by mechanize.
self.controls.append(ClientForm.TextControl('text', 'ch1', {'value': ''}))
self.set_all_readonly(False)
self['ch1'] = login
self['ch5'] = img.get_codes(password)
r = self.submit()
self.__set_location(self.open('/NSFR?Action=DSP_VGLOBALE'))
if self.isOnPage(pages.LoginPage):
raise LoginError('Login or password incorrect.')
def get_comptes_list(self):
self.__set_location(self.open('/NSFR?Action=DSP_VGLOBALE'))
self.location('/NSFR?Action=DSP_VGLOBALE')
return self.page.get_list()
def location(self, *args, **kwargs):
return self.__set_location(self.open(*args, **kwargs))
def __set_location(self, result):
pageCls = None
for key, value in self.PAGES.iteritems():
regexp = re.compile(".*identifiant=(.*)")
m = regexp.match(result.geturl())
if m and m.group(1).startswith(key):
regexp = re.compile('.*%s.*' % key)
if regexp.match(result.geturl()):
pageCls = value
break
......
# -*- coding: utf-8 -*-
"""
Copyright(C) 2009 Romain Bignon
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
class LoginError(Exception): pass
......@@ -19,6 +19,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
from .comptes_list import ComptesList
from .login import LoginPage
class CompteHistory(ComptesList): pass
class CompteCurrent(ComptesList): pass
# -*- coding: utf-8 -*-
"""
Copyright(C) 2009 Romain Bignon
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import ClientForm
import sys
from bnporc.pages.base import PageBase
from bnporc.captcha import Captcha, TileError
class LoginPage(PageBase):
def loaded(self):
pass
def login(self, login, password):
img = Captcha(self.bnporc.open('/NSImgGrille'))
self.bnporc.back()
try:
img.build_tiles()
except TileError, err:
print >>sys.stderr, "Error: %s" % err
if err.tile:
err.tile.display()
self.bnporc.select_form('logincanalnet')
# HACK because of fucking malformed HTML, the field isn't recognized by mechanize.
self.bnporc.controls.append(ClientForm.TextControl('text', 'ch1', {'value': ''}))
self.bnporc.set_all_readonly(False)
self.bnporc['ch1'] = login
self.bnporc['ch5'] = img.get_codes(password)
r = self.bnporc.submit()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment