Creditcoop/perso : upgrade to browser2

This commit is contained in:
Florent 2014-10-10 11:29:41 +02:00
commit 40a8bc0ce1
3 changed files with 68 additions and 78 deletions

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2012 Kevin Pouget
# Copyright(C) 2012-2014 Kevin Pouget, Florent Fourcot
#
# This file is part of weboob.
#
@ -18,26 +18,27 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from decimal import Decimal
import re
from weboob.tools.json import json
from weboob.deprecated.browser import Page
from weboob.capabilities.bank import Account
from weboob.tools.capabilities.bank.transactions import FrenchTransaction
from weboob.browser.pages import HTMLPage, JsonPage, LoggedPage
from weboob.browser.filters.standard import Filter, Format, CleanText, CleanDecimal
from weboob.browser.elements import ListElement, ItemElement, method
class LoginPage(Page):
class LoginPage(HTMLPage):
def login(self, login, password):
self.browser.select_form(predicate=lambda form: form.attrs.get('id', '') == 'AuthForm')
self.browser['j_username'] = login.encode('iso-8859-15')
self.browser['j_password'] = password.encode('iso-8859-15')
self.browser.submit(nologin=True)
form = self.get_form(xpath='//form[@id="AuthForm"]')
form['j_username'] = login.encode('iso-8859-15')
form['j_password'] = password.encode('iso-8859-15')
form.submit()
class LoggedPage(Page):
class CreditLoggedPage(HTMLPage):
def get_error(self):
div = self.document.xpath('//div[@class="errorForm-msg"]')
div = self.doc.xpath('//div[@class="errorForm-msg"]')
if len(div) == 0:
return None
@ -45,24 +46,31 @@ class LoggedPage(Page):
return re.sub('[\r\n\t\xa0]+', ' ', msg)
class AccountsPage(Page):
ACCOUNT_TYPES = {u'COMPTE NEF': Account.TYPE_CHECKING}
class AddType(Filter):
types = {u'COMPTE NEF': Account.TYPE_CHECKING,
u'CPTE A VUE': Account.TYPE_CHECKING,
u'LIVRET AGIR': Account.TYPE_SAVINGS}
def get_list(self):
for table in self.document.getroot().cssselect('table.table-synthese'):
account = Account()
labels = table.xpath('.//ul[@class="nClient"]/li')
account_type_str = table.xpath('.//h2[@class="tt_compte"]')[0].text.strip()
def filter(self, str_type):
for key, acc_type in self.types.items():
if key == str_type:
return acc_type
return Account.TYPE_UNKNOWN
account.id = re.sub(u'[^0-9]', '', labels[-1].text)
account.label = u' '.join([account_type_str, labels[0].text.strip()])
account.type = self.ACCOUNT_TYPES.get(account_type_str, Account.TYPE_UNKNOWN)
balance = table.xpath('.//td[@class="sum_solde"]//span')[-1].text
account.balance = Decimal(FrenchTransaction.clean_amount(balance))
account.currency = account.get_currency(balance)
class AccountsPage(LoggedPage, HTMLPage):
@method
class get_list(ListElement):
item_xpath = '//table[has-class("table-synthese")]'
yield account
class item(ItemElement):
klass = Account
obj_label = Format('%s %s', CleanText('.//h2[@class="tt_compte"][1]'), CleanText('.//ul[@class="nClient"]/li[1]'))
obj_id = CleanText('.//ul[@class="nClient"]/li[last()]', symbols=u'')
obj_type = AddType(CleanText('.//h2[@class="tt_compte"][1]'))
obj_balance = CleanDecimal('.//td[@class="sum_solde"]//span[last()]', replace_dots=True)
obj_currency = u'EUR'
class Transaction(FrenchTransaction):
@ -79,7 +87,7 @@ class Transaction(FrenchTransaction):
(re.compile('^(PRLV|PRELEVEMENT) (?P<text>.*?)(- .*)?$'),
FrenchTransaction.TYPE_ORDER),
(re.compile('^CHEQUE.*'), FrenchTransaction.TYPE_CHECK),
(re.compile('^(AGIOS /|FRAIS) (?P<text>.*)'),FrenchTransaction.TYPE_BANK),
(re.compile('^(AGIOS /|FRAIS) (?P<text>.*)'), FrenchTransaction.TYPE_BANK),
(re.compile('^ABONNEMENT (?P<text>.*)'), FrenchTransaction.TYPE_BANK),
(re.compile('^REMISE (?P<text>.*)'), FrenchTransaction.TYPE_DEPOSIT),
(re.compile('^(?P<text>.*)( \d+)? QUITTANCE .*'),
@ -89,11 +97,11 @@ class Transaction(FrenchTransaction):
]
class TransactionsPage(Page):
class TransactionsPage(LoggedPage, HTMLPage):
pass
class TransactionsJSONPage(Page):
class TransactionsJSONPage(LoggedPage, JsonPage):
ROW_DATE = 0
ROW_TEXT = 2
ROW_CREDIT = -1
@ -101,7 +109,7 @@ class TransactionsJSONPage(Page):
def get_transactions(self):
seen = set()
for tr in self.document['exportData'][1:]:
for tr in self.doc['exportData'][1:]:
t = Transaction(0)
t.parse(tr[self.ROW_DATE], tr[self.ROW_TEXT])
t.set_amount(tr[self.ROW_CREDIT], tr[self.ROW_DEBIT])
@ -109,7 +117,7 @@ class TransactionsJSONPage(Page):
yield t
class ComingTransactionsPage(Page):
class ComingTransactionsPage(LoggedPage, HTMLPage):
ROW_REF = 0
ROW_TEXT = 1
ROW_DATE = 2
@ -118,7 +126,7 @@ class ComingTransactionsPage(Page):
def get_transactions(self):
data = []
for script in self.document.xpath('//script'):
for script in self.doc.xpath('//script'):
txt = script.text
if txt is None:
continue