diff --git a/modules/paypal/browser.py b/modules/paypal/browser.py index d13106f6..ae1262c5 100644 --- a/modules/paypal/browser.py +++ b/modules/paypal/browser.py @@ -19,7 +19,7 @@ from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword -from .pages import LoginPage, AccountPage, DownloadHistoryPage, SubmitPage, HistoryParser, UselessPage, HistoryPage +from .pages import LoginPage, AccountPage, DownloadHistoryPage, LastDownloadHistoryPage, SubmitPage, HistoryParser, UselessPage, HistoryPage, CSVAlreadyAsked import datetime @@ -39,7 +39,9 @@ class Paypal(BaseBrowser): '/cgi-bin/webscr\?cmd=_history-download&nav=0.3.1$': DownloadHistoryPage, '/cgi-bin/webscr\?cmd=_history&nav=0.3.0$': HistoryPage, '/cgi-bin/webscr\?cmd=_history&dispatch=[a-z0-9]+$': HistoryPage, + '/cgi-bin/webscr\?cmd=_history-download-recent$': LastDownloadHistoryPage, '/cgi-bin/webscr\?dispatch=[a-z0-9]+$': (SubmitPage, HistoryParser()), + '/cgi-bin/webscr\?cmd=_history-download-recent-submit&dispatch=[a-z0-9]+$': (SubmitPage, HistoryParser()), } DEFAULT_TIMEOUT = 30 # CSV export is slow @@ -104,11 +106,16 @@ class Paypal(BaseBrowser): if self.download_history(start, end).rows: return self.page.iter_transactions(account) assert step_max <= 365*2 # PayPal limitations as of 2014-06-16 - return self.smart_fetch(beginning=self.BEGINNING, + try: + for i in self.smart_fetch(beginning=self.BEGINNING, end=datetime.date.today(), step_min=step_min, step_max=step_max, - fetch_fn=fetch_fn) + fetch_fn=fetch_fn): + yield i + except CSVAlreadyAsked: + for i in self.download_last_history(account): + yield i def smart_fetch(self, beginning, end, step_min, step_max, fetch_fn): """ @@ -144,5 +151,11 @@ class Paypal(BaseBrowser): assert self.is_on_page(SubmitPage) return self.page.document + def download_last_history(self, account): + self.location('/en/cgi-bin/webscr?cmd=_history-download-recent') + self.page.download() + if self.page.document.rows: + return self.page.iter_transactions(account) + def transfer(self, from_id, to_id, amount, reason=None): raise NotImplementedError() diff --git a/modules/paypal/pages.py b/modules/paypal/pages.py index 1effc5de..06078c96 100644 --- a/modules/paypal/pages.py +++ b/modules/paypal/pages.py @@ -29,8 +29,10 @@ from weboob.tools.misc import to_unicode from weboob.capabilities.bank import Account, Transaction from weboob.tools.capabilities.bank.transactions import FrenchTransaction -__all__ = ['LoginPage', 'AccountPage'] +__all__ = ['LoginPage', 'AccountPage', 'LastDownloadHistoryPage'] +class CSVAlreadyAsked(Exception): + pass def clean_amount(text): """ @@ -126,6 +128,9 @@ class AccountPage(BasePage): class DownloadHistoryPage(BasePage): def download(self, start, end): + last_file_request = self.document.xpath('//table//table//table//tr[2]//td')[1].text[:-1] + if dateutil.parser.parse(last_file_request).date() == datetime.date.today(): + raise CSVAlreadyAsked('') self.browser.select_form(name='form1') self.browser['to_c'] = str(end.year) self.browser['to_a'] = str(end.month) @@ -139,6 +144,12 @@ class DownloadHistoryPage(BasePage): self.browser.submit() +class LastDownloadHistoryPage(BasePage): + def download(self): + self.browser.select_form(nr=1) + log_select = self.document.xpath('//table//form//input[@type="radio"]')[0].attrib['value'] + self.browser['log_select'] = [log_select] + self.browser.submit() class SubmitPage(BasePage): """