Commit f712ff5c authored by Damien Mat's avatar Damien Mat Committed by ntome

[americanexpress] A few code tweaks in browser.py and pages.py

parent 1f1cde8b
......@@ -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 <http://www.gnu.org/licenses/>.
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 @@ class AmericanExpressBrowser(LoginBrowser):
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 @@ class AmericanExpressBrowser(LoginBrowser):
@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:
......
......@@ -22,7 +22,6 @@ from __future__ import unicode_literals
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.capabilities.base import NotAvailable
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 @@ class AccountsPage(LoggedPage, HTMLPage):
# 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 @@ class AccountsPage(LoggedPage, HTMLPage):
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 @@ class JsonHistory(LoggedPage, JsonPage):
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 @@ class JsonHistory(LoggedPage, JsonPage):
else:
return original_amount
# obj__ref = Dict('reference_id')
obj__ref = Dict('identifier')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment