From 2446a7815c93184a3a1193351465edf37c5f4381 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Tue, 4 Nov 2014 19:11:19 +0100 Subject: [PATCH] rewrite ganassurance with browser2 and fix auth and history --- modules/ganassurances/browser.py | 54 ++++++++++---------------------- modules/ganassurances/module.py | 18 +++-------- modules/ganassurances/pages.py | 50 +++++++++++++---------------- 3 files changed, 44 insertions(+), 78 deletions(-) diff --git a/modules/ganassurances/browser.py b/modules/ganassurances/browser.py index e33af36c..45bd8f6a 100644 --- a/modules/ganassurances/browser.py +++ b/modules/ganassurances/browser.py @@ -18,7 +18,8 @@ # along with weboob. If not, see . -from weboob.deprecated.browser import Browser, BrowserIncorrectPassword +from weboob.browser import LoginBrowser, URL, need_login +from weboob.exceptions import BrowserIncorrectPassword from .pages import LoginPage, AccountsPage, TransactionsPage @@ -26,24 +27,16 @@ from .pages import LoginPage, AccountsPage, TransactionsPage __all__ = ['GanAssurances'] -class GanAssurances(Browser): - PROTOCOL = 'https' - PAGES = {'https://[^/]+/wps/portal/login.*': LoginPage, - 'https://[^/]+/wps/myportal/TableauDeBord': AccountsPage, - 'https://[^/]+/wps/myportal/!ut.*': TransactionsPage, - } +class GanAssurances(LoginBrowser): + login = URL('/wps/portal/login.*', LoginPage) + accounts = URL('/wps/myportal/TableauDeBord', AccountsPage) + transactions = URL('/wps/myportal/!ut.*', TransactionsPage) def __init__(self, website, *args, **kwargs): - self.DOMAIN = website - Browser.__init__(self, *args, **kwargs) + self.BASEURL = 'https://%s' % website + super(GanAssurances, self).__init__(*args, **kwargs) - def is_logged(self): - return self.page is not None and not self.is_on_page(LoginPage) - - def home(self): - self.location('/wps/myportal/TableauDeBord') - - def login(self): + def do_login(self): """ Attempt to log in. Note: this method does nothing if we are already logged in. @@ -51,38 +44,25 @@ class GanAssurances(Browser): assert isinstance(self.username, basestring) assert isinstance(self.password, basestring) - if self.is_logged(): - return - - if not self.is_on_page(LoginPage): - self.home() + self.login.stay_or_go() self.page.login(self.username, self.password) - if not self.is_logged(): + if self.login.is_here(): raise BrowserIncorrectPassword() + @need_login def get_accounts_list(self): - if not self.is_on_page(AccountsPage): - self.location('/wps/myportal/TableauDeBord') + self.accounts.stay_or_go() return self.page.get_list() - def get_account(self, id): - assert isinstance(id, basestring) - - l = self.get_accounts_list() - for a in l: - if a.id == id: - return a - - return None - def get_history(self, account): if account._link is None: return iter([]) self.location(account._link) - assert self.is_on_page(TransactionsPage) + + assert self.transactions.is_here() return self.page.get_history() @@ -91,9 +71,9 @@ class GanAssurances(Browser): return iter([]) self.location(account._link) - assert self.is_on_page(TransactionsPage) + assert self.transactions.is_here() self.location(self.page.get_coming_link()) - assert self.is_on_page(TransactionsPage) + assert self.transactions.is_here() return self.page.get_history() diff --git a/modules/ganassurances/module.py b/modules/ganassurances/module.py index 50137b96..3f70ebbb 100644 --- a/modules/ganassurances/module.py +++ b/modules/ganassurances/module.py @@ -19,6 +19,7 @@ from weboob.capabilities.bank import CapBank, AccountNotFound +from weboob.capabilities.base import find_object from weboob.tools.backend import Module, BackendConfig from weboob.tools.ordereddict import OrderedDict from weboob.tools.value import ValueBackendPassword, Value @@ -51,22 +52,13 @@ class GanAssurancesModule(Module, CapBank): self.config['password'].get()) def iter_accounts(self): - with self.browser: - return self.browser.get_accounts_list() + return self.browser.get_accounts_list() def get_account(self, _id): - with self.browser: - account = self.browser.get_account(_id) - - if account: - return account - else: - raise AccountNotFound() + return find_object(self.browser.get_accounts_list(), id=_id, error=AccountNotFound) def iter_history(self, account): - with self.browser: - return self.browser.get_history(account) + return self.browser.get_history(account) def iter_coming(self, account): - with self.browser: - return self.browser.get_coming(account) + return self.browser.get_coming(account) diff --git a/modules/ganassurances/pages.py b/modules/ganassurances/pages.py index 160e3df2..2a954e29 100644 --- a/modules/ganassurances/pages.py +++ b/modules/ganassurances/pages.py @@ -21,21 +21,22 @@ from decimal import Decimal import re -from weboob.deprecated.browser import Page +from weboob.browser.pages import HTMLPage +from weboob.browser.elements import method from weboob.capabilities.bank import Account from weboob.tools.capabilities.bank.transactions import FrenchTransaction -class LoginPage(Page): +class LoginPage(HTMLPage): def login(self, login, passwd): - self.browser.select_form(name='loginForm') - self.browser.set_all_readonly(False) - self.browser['LoginPortletFormID'] = login.encode(self.browser.ENCODING) - self.browser['LoginPortletFormPassword1'] = passwd.encode(self.browser.ENCODING) - self.browser.submit(nologin=True) + form = self.get_form(name='loginForm') + form['LoginPortletFormID'] = login + form['LoginPortletFormPassword1'] = passwd + form.submit() -class AccountsPage(Page): +class AccountsPage(HTMLPage): + logged = True ACCOUNT_TYPES = {u'Solde des comptes bancaires - Groupama Banque': Account.TYPE_CHECKING, u'Epargne bancaire constituée - Groupama Banque': Account.TYPE_SAVINGS, } @@ -44,7 +45,7 @@ class AccountsPage(Page): account_type = Account.TYPE_UNKNOWN accounts = [] - for tr in self.document.xpath('//table[@class="ecli"]/tr'): + for tr in self.doc.xpath('//table[@class="ecli"]/tr'): if tr.attrib.get('class', '') == 'entete': account_type = self.ACCOUNT_TYPES.get(tr.find('th').text.strip(), Account.TYPE_UNKNOWN) continue @@ -88,28 +89,21 @@ class Transaction(FrenchTransaction): ] -class TransactionsPage(Page): - def get_history(self): - count = 0 - for tr in self.document.xpath('//table[@id="releve_operation"]/tr'): - tds = tr.findall('td') +class TransactionsPage(HTMLPage): + logged = True - if len(tds) < 4: - continue + @method + class get_history(Transaction.TransactionsElement): + head_xpath = '//table[@id="releve_operation"]//tr/th' + item_xpath = '//table[@id="releve_operation"]//tr' - t = Transaction(count) + col_date = [u'Date opé'] + col_vdate = [u'Date valeur'] - date = u''.join([txt.strip() for txt in tds[0].itertext()]) - raw = u' '.join([txt.strip() for txt in tds[1].itertext()]) - debit = u''.join([txt.strip() for txt in tds[-2].itertext()]) - credit = u''.join([txt.strip() for txt in tds[-1].itertext()]) - t.parse(date, re.sub(r'[ ]+', ' ', raw)) - t.set_amount(credit, debit) - - yield t - - count += 1 + class item(Transaction.TransactionElement): + def condition(self): + return len(self.el.xpath('./td')) > 3 def get_coming_link(self): - a = self.document.getroot().cssselect('div#sous_nav ul li a.bt_sans_off')[0] + a = self.doc.getroot().cssselect('div#sous_nav ul li a.bt_sans_off')[0] return re.sub('[ \t\r\n]+', '', a.attrib['href'])