support repositories to manage backends (closes #747)

This commit is contained in:
Romain Bignon 2012-01-03 12:10:21 +01:00
commit 14a7a1d362
410 changed files with 1079 additions and 297 deletions

23
modules/lcl/__init__.py Normal file
View file

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from .backend import LCLBackend
__all__ = ['LCLBackend']

70
modules/lcl/backend.py Normal file
View file

@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon, Pierre Mazière
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
from weboob.capabilities.bank import ICapBank, AccountNotFound
from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.tools.value import ValueBackendPassword, Value
from .browser import LCLBrowser
__all__ = ['LCLBackend']
class LCLBackend(BaseBackend, ICapBank):
NAME = 'lcl'
MAINTAINER = u'Pierre Mazière'
EMAIL = 'pierre.maziere@gmail.com'
VERSION = '0.a'
DESCRIPTION = 'Le Credit Lyonnais crappy french bank'
LICENSE = 'AGPLv3+'
CONFIG = BackendConfig(ValueBackendPassword('login', label='Account ID', regexp='^\d{1,6}\w$', masked=False),
ValueBackendPassword('password', label='Password of account'),
Value('agency', label='Agency code', regexp='^\d{3,4}$'))
BROWSER = LCLBrowser
def create_default_browser(self):
return self.create_browser(self.config['agency'].get(),
self.config['login'].get(),
self.config['password'].get())
def iter_accounts(self):
for account in self.browser.get_accounts_list():
yield account
def get_account(self, _id):
with self.browser:
account = self.browser.get_account(_id)
if account:
return account
else:
raise AccountNotFound()
def iter_operations(self, account):
""" TODO Not supported yet """
return iter([])
def iter_history(self, account):
with self.browser:
for history in self.browser.get_history(account):
yield history

93
modules/lcl/browser.py Normal file
View file

@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon, Pierre Mazière
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
from .pages import LoginPage, LoginResultPage, FramePage, AccountsPage, AccountHistoryPage
__all__ = ['LCLBrowser']
# Browser
class LCLBrowser(BaseBrowser):
PROTOCOL = 'https'
DOMAIN = 'particuliers.secure.lcl.fr'
ENCODING = 'utf-8'
USER_AGENT = BaseBrowser.USER_AGENTS['wget']
PAGES = {
'https://particuliers.secure.lcl.fr/everest/UWBI/UWBIAccueil\?DEST=PAGEIDENT': LoginPage,
'https://particuliers.secure.lcl.fr/everest/UWBI/UWBIAccueil\?DEST=IDENTIFICATION': LoginResultPage,
'https://particuliers.secure.lcl.fr/outil/UWSP/Synthese/accesSynthese': AccountsPage,
'https://particuliers.secure.lcl.fr/outil/UWB2/Accueil\?DEST=INIT': FramePage,
'https://particuliers.secure.lcl.fr/outil/UWLM/ListeMouvements.*/accesListeMouvements.*': AccountHistoryPage,
}
def __init__(self, agency, *args, **kwargs):
self.agency = agency
BaseBrowser.__init__(self, *args, **kwargs)
def is_logged(self):
return self.page and not self.is_on_page(LoginPage)
def login(self):
assert isinstance(self.username, basestring)
assert isinstance(self.password, basestring)
assert self.password.isdigit()
assert isinstance(self.agency, basestring)
assert self.agency.isdigit()
if not self.is_on_page(LoginPage):
self.location('%s://%s/everest/UWBI/UWBIAccueil?DEST=PAGEIDENT' \
% (self.PROTOCOL, self.DOMAIN),
no_login=True)
if not self.page.login(self.agency, self.username, self.password) or \
not self.is_logged() or \
(self.is_on_page(LoginResultPage) and self.page.is_error()) :
raise BrowserIncorrectPassword()
self.location('%s://%s/outil/UWSP/Synthese/accesSynthese' \
% (self.PROTOCOL, self.DOMAIN))
def get_accounts_list(self):
if not self.is_on_page(AccountsPage):
self.login()
return self.page.get_list()
def get_account(self, id):
assert isinstance(id, basestring)
l = self.get_accounts_list()
for a in l:
if a.id == id:
return a
return None
def get_history(self,account):
if not self.is_on_page(AccountHistoryPage) :
self.location('%s://%s%s' % (self.PROTOCOL, self.DOMAIN, account.link_id))
return self.page.get_operations(account)
#def get_coming_operations(self, account):
# if not self.is_on_page(AccountComing) or self.page.account.id != account.id:
# self.location('/NS_AVEEC?ch4=%s' % account.link_id)
# return self.page.get_operations()

BIN
modules/lcl/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

186
modules/lcl/pages.py Normal file
View file

@ -0,0 +1,186 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon, Pierre Mazière
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
import base64
from datetime import date
from weboob.capabilities.bank import Operation
from weboob.capabilities.bank import Account
from weboob.tools.browser import BasePage, BrowserUnavailable
from weboob.tools.captcha.virtkeyboard import MappedVirtKeyboard, VirtKeyboardError
from logging import error
import tempfile
class LCLVirtKeyboard(MappedVirtKeyboard):
symbols={'0':'9da2724133f2221482013151735f033c',
'1':'873ab0087447610841ae1332221be37b',
'2':'93ce6c330393ff5980949d7b6c800f77',
'3':'b2d70c69693784e1bf1f0973d81223c0',
'4':'498c8f5d885611938f94f1c746c32978',
'5':'359bcd60a9b8565917a7bf34522052c3',
'6':'aba912172f21f78cd6da437cfc4cdbd0',
'7':'f710190d6b947869879ec02d8e851dfa',
'8':'b42cc25e1539a15f767aa7a641f3bfec',
'9':'cc60e5894a9d8e12ee0c2c104c1d5490'
}
url="/UWBI/UWBIAccueil?DEST=GENERATION_CLAVIER"
color=(255,255,255,255)
def __init__(self,basepage):
img=basepage.document.find("//img[@id='idImageClavier']")
MappedVirtKeyboard.__init__(self,basepage.browser.openurl(self.url),
basepage.document,img,self.color)
if basepage.browser.responses_dirname is None:
basepage.browser.responses_dirname = \
tempfile.mkdtemp(prefix='weboob_session_')
self.check_symbols(self.symbols,basepage.browser.responses_dirname)
def get_symbol_code(self,md5sum):
code=MappedVirtKeyboard.get_symbol_code(self,md5sum)
return code[-5:-3]
def get_string_code(self,string):
code=''
for c in string:
code+=self.get_symbol_code(self.symbols[c])
return code
class LoginPage(BasePage):
def myXOR(self,value,seed):
s=''
for i in xrange(len(value)):
s+=chr(seed^ord(value[i]))
return s
def login(self, agency, login, passwd):
try:
vk=LCLVirtKeyboard(self)
except VirtKeyboardError,err:
error("Error: %s"%err)
return False
password=vk.get_string_code(passwd)
seed=-1
str="var aleatoire = "
for script in self.document.findall("/head/script"):
if(script.text is None or len(script.text)==0):
continue
offset=script.text.find(str)
if offset!=-1:
seed=int(script.text[offset+len(str):offset+len(str)+1])
break
if seed==-1:
error("Variable 'aleatoire' not found")
return False
self.browser.select_form(nr=0)
self.browser.form.set_all_readonly(False)
self.browser['agenceId'] = base64.b64encode(self.myXOR(agency,seed))
self.browser['compteId'] = base64.b64encode(self.myXOR(login,seed))
self.browser['postClavier'] = base64.b64encode(self.myXOR(password,seed))
try:
self.browser.submit()
except BrowserUnavailable:
# Login is not valid
return False
return True
class LoginResultPage(BasePage):
def is_error(self):
for text in self.document.find('body').itertext():
text=text.strip()
# Login seems valid, but password does not
needle='Les données saisies sont incorrectes'
if text.startswith(needle.decode('utf-8')):
return True
return False
class FramePage(BasePage):
pass
class AccountsPage(BasePage):
def get_list(self):
l = []
for div in self.document.getiterator('div'):
if div.attrib.get('class')=="unCompte-CA" or\
div.attrib.get('class')=="unCompte-CC" or\
div.attrib.get('class')=="unCompte-CD" or\
div.attrib.get('class')=="unCompte-CE":
#CA=> ? maybe Assurance-vie
#CC=> Compte Courant
#CD=> Compte Dépôt
#CE=> Compte d'Epargne
account = Account()
account.type=div.attrib.get('class')[-2:]
account.id = div.attrib.get('id').replace('-','')
for td in div.getiterator('td'):
if td.find("div") is not None and td.find("div").attrib.get('class') == 'libelleCompte':
account.label = td.find("div").text
elif td.find('a') is not None and td.find('a').attrib.get('class') is None:
balance = td.find('a').text.replace(u"\u00A0",'').replace('.','').replace('+','').replace(',','.')
account.balance = float(balance)
account.link_id = td.find('a').attrib.get('href')
l.append(account)
return l
class AccountHistoryPage(BasePage):
def get_specific_operations(self,tableHeaderPrefixes,debitColumns,creditColumns):
operations = []
for td in self.document.iter('td'):
text=td.findtext("b")
if text is None:
continue
for i in range(len(tableHeaderPrefixes)):
if text.startswith(tableHeaderPrefixes[i].decode('utf-8')):
tbody=td.getparent().getparent()
for tr in tbody.iter('tr'):
tr_class=tr.attrib.get('class')
if tr_class == 'tbl1' or tr_class=='tbl2':
tds=tr.findall('td')
d=date(*reversed([int(x) for x in tds[0].text.split('/')]))
label=u''+tds[1].find('a').text.strip()
if tds[debitColumns[i]].text.strip() != u"":
amount = - float(tds[debitColumns[i]].text.strip().replace('.','').replace(',','.').replace(u"\u00A0",'').replace(' ',''))
else:
amount= float(tds[creditColumns[i]].text.strip().replace('.','').replace(',','.').replace(u"\u00A0",'').replace(' ',''))
operation=Operation(len(operations))
operation.date=d
operation.label=label
operation.amount=amount
operations.append(operation)
return operations
def get_operations(self,account):
if account.type=="CA":
return [] # Not supported: page example required
elif account.type=="CC":
return self.get_specific_operations(['Opérations effectuées'],[3],[4])
elif account.type=="CD":
return self.get_specific_operations(['Solde au'],[2],[3])
elif account.type=="CE":
return self.get_specific_operations(['Solde au'],[2],[3])

27
modules/lcl/test.py Normal file
View file

@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.test import BackendTest
class LCLtTest(BackendTest):
BACKEND = 'lcl'
def test_lcl(self):
list(self.backend.iter_accounts())