From f712ff5c713a43d3019258b5669813b182aaf47e Mon Sep 17 00:00:00 2001 From: Damien Mat Date: Thu, 25 Jul 2019 15:45:53 +0200 Subject: [PATCH] [americanexpress] A few code tweaks in browser.py and pages.py --- modules/americanexpress/browser.py | 45 ++++++++++++++++-------------- modules/americanexpress/pages.py | 16 +++++------ 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/modules/americanexpress/browser.py b/modules/americanexpress/browser.py index a1db82ea28..35e433d6a8 100644 --- a/modules/americanexpress/browser.py +++ b/modules/americanexpress/browser.py @@ -17,17 +17,20 @@ # You should have received a copy of the GNU Lesser General Public License # along with this weboob module. If not, see . +from __future__ import unicode_literals + import datetime from weboob.exceptions import BrowserIncorrectPassword from weboob.browser.browsers import LoginBrowser, need_login from weboob.browser.exceptions import HTTPNotFound, ServerError from weboob.browser.url import URL +from dateutil.parser import parse as parse_date from .pages import ( AccountsPage, JsonBalances, JsonPeriods, JsonHistory, JsonBalances2, CurrencyPage, LoginPage, WrongLoginPage, AccountSuspendedPage, - NoCardPage, NotFoundPage + NoCardPage, NotFoundPage, ) @@ -51,14 +54,14 @@ class AmericanExpressBrowser(LoginBrowser): js_periods = URL(r'/account-data/v1/financials/statement_periods', JsonPeriods) currency_page = URL(r'https://www.aexp-static.com/cdaas/axp-app/modules/axp-offers/1.11.1/fr-fr/axp-offers.json', CurrencyPage) - no_card = URL('https://www.americanexpress.com/us/content/no-card/', - 'https://www.americanexpress.com/us/no-card/', NoCardPage) + no_card = URL(r'https://www.americanexpress.com/us/content/no-card/', + r'https://www.americanexpress.com/us/no-card/', NoCardPage) not_found = URL(r'/accounts/error', NotFoundPage) SUMMARY_CARD_LABEL = [ - u'PAYMENT RECEIVED - THANK YOU', - u'PRELEVEMENT AUTOMATIQUE ENREGISTRE-MERCI' + 'PAYMENT RECEIVED - THANK YOU', + 'PRELEVEMENT AUTOMATIQUE ENREGISTRE-MERCI', ] def __init__(self, *args, **kwargs): @@ -73,7 +76,6 @@ def do_login(self): if self.wrong_login.is_here() or self.login.is_here() or self.account_suspended.is_here(): raise BrowserIncorrectPassword() - @need_login def get_accounts(self): self.accounts.go() @@ -120,29 +122,30 @@ def iter_history(self, account): @need_login def iter_coming(self, account): - """ - Coming transactions can be found in a'pending' JSON if it exists (corresponding a 'Transactions en attente' tab on the website), - as well as in a 'posted' JSON (corresponding a 'Transactions enregistrées' tab on the website for futur transactions) - """ + # Coming transactions can be found in a 'pending' JSON if it exists + # ('En attente' tab on the website), as well as in a 'posted' JSON + # ('Enregistrées' tab on the website) + # "pending" have no vdate and debit date is in future self.js_periods.go(headers={'account_token': account._token}) - date = datetime.datetime.strptime(self.page.get_periods()[0], '%Y-%m-%d').date() periods = self.page.get_periods() + date = parse_date(periods[0]).date() today = datetime.date.today() - try: - self.js_pending.go(offset=0, headers={'account_token': account._token}) - # when the latest period ends today we can't know the coming debit date - if date != today: + # when the latest period ends today we can't know the coming debit date + if date != today: + try: + self.js_pending.go(offset=0, headers={'account_token': account._token}) + except ServerError as exc: + # At certain times of the month a connection might not have pendings; + # in that case, `js_pending.go` would throw a 502 error Bad Gateway + error_code = exc.response.json().get('code') + error_message = exc.response.json().get('message') + self.logger.warning('No pendings page to access to, got error %s and message "%s" instead.', error_code, error_message) + else: for tr in self.page.iter_history(periods=periods): if tr._owner == account._idforJSON: tr.date = date yield tr - except ServerError as exc: - # At certain time of the month a connection might not have pendings; - # in that case, `js_pending.go` would throw a 502 error Bad Gateway - error_code = exc.response.json().get('code') - error_message = exc.response.json().get('message') - self.logger.warning('No pendings page to access to, got error %s and message "%s" instead.' % (error_code, error_message)) # "posted" have a vdate but debit date can be future or past for p in periods: diff --git a/modules/americanexpress/pages.py b/modules/americanexpress/pages.py index 2f969ca305..5c7b59b4f5 100644 --- a/modules/americanexpress/pages.py +++ b/modules/americanexpress/pages.py @@ -22,7 +22,6 @@ from ast import literal_eval from decimal import Decimal import re -from dateutil.parser import parse as parse_date from weboob.browser.pages import LoggedPage, JsonPage, HTMLPage from weboob.browser.elements import ItemElement, DictElement, method @@ -33,11 +32,13 @@ from weboob.tools.json import json from weboob.tools.compat import basestring from weboob.exceptions import ActionNeeded, BrowserUnavailable +from dateutil.parser import parse as parse_date def float_to_decimal(f): return Decimal(str(f)) + def parse_decimal(s): # we might get 1,399,680 in rupie indonésienne if s.count(',') > 1 and not s.count('.'): @@ -93,7 +94,7 @@ def iter_accounts(self): # search for products to get products list for index, el in enumerate(data[14][2]): if 'products' in el: - accounts_data = data[14][2][index+1] + accounts_data = data[14][2][index + 1] assert len(accounts_data) == 2 assert accounts_data[1][4] == 'productsList' @@ -105,14 +106,14 @@ def iter_accounts(self): if isinstance(account_data, basestring): balances_token = account_data - elif isinstance(account_data, list) and not account_data[4][2][0]=="Canceled": + elif isinstance(account_data, list) and not account_data[4][2][0] == "Canceled": acc = Account() if len(account_data) > 15: token.append(account_data[-11]) - acc._idforJSON = account_data[10][-1] + acc._idforJSON = account_data[10][-1] else: acc._idforJSON = account_data[-5][-1] - acc._idforJSON = re.sub('\s+', ' ', acc._idforJSON) + acc._idforJSON = re.sub(r'\s+', ' ', acc._idforJSON) acc.number = '-%s' % account_data[2][2] acc.label = '%s %s' % (account_data[6][4], account_data[10][-1]) acc._balances_token = acc.id = balances_token @@ -172,7 +173,8 @@ def obj_type(self): obj_raw = CleanText(Dict('description', default='')) def obj_date(self): - """ 'statement_end_date' might be absent from this json, we must match the rdate with the right date period """ + # 'statement_end_date' might be absent from this json, + # we must match the rdate with the right date period _date = Date(Dict('statement_end_date', default=None), default=NotAvailable)(self) if not _date: periods = Env('periods')(self) @@ -207,6 +209,4 @@ def obj_original_amount(self): else: return original_amount - # obj__ref = Dict('reference_id') obj__ref = Dict('identifier') - -- GitLab