From bcac37b437787d3e2cd14aba0d9565964c9c7d39 Mon Sep 17 00:00:00 2001 From: Maxime Pommier Date: Wed, 10 Jul 2019 14:52:18 +0200 Subject: [PATCH] [bnporc] Implement iter_accounts in browser2 --- modules/bnporc/pp/browser.py | 2 +- modules/bnporc/pp/pages.py | 97 ++++++++++++++++++++++-------------- 2 files changed, 61 insertions(+), 38 deletions(-) diff --git a/modules/bnporc/pp/browser.py b/modules/bnporc/pp/browser.py index 2820367e55..367bc638c8 100644 --- a/modules/bnporc/pp/browser.py +++ b/modules/bnporc/pp/browser.py @@ -180,7 +180,7 @@ def iter_accounts(self): except (TransferAssertionError, AttributeError): pass - accounts = list(self.accounts.go().iter_accounts(ibans)) + accounts = list(self.accounts.go().iter_accounts(ibans=ibans)) self.market_syn.go(data=JSON({})) # do a post on the given URL market_accounts = self.page.get_list() # get the list of 'Comptes Titres' checked_accounts = set() diff --git a/modules/bnporc/pp/pages.py b/modules/bnporc/pp/pages.py index 4a12736217..9c7520c143 100644 --- a/modules/bnporc/pp/pages.py +++ b/modules/bnporc/pp/pages.py @@ -29,7 +29,9 @@ from weboob.browser.elements import DictElement, ListElement, TableElement, ItemElement, method from weboob.browser.filters.json import Dict -from weboob.browser.filters.standard import Format, Eval, Regexp, CleanText, Date, CleanDecimal, Field +from weboob.browser.filters.standard import ( + Format, Eval, Regexp, CleanText, Date, CleanDecimal, Field, Coalesce, Map, Env, Currency, +) from weboob.browser.filters.html import TableCell from weboob.browser.pages import JsonPage, LoggedPage, HTMLPage from weboob.capabilities import NotAvailable @@ -37,6 +39,7 @@ Account, Investment, Recipient, Transfer, TransferBankError, AddRecipientBankError, AddRecipientTimeout, ) +from weboob.capabilities.base import empty from weboob.capabilities.contact import Advisor from weboob.capabilities.profile import Person, ProfileMissing from weboob.exceptions import BrowserIncorrectPassword, BrowserUnavailable, BrowserPasswordExpired, ActionNeeded @@ -303,46 +306,66 @@ def obj_company_siren(self): class AccountsPage(BNPPage): - FAMILY_TO_TYPE = { - 1: Account.TYPE_CHECKING, - 2: Account.TYPE_SAVINGS, - 3: Account.TYPE_DEPOSIT, - 4: Account.TYPE_MARKET, - 5: Account.TYPE_LIFE_INSURANCE, - 6: Account.TYPE_LIFE_INSURANCE, - 8: Account.TYPE_LOAN, - 9: Account.TYPE_LOAN, - } - LABEL_TO_TYPE = { - u'PEA Espèces': Account.TYPE_PEA, - u'PEA Titres': Account.TYPE_PEA, - u'PEL': Account.TYPE_SAVINGS, - u'Plan Epargne Retraite Particulier': Account.TYPE_PERP, - } + @method + class iter_accounts(DictElement): + item_xpath = 'data/infoUdc/familleCompte' + + class iter_accounts_details(DictElement): + item_xpath = 'compte' + + class item(ItemElement): + FAMILY_TO_TYPE = { + 1: Account.TYPE_CHECKING, + 2: Account.TYPE_SAVINGS, + 3: Account.TYPE_DEPOSIT, + 4: Account.TYPE_MARKET, + 5: Account.TYPE_LIFE_INSURANCE, + 6: Account.TYPE_LIFE_INSURANCE, + 8: Account.TYPE_LOAN, + 9: Account.TYPE_LOAN, + } + + LABEL_TO_TYPE = { + 'PEA Espèces': Account.TYPE_PEA, + 'PEA Titres': Account.TYPE_PEA, + 'PEL': Account.TYPE_SAVINGS, + 'Plan Epargne Retraite Particulier': Account.TYPE_PERP, + } + + klass = Account + + obj_id = Dict('key') + obj_label = Coalesce( + Dict('libellePersoProduit', default=NotAvailable), + Dict('libelleProduit', default=NotAvailable), + default=NotAvailable + ) + obj_currency = Currency(Dict('devise')) + obj_type = Coalesce( + Map(Dict('libelleProduit'), LABEL_TO_TYPE, default=NotAvailable), + Map(Env('account_type'), FAMILY_TO_TYPE, default=NotAvailable), + default=Account.TYPE_UNKNOWN + ) + obj_balance = Dict('soldeDispo') + obj_coming = Dict('soldeAVenir') + obj_number = Dict('value') + + def obj_iban(self): + iban = Map(Dict('key'), Env('ibans')(self), default=NotAvailable)(self) + + if not empty(iban): + if not is_iban_valid(iban): + iban = rib2iban(rebuild_rib(iban)) + return iban + return None - def iter_accounts(self, ibans): - for f in self.path('data.infoUdc.familleCompte.*'): - for a in f.get('compte'): - iban = ibans.get(a.get('key')) - if iban is not None and not is_iban_valid(iban): - iban = rib2iban(rebuild_rib(iban)) - - acc = Account.from_dict({ - 'id': a.get('key'), - 'label': a.get('libellePersoProduit') or a.get('libelleProduit'), - 'currency': a.get('devise'), - 'type': self.LABEL_TO_TYPE.get(' '.join(a.get('libelleProduit').split())) or \ - self.FAMILY_TO_TYPE.get(f.get('idFamilleCompte')) or Account.TYPE_UNKNOWN, - 'balance': a.get('soldeDispo'), - 'coming': a.get('soldeAVenir'), - 'iban': iban, - 'number': a.get('value') - }) # softcap not used TODO don't pass this key when backend is ready # deferred cb can disappear the day after the appear, so 0 as day_for_softcap - acc._bisoftcap = {'deferred_cb': {'softcap_day': 1000, 'day_for_softcap': 0, 'date_field': 'rdate'}} - yield acc + obj__bisoftcap = {'deferred_cb': {'softcap_day': 1000, 'day_for_softcap': 1}} + + def parse(self, el): + self.env['account_type'] = Dict('idFamilleCompte')(el) class AccountsIBANPage(BNPPage): -- GitLab