start working on Credit Cooperatif backend

This commit is contained in:
Kevin Pouget 2012-11-09 11:27:37 +01:00 committed by Romain Bignon
commit e6a837f6ab
6 changed files with 346 additions and 0 deletions

View file

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Kevin Pouget, based on Romain Bignon work
#
# 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 CreditCooperatifBackend
__all__ = ['CreditCooperatifBackend']

View file

@ -0,0 +1,62 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Kevin Pouget, based on Romain Bignon work
#
# 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.capabilities.bank import ICapBank, AccountNotFound
from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.tools.ordereddict import OrderedDict
from weboob.tools.value import ValueBackendPassword, Value
from .browser import CreditCooperatif
__all__ = ['CreditCooperatifBackend']
class CreditCooperatifBackend(BaseBackend, ICapBank):
NAME = 'creditcooperatif'
MAINTAINER = u'Kevin Pouget'
EMAIL = 'weboob@kevin.pouget.me'
VERSION = '0.d'
DESCRIPTION = u'Credit Cooperatif French bank website'
LICENSE = 'AGPLv3+'
CONFIG = BackendConfig(ValueBackendPassword('login', label='Account ID', masked=False))
BROWSER = CreditCooperatif
WEBSITE = "www.coopanet.com"
def create_default_browser(self):
return self.create_browser(WEBSITE,
self.config['login'].get(),
self.config['password'].get())
def iter_accounts(self):
with self.browser:
return self.browser.get_accounts_list()
def get_account(self, _id):
with self.browser:
account = self.browser.get_account(_id)
if account:
return account
else:
raise AccountNotFound()
def iter_history(self, account):
with self.browser:
return self.browser.get_history(account)

View file

@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2012 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/>.
import urllib
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
from .pages import LoginPage, AccountsPage
__all__ = ['CreditCooperatif']
class CreditCooperatif(BaseBrowser):
PROTOCOL = 'https'
ENCODING = 'iso-8859-15'
PAGES = {'https://[^/]+/banque/sso/ssologin.do".*': LoginPage,
'https://[^/]+/cyber/internet/StartTask.do\?taskInfoOID=mesComptes.*': AccountsPage
}
def __init__(self, website, *args, **kwargs):
self.DOMAIN = website
self.token = None
BaseBrowser.__init__(self, *args, **kwargs)
def is_logged(self):
return not self.is_on_page(LoginPage)
def login(self):
"""
Attempt to log in.
Note: this method does nothing if we are already logged in.
"""
assert isinstance(self.username, basestring)
assert isinstance(self.password, basestring)
if self.is_logged():
return
if not self.is_on_page(LoginPage):
self.home()
self.page.login(self.username, self.password)
if not self.is_logged():
raise BrowserIncorrectPassword()
self.token = self.page.get_token()
def get_accounts_list(self):
self.location(self.buildurl('/cyber/internet/StartTask.do', taskInfoOID='mesComptes', token=self.token))
if self.page.is_error():
self.location(self.buildurl('/cyber/internet/StartTask.do', taskInfoOID='maSyntheseGratuite', token=self.token))
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):
self.location('/cyber/internet/ContinueTask.do', urllib.urlencode(account._params))
while 1:
assert self.is_on_page(TransactionsPage)
for tr in self.page.get_history():
yield tr
next_params = self.page.get_next_params()
if next_params is None:
return
self.location(self.buildurl('/cyber/internet/Page.do', **next_params))

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -0,0 +1,133 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Kevin Pouget, based on Romain Bignon work
#
# 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 urlparse import urlsplit, parse_qsl
from decimal import Decimal
import re
from weboob.tools.browser import BasePage
from weboob.capabilities.bank import Account
from weboob.tools.capabilities.bank.transactions import FrenchTransaction
__all__ = ['LoginPage', 'AccountsPage']
class UnavailablePage(BasePage):
def on_loaded(self):
a = self.document.xpath('//a[@class="btn"]')[0]
self.browser.location(a.attrib['href'])
class LoginPage(BasePage):
def login(self, login, passwd):
self.browser.select_form(name='loginCoForm')
self.browser['codeUtil'] = login
self.browser['motPasse'] = passwd
self.browser.submit(nologin=True)
class AccountsPage(BasePage):
ACCOUNT_TYPES = {u'COMPTE NEF': Account.TYPE_CHECKING
}
CPT_ROW_ID = 0
CPT_ROW_NAME = 1
CPT_ROW_NATURE = 2
CPT_ROW_BALANCE = 3
CPT_ROW_ENCOURS = 4
def is_error(self):
for script in self.document.xpath('//script'):
if script.text is not None and u"Le service est momentanément indisponible" in script.text:
return True
return False
def get_list(self):
for tbCompte in self.document.xpath('//table[@id="compte"]'):
for trCompte in tbCompte.xpath('.//tbody/tr'):
tds = tr.findall('td')
account = Account()
account.id = tds[CPT_ROW_ID].text()
account.label = tds[CPT_ROW_NAME].text()
account_type_str = "".join([td.text() for td in tds[CPT_ROW_ID].xpath('.//td[@id="tx"]')]).strip()
account.type = ACCOUNT_TYPES.get(account_type_str, Account.TYPE_UNKNOWN)
balance_link = tds[CPT_ROW_BALANCE].find("a")
account.balance = Decimal(FrenchTransaction.clean_amount(blance_link.text()))
yield account
return
class Transaction(FrenchTransaction):
PATTERNS = [(re.compile('^RET DAB (?P<text>.*?) RETRAIT DU (?P<dd>\d{2})(?P<mm>\d{2})(?P<yy>\d{2}).*'),
FrenchTransaction.TYPE_WITHDRAWAL),
(re.compile('^RET DAB (?P<text>.*?) CARTE ?:.*'),
FrenchTransaction.TYPE_WITHDRAWAL),
(re.compile('^(?P<text>.*) RETRAIT DU (?P<dd>\d{2})(?P<mm>\d{2})(?P<yy>\d{2}) .*'),
FrenchTransaction.TYPE_WITHDRAWAL),
(re.compile('(\w+) (?P<dd>\d{2})(?P<mm>\d{2})(?P<yy>\d{2}) CB:[^ ]+ (?P<text>.*)'),
FrenchTransaction.TYPE_CARD),
(re.compile('^VIR(EMENT)? (?P<text>.*)'), FrenchTransaction.TYPE_TRANSFER),
(re.compile('^PRLV (?P<text>.*)'), FrenchTransaction.TYPE_ORDER),
(re.compile('^CHEQUE.*'), FrenchTransaction.TYPE_CHECK),
(re.compile('^(AGIOS /|FRAIS) (?P<text>.*)'), FrenchTransaction.TYPE_BANK),
(re.compile('^(CONVENTION \d+ )?COTIS(ATION)? (?P<text>.*)'),
FrenchTransaction.TYPE_BANK),
(re.compile('^REMISE (?P<text>.*)'), FrenchTransaction.TYPE_DEPOSIT),
(re.compile('^(?P<text>.*)( \d+)? QUITTANCE .*'),
FrenchTransaction.TYPE_ORDER),
(re.compile('^.* LE (?P<dd>\d{2})/(?P<mm>\d{2})/(?P<yy>\d{2})$'),
FrenchTransaction.TYPE_UNKNOWN),
]
class TransactionsPage(BasePage):
def get_next_params(self):
if len(self.document.xpath('//li[@id="tbl1_nxt"]')) == 0:
return None
params = {}
for field in self.document.xpath('//input'):
params[field.attrib['name']] = field.attrib.get('value', '')
params['validationStrategy'] = 'NV'
params['pagingDirection'] = 'NEXT'
params['pagerName'] = 'tbl1'
return params
def get_history(self):
for tr in self.document.xpath('//table[@id="tbl1"]/tbody/tr'):
tds = tr.findall('td')
t = Transaction(tr.attrib['id'].split('_', 1)[1])
date = u''.join([txt.strip() for txt in tds[4].itertext()])
raw = u' '.join([txt.strip() for txt in tds[1].itertext()])
debit = u''.join([txt.strip() for txt in tds[-2].itertext()])
credit = u''.join([txt.strip() for txt in tds[-1].itertext()])
t.parse(date, re.sub(r'[ ]+', ' ', raw))
t.set_amount(credit, debit)
yield t

View file

@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Kevin Pouget, based on 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 CreditCooperatifTest(BackendTest):
BACKEND = 'creditcooperatif'
def test_creditcoop(self):
l = list(self.backend.iter_accounts())
if len(l) > 0:
a = l[0]
list(self.backend.iter_history(a))