From ae82341227324a9f190ffb217855589c848b2d45 Mon Sep 17 00:00:00 2001 From: Olivier Da Rocha Date: Wed, 30 Dec 2020 13:37:49 +0100 Subject: [PATCH] [bp] handle account details page not being available Sometimes the details page is not yet accessible for some accounts. Prevent crashing in this case. --- modules/bp/browser.py | 12 +++++++++--- modules/bp/pages/accountlist.py | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/modules/bp/browser.py b/modules/bp/browser.py index 73e9c27bfa..9b942aba2a 100644 --- a/modules/bp/browser.py +++ b/modules/bp/browser.py @@ -564,9 +564,15 @@ def get_accounts_list(self): elif account.type == Account.TYPE_PERP: # PERP balances must be fetched from the details page, # otherwise we just scrape the "Rente annuelle estimée": - balance = self.open(account.url).page.get_balance() - if balance is not None: - account.balance = balance + balance_page = self.open(account.url).page + # Sometimes the balance page is not available (with the site saying the account detail will + # soon be available). In the meantime, just skip it and use the balance we already have. + if balance_page and isinstance(balance_page, SavingAccountSummary): + balance = balance_page.get_balance() + if balance is not None: + account.balance = balance + else: + self.logger.debug('Balance page not yet available for account %r', account) accounts.append(account) else: diff --git a/modules/bp/pages/accountlist.py b/modules/bp/pages/accountlist.py index 048f67fe9f..daa20b083a 100644 --- a/modules/bp/pages/accountlist.py +++ b/modules/bp/pages/accountlist.py @@ -116,6 +116,10 @@ def obj_coming(self): coming = 0 details_page = self.page.browser.open(Field('url')(self)) + if not details_page.page: + # Details page might not always be available + return NotAvailable + coming_op_link = Link( '//a[contains(text(), "Opérations à venir")]', default=NotAvailable @@ -157,6 +161,10 @@ def obj_iban(self): return NotAvailable details_page = self.page.browser.open(Field('url')(self)).page + if not details_page: + # Details page might not always be available + return NotAvailable + rib_link = Link('//a[abbr[contains(text(), "RIB")]]', default=NotAvailable)(details_page.doc) if rib_link: response = self.page.browser.open(rib_link) @@ -516,7 +524,12 @@ class AccountRIB(LoggedPage, RawPage): iban_regexp = r'[A-Z]{2}\d{12}[0-9A-Z]{11}\d{2}' def get_iban(self): - m = re.search(self.iban_regexp, extract_text(self.data)) + content = extract_text(self.data) + if not content: + # This can happen if there's an error on the site while rendering the PDF (no RIB is shown) + return NotAvailable + + m = re.search(self.iban_regexp, content) if m: return unicode(m.group(0)) return None -- GitLab