From 014234576ed0baa8c41c77b1cc24040de43148f4 Mon Sep 17 00:00:00 2001 From: Romain Bignon Date: Sat, 22 Mar 2014 16:06:13 +0100 Subject: [PATCH] convert to browser2 --- modules/alloresto/backend.py | 13 +++---- modules/alloresto/browser.py | 51 +++++++++------------------ modules/alloresto/pages.py | 68 +++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 76 deletions(-) diff --git a/modules/alloresto/backend.py b/modules/alloresto/backend.py index 00c7be85..a08a6a91 100644 --- a/modules/alloresto/backend.py +++ b/modules/alloresto/backend.py @@ -45,13 +45,10 @@ class AlloRestoBackend(BaseBackend, ICapBank): self.config['password'].get()) def iter_accounts(self): - with self.browser: - for account in self.browser.get_accounts_list(): - yield account + return self.browser.get_accounts_list() def get_account(self, _id): - with self.browser: - account = self.browser.get_account(_id) + account = self.browser.get_account(_id) if account: return account @@ -59,10 +56,8 @@ class AlloRestoBackend(BaseBackend, ICapBank): raise 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/alloresto/browser.py b/modules/alloresto/browser.py index 3b610a8e..161a6a63 100644 --- a/modules/alloresto/browser.py +++ b/modules/alloresto/browser.py @@ -18,7 +18,8 @@ # along with weboob. If not, see . -from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword +from weboob.tools.browser2 import LoginBrowser, URL, need_login +from weboob.tools.browser import BrowserIncorrectPassword from .pages import LoginPage, AccountsPage @@ -26,58 +27,40 @@ from .pages import LoginPage, AccountsPage __all__ = ['AlloRestoBrowser'] -class AlloRestoBrowser(BaseBrowser): - DOMAIN = 'www.alloresto.fr' - PROTOCOL = 'http' - ENCODING = 'utf-8' - PAGES = {'http://www.alloresto.fr/identification-requise.*': LoginPage, - 'http://www.alloresto.fr/chez-moi/releve-compte-miams': AccountsPage, - } +class AlloRestoBrowser(LoginBrowser): + BASEURL = 'http://www.alloresto.fr' - def is_logged(self): - return self.page is not None and not self.is_on_page(LoginPage) + login = URL('/identification-requise.*', LoginPage) + accounts = URL('/chez-moi/releve-compte-miams', AccountsPage) - def home(self): - self.go_on_accounts_list() - - def login(self): + def do_login(self): assert isinstance(self.username, basestring) assert isinstance(self.password, basestring) - if not self.is_on_page(LoginPage): - self.location('http://www.alloresto.fr/identification-requise', no_login=True) - + self.accounts.stay_or_go() self.page.login(self.username, self.password) - if not self.is_logged(): + if self.login.is_here(): raise BrowserIncorrectPassword() - def go_on_accounts_list(self): - self.location('http://www.alloresto.fr/chez-moi/releve-compte-miams') - + @need_login def get_accounts_list(self): - if not self.is_on_page(AccountsPage): - self.go_on_accounts_list() - return self.page.get_list() + return self.accounts.stay_or_go().iter_accounts() + @need_login def get_account(self, id): assert isinstance(id, basestring) - l = self.get_accounts_list() - for a in l: + for a in self.get_accounts_list(): if a.id == id: return a return None + @need_login def get_history(self, account): - if not self.is_on_page(AccountsPage): - self.go_on_accounts_list() - - return self.page.get_transactions() + return self.accounts.stay_or_go().get_transactions(type='consommable') + @need_login def get_coming(self, account): - if not self.is_on_page(AccountsPage): - self.go_on_accounts_list() - - return self.page.get_transactions('acquisition') + return self.accounts.stay_or_go().get_transactions(type='acquisition') diff --git a/modules/alloresto/pages.py b/modules/alloresto/pages.py index 1f77803b..3000a156 100644 --- a/modules/alloresto/pages.py +++ b/modules/alloresto/pages.py @@ -21,7 +21,8 @@ import datetime from decimal import Decimal -from weboob.tools.browser import BasePage +from weboob.tools.browser2.page import HTMLPage, LoggedPage, method, ItemElement +from weboob.tools.browser2.filters import CleanDecimal, CleanText, Filter, TableCell from weboob.capabilities.bank import Account from weboob.tools.capabilities.bank.transactions import FrenchTransaction as Transaction @@ -29,46 +30,47 @@ from weboob.tools.capabilities.bank.transactions import FrenchTransaction as Tra __all__ = ['LoginPage', 'AccountsPage'] -class LoginPage(BasePage): +class LoginPage(HTMLPage): def login(self, username, password): - self.browser.select_form(nr=0) - self.browser['uname'] = username.encode(self.browser.ENCODING) - self.browser['pass'] = password.encode(self.browser.ENCODING) - self.browser.submit(nologin=True) + form = self.get_form(nr=1) + form['uname'] = username + form['pass'] = password + form.submit() -class AccountsPage(BasePage): - def get_list(self): - a = Account() - a.id = '0' - a.label = u'Compte miams' - a.balance = Decimal(self.parser.tocleanstring(self.document.xpath('//div[@class="compteur"]//strong')[0])) - a.currency = u'MIAM' - try: - a.coming = Decimal(Transaction.clean_amount(self.document.xpath('//table[@id="solde_acquisition_lignes"]//th[@class="col_montant"]')[0].text)) - except IndexError: - a.coming = Decimal('0') - yield a +class AccountsPage(LoggedPage, HTMLPage): + @method + class iter_accounts(ItemElement): + def __call__(self): + return self - COL_DATE = 0 - COL_LABEL = 1 - COL_AMOUNT = 2 + klass = Account - 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_transactions(self, _type='consommable'): - for tr in self.document.xpath('//table[@id="solde_%s_lignes"]/tbody/tr' % _type): - cols = tr.findall('td') + obj_id = '0' + obj_label = u'Compte miams' + obj_balance = CleanDecimal('//div[@class="compteur"]//strong') + obj_currency = u'MIAM' + obj_coming = CleanDecimal('//table[@id="solde_acquisition_lignes"]//th[@class="col_montant"]', default=Decimal('0')) - t = Transaction(0) - - day, month, year = self.parser.tocleanstring(cols[self.COL_DATE]).split(' ') + class MyDate(Filter): + 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 filter(self, txt): + day, month, year = txt.split(' ') day = int(day) year = int(year) month = self.MONTHS.index(month.rstrip('.')) + 1 - date = datetime.date(year, month, day) + return datetime.date(year, month, day) - label = self.parser.tocleanstring(cols[self.COL_LABEL]) - t.parse(date, label) - t.set_amount(self.parser.tocleanstring(cols[self.COL_AMOUNT])) + def get_transactions(self, type='consommable'): + class get_history(Transaction.TransactionsElement): + head_xpath = '//table[@id="solde_%s_lignes"]//thead//tr/th/text()' % type + item_xpath = '//table[@id="solde_%s_lignes"]//tbody/tr' % type - yield t + col_date = u"Date de valeur" + col_raw = u"Motif" + + class item(Transaction.TransactionElement): + obj_amount = Transaction.Amount('./td[last()]') + obj_date = AccountsPage.MyDate(CleanText(TableCell('date'))) + + return get_history(self)()