diff --git a/modules/americanexpress/backend.py b/modules/americanexpress/backend.py index f1acf1e4..0359781c 100644 --- a/modules/americanexpress/backend.py +++ b/modules/americanexpress/backend.py @@ -62,10 +62,5 @@ class AmericanExpressBackend(BaseBackend, ICapBank): with self.browser: transactions = list(self.browser.get_history(account)) transactions.sort(key=lambda tr: tr.rdate, reverse=True) - return [tr for tr in transactions if not tr._is_coming] + return transactions - def iter_coming(self, account): - with self.browser: - transactions = list(self.browser.get_history(account)) - transactions.sort(key=lambda tr: tr.rdate, reverse=True) - return [tr for tr in transactions if tr._is_coming] diff --git a/modules/americanexpress/browser.py b/modules/americanexpress/browser.py index 8943a10a..d773bac4 100644 --- a/modules/americanexpress/browser.py +++ b/modules/americanexpress/browser.py @@ -21,7 +21,7 @@ from urlparse import urlsplit, parse_qsl from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword -from weboob.tools.date import LinearDateGuesser +from weboob.tools.date import ChaoticDateGuesser from .pages import LoginPage, AccountsPage, TransactionsPage @@ -85,9 +85,6 @@ class AmericanExpressBrowser(BaseBrowser): url = account._link - coming = True - date_guesser = LinearDateGuesser() - while url is not None: self.select_form(name='leftnav') self.form.action = self.absurl(url) @@ -95,10 +92,7 @@ class AmericanExpressBrowser(BaseBrowser): assert self.is_on_page(TransactionsPage) - for tr in self.page.get_history(date_guesser): - if tr.amount > 0: - coming = False - tr._is_coming = coming + for tr in self.page.get_history(): yield tr if self.page.is_last(): diff --git a/modules/americanexpress/pages.py b/modules/americanexpress/pages.py index 8a193300..d480d602 100644 --- a/modules/americanexpress/pages.py +++ b/modules/americanexpress/pages.py @@ -26,6 +26,7 @@ from weboob.tools.browser import BasePage, BrokenPageError from weboob.capabilities.bank import Account from weboob.capabilities import NotAvailable from weboob.tools.capabilities.bank.transactions import FrenchTransaction as Transaction +from weboob.tools.date import ChaoticDateGuesser __all__ = ['LoginPage', 'AccountsPage', 'TransactionsPage'] @@ -78,10 +79,18 @@ class TransactionsPage(BasePage): return True - def get_debit_date(self): + def get_end_debit_date(self): for option in self.document.xpath('//select[@id="viewPeriod"]/option'): if 'selected' in option.attrib: - m = re.search('(\d+) ([\w\.]+) (\d{4})$', option.text.strip()) + m = re.search('(\d+) ([\w\.]+) (\d{4})$', option.text.strip(), re.UNICODE) + if m: + return datetime.date(int(m.group(3)), + self.MONTHS.index(m.group(2).rstrip('.')) + 1, + int(m.group(1))) + def get_beginning_debit_date(self): + for option in self.document.xpath('//select[@id="viewPeriod"]/option'): + if 'selected' in option.attrib: + m = re.search('^(\d+) ([\w\.]+) (\d{4})', option.text.strip(), re.UNICODE) if m: return datetime.date(int(m.group(3)), self.MONTHS.index(m.group(2).rstrip('.')) + 1, @@ -94,10 +103,11 @@ class TransactionsPage(BasePage): MONTHS = ['janv', u'févr', u'mars', u'avr', u'mai', u'juin', u'juil', u'août', u'sept', u'oct', u'nov', u'déc'] - def get_history(self, guesser): - debit_date = self.get_debit_date() - if debit_date is not None: - guesser.current_date = debit_date + def get_history(self): + #adding a time delta because amex have hard time to put the date in a good interval + beginning_date = self.get_beginning_debit_date() - datetime.timedelta(days=30) + end_date = self.get_end_debit_date() + guesser = ChaoticDateGuesser(beginning_date, end_date) for tr in reversed(self.document.xpath('//div[@id="txnsSection"]//tr[@class="tableStandardText"]')): cols = tr.findall('td') @@ -107,7 +117,7 @@ class TransactionsPage(BasePage): day, month = self.parser.tocleanstring(cols[self.COL_DATE]).split(' ', 1) day = int(day) month = self.MONTHS.index(month.rstrip('.')) + 1 - date = guesser.guess_date(day, month, False) + date = guesser.guess_date(day, month) try: detail = self.parser.select(cols[self.COL_TEXT], 'div.hiddenROC', 1)