Convert poivy to Browser2

This commit is contained in:
Florent 2014-03-20 15:07:10 +01:00
commit 8ec4f24c9a
4 changed files with 93 additions and 114 deletions

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2013 Florent Fourcot
# Copyright(C) 2013-2014 Florent Fourcot
#
# This file is part of weboob.
#
@ -17,7 +17,8 @@
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BasePage
from weboob.tools.browser2.page import HTMLPage, LoggedPage, method, ListElement, ItemElement
from weboob.tools.browser2.filters import Env, CleanText, CleanDecimal, Field, Attr, Filter, Time, Date
from weboob.capabilities.bill import Subscription, Detail
from decimal import Decimal, InvalidOperation
from datetime import datetime, date, time
@ -27,15 +28,11 @@ import re
__all__ = ['LoginPage', 'HomePage', 'HistoryPage', 'BillsPage', 'ErrorPage']
class ErrorPage(BasePage):
def on_loaded(self):
class ErrorPage(HTMLPage):
pass
class LoginPage(BasePage):
def on_loaded(self):
pass
class LoginPage(HTMLPage):
def _predicate_form(self, form):
try:
return form.attrs['class'] == "form-detail"
@ -43,73 +40,78 @@ class LoginPage(BasePage):
return False
def login(self, login, password):
captcha = self.document.xpath('//label[@class="label_captcha_input"]')
captcha = self.doc.xpath('//label[@class="label_captcha_input"]')
if len(captcha) > 0:
return False
form_newsletter = self.document.xpath('//form[@id="newsletter_form"]')[0]
hidden_input = form_newsletter.xpath('./input[@type="hidden"]')[0]
hidden_id = hidden_input.attrib["value"]
hidden_name = hidden_input.attrib["name"]
xpath_hidden = '//form[@id="newsletter_form"]/input[@type="hidden"]'
hidden_id = Attr(xpath_hidden, "value")(self.doc)
hidden_name = Attr(xpath_hidden, "name")(self.doc)
# Form without name
self.browser.select_form(predicate=self._predicate_form)
self.browser.set_all_readonly(False)
self.browser['login[username]'] = login.encode('iso-8859-1')
self.browser['login[password]'] = password.encode('iso-8859-1')
self.browser[hidden_name] = hidden_id
self.browser.submit(nologin=True)
form = self.get_form(xpath="//form[@class='form-detail']")
form['login[username]'] = login.encode('iso-8859-1')
form['login[password]'] = password.encode('iso-8859-1')
form[hidden_name] = hidden_id
form.submit()
return True
class HomePage(BasePage):
def on_loaded(self):
pass
class Insert2(Filter):
"""
Insert two Filters inside a string
"""
def __init__(self, selector, selector2, string):
super(Insert2, self).__init__(selector)
self.string = string
self.selector2 = selector2
def get_list(self):
spanabo = self.document.xpath('//span[@class="welcome-text"]/b')[0]
owner = spanabo.text_content()
credit = self.document.xpath('//span[@class="balance"]')[0].text_content()
def __call__(self, item):
value = self.selector(item)
value2 = self.selector2(item)
return self.filter(value, value2)
subscription = Subscription(owner)
subscription.label = u"Poivy - %s - %s" % (owner, credit)
subscription._balance = Decimal(re.sub(u'[^\d\-\.]', '', credit))
return [subscription]
def filter(self, txt, txt2):
return self.string % (txt, txt2)
class HistoryPage(BasePage):
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
class HomePage(LoggedPage, HTMLPage):
def on_loaded(self):
pass
@method
class get_list(ListElement):
item_xpath = '.'
def get_calls(self):
table = self.document.xpath('//table/tbody')[0]
for tr in table.xpath('tr'):
tds = tr.xpath('td')
class item(ItemElement):
klass = Subscription
rawdate = tds[0].text_content()
splitdate = rawdate.split('-')
month_no = self.months.index(splitdate[1]) + 1
mydate = date(int(splitdate[2]), month_no, int(splitdate[0]))
rawtime = tds[1].text_content()
mytime = time(*[int(x) for x in rawtime.split(":")])
price = re.sub(u'[^\d\-\.]', '', tds[6].text)
detail = Detail()
detail.datetime = datetime.combine(mydate, mytime)
detail.label = u"%s from %s to %s - %s" % (tds[2].text, tds[3].text, tds[4].text, tds[5].text)
try:
detail.price = Decimal(price)
except InvalidOperation:
detail.price = Decimal(0) # free calls
detail.currency = 'EUR'
yield detail
obj_id = CleanText('//span[@class="welcome-text"]/b')
obj__balance = CleanDecimal(CleanText('//span[@class="balance"]'), replace_dots=False)
obj_label = Insert2(Field('id'), Field('_balance'), u"Poivy - %s - %s")
class BillsPage(BasePage):
def on_loaded(self):
pass
class HistoryPage(LoggedPage, HTMLPage):
@method
class get_calls(ListElement):
item_xpath = '//table/tbody/tr'
class item(ItemElement):
klass = Detail
obj_datetime = Env('datetime')
obj_price = CleanDecimal('td[7]', replace_dots=False)
obj_currency = u'EUR'
obj_label = Env('label')
def parse(self, el):
tds = el.xpath('td')
mydate = Date(CleanText('td[1]'))(el)
mytime = Time(CleanText('td[2]'))(el)
self.env['datetime'] = datetime.combine(mydate, mytime)
self.env['label'] = u"%s from %s to %s - %s" % (tds[2].text, tds[3].text, tds[4].text, tds[5].text)
#TODO
class BillsPage(HTMLPage):
pass