From d9301155ec076cd80fafd594c59a15692b54b6d3 Mon Sep 17 00:00:00 2001 From: Vincent Paredes Date: Thu, 6 Aug 2015 17:46:57 +0200 Subject: [PATCH] adding support for bill on bouygues --- modules/bouygues/browser.py | 19 +++++++++++++--- modules/bouygues/module.py | 27 +++++++++++++++++++++- modules/bouygues/pages.py | 45 ++++++++++++++++++++++++++++++++++--- 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/modules/bouygues/browser.py b/modules/bouygues/browser.py index 46299a19..54089650 100644 --- a/modules/bouygues/browser.py +++ b/modules/bouygues/browser.py @@ -19,7 +19,7 @@ from weboob.browser import LoginBrowser, URL, need_login from weboob.exceptions import BrowserIncorrectPassword -from .pages import LoginPage, LoginSuccess, SendSMSPage, SendSMSErrorPage +from .pages import LoginPage, HomePage, SendSMSPage, SendSMSErrorPage, BillsPage from weboob.capabilities.messages import CantSendMessage @@ -30,7 +30,8 @@ class BouyguesBrowser(LoginBrowser): BASEURL = 'https://www.mon-compte.bouyguestelecom.fr/' TIMEOUT = 20 - home = URL('http://www.bouyguestelecom.fr/mon-compte/', LoginSuccess) + billspage = URL('http://www.bouyguestelecom.fr/mon-compte/mes-factures', BillsPage) + home = URL('http://www.bouyguestelecom.fr/mon-compte/', HomePage) login = URL('cas/login', LoginPage) sms_page = URL('http://www.mobile.service.bbox.bouyguestelecom.fr/services/SMSIHD/sendSMS.phtml', @@ -42,8 +43,12 @@ class BouyguesBrowser(LoginBrowser): sms_error_page = URL('http://www.mobile.service.bbox.bouyguestelecom.fr/services/SMSIHD/SMS_erreur.phtml', SendSMSErrorPage) + def do_login(self): - self.login.go().login(self.username, self.password) + self.login.go() + if self.home.is_here(): + return + self.page.login(self.username, self.password) if not self.home.is_here(): raise BrowserIncorrectPassword @@ -62,3 +67,11 @@ class BouyguesBrowser(LoginBrowser): raise CantSendMessage(self.page.get_error_message()) self.confirm.open() + + @need_login + def get_subscription_list(self): + return self.home.stay_or_go().get_list() + + @need_login + def iter_bills(self, subscription): + return self.billspage.stay_or_go().get_bills(subid=subscription.id) diff --git a/modules/bouygues/module.py b/modules/bouygues/module.py index 0f7f7043..7ec0f87d 100644 --- a/modules/bouygues/module.py +++ b/modules/bouygues/module.py @@ -18,7 +18,9 @@ # along with weboob. If not, see . +from weboob.capabilities.bill import CapBill, Subscription, Bill, SubscriptionNotFound, BillNotFound from weboob.capabilities.messages import CantSendMessage, CapMessages, CapMessagesPost +from weboob.capabilities.base import find_object from weboob.tools.backend import Module, BackendConfig from weboob.tools.value import ValueBackendPassword, Value @@ -28,7 +30,7 @@ from .browser import BouyguesBrowser __all__ = ['BouyguesModule'] -class BouyguesModule(Module, CapMessages, CapMessagesPost): +class BouyguesModule(Module, CapMessages, CapMessagesPost, CapBill): NAME = 'bouygues' MAINTAINER = u'Bezleputh' EMAIL = 'carton_ben@yahoo.fr' @@ -46,3 +48,26 @@ class BouyguesModule(Module, CapMessages, CapMessagesPost): if not message.content.strip(): raise CantSendMessage(u'Message content is empty.') self.browser.post_message(message) + + def iter_subscription(self): + return self.browser.get_subscription_list() + + def get_subscription(self, _id): + return find_object(self.iter_subscription(), id=_id, error=SubscriptionNotFound) + + def get_bill(self, _id): + subid = _id.split('.')[0] + subscription = self.get_subscription(subid) + + return find_object(self.iter_bills(subscription), id=_id, error=BillNotFound) + + def iter_bills(self, subscription): + if not isinstance(subscription, Subscription): + subscription = self.get_subscription(subscription) + return self.browser.iter_bills(subscription) + + def download_bill(self, bill): + if not isinstance(bill, Bill): + bill = self.get_bill(bill) + return self.browser.open('http://www.bouyguestelecom.fr/mon-compte/suiviconso/index/facturepdf?id=%s' % bill._id_bill).content + diff --git a/modules/bouygues/pages.py b/modules/bouygues/pages.py index a2222e6a..28fac360 100644 --- a/modules/bouygues/pages.py +++ b/modules/bouygues/pages.py @@ -16,10 +16,15 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . +from decimal import Decimal +import re + from weboob.capabilities.messages import CantSendMessage +from weboob.capabilities.bill import Bill, Subscription from weboob.browser.pages import HTMLPage, LoggedPage -from weboob.browser.filters.standard import CleanDecimal, CleanText, Regexp +from weboob.browser.filters.standard import CleanDecimal, CleanText, Env, Format, Date, Regexp +from weboob.browser.elements import ListElement, ItemElement, method class LoginPage(HTMLPage): @@ -30,8 +35,18 @@ class LoginPage(HTMLPage): form.submit() -class LoginSuccess(HTMLPage, LoggedPage): - pass +class HomePage(HTMLPage, LoggedPage): + @method + class get_list(ListElement): + class item(ItemElement): + klass = Subscription + + obj_label = CleanText('//span[@class="ecconumteleule"]') + obj_subscriber = CleanText('//span[@class="economligneaseule eccobold"]') + obj_id = Env('id') + + def parse(self, el): + self.env['id'] = re.sub(r'[^\d\-\.]', '', el.xpath('//span[@class="ecconumteleule"]')[0].text) class SendSMSPage(HTMLPage): @@ -50,3 +65,27 @@ class SendSMSPage(HTMLPage): class SendSMSErrorPage(HTMLPage): def get_error_message(self): return CleanText('//span[@class="txt12-o"][1]')(self.doc) + + +class BillsPage(HTMLPage): + @method + class get_bills(ListElement): + item_xpath = '//table[@class="ecconotif historique"]/tbody/tr' + + class item(ItemElement): + klass = Bill + + obj_id = Format('%s.%s', Env('subid'), Env('id')) + obj__id_bill = Env('id') + obj_date = Date(CleanText('./td[1]/span'), dayfirst=True) + obj_format = u"pdf" + obj_price = Env('price') + + def parse(self, el): + try: + deci = Decimal(el.xpath('./td[2]//span[@class="priceCT"]')[0].text) / 100 + except IndexError: + deci = 0 + self.env['price'] = Decimal(el.xpath('./td[2]/span')[0].text) + deci + onclick = el.xpath('.//td[@class="visuFacture"]/span/a/@onclick')[0] + self.env['id'] = re.findall(r'\d\d+', onclick)[0]