diff --git a/modules/edf/__init__.py b/modules/edf/__init__.py new file mode 100644 index 00000000..fa2bfdd5 --- /dev/null +++ b/modules/edf/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Gouiran +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from .backend import EdfBackend + + +__all__ = ['EdfBackend'] diff --git a/modules/edf/backend.py b/modules/edf/backend.py new file mode 100644 index 00000000..a070c56c --- /dev/null +++ b/modules/edf/backend.py @@ -0,0 +1,90 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Gouiran +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from weboob.capabilities.bill import ICapBill, SubscriptionNotFound, BillNotFound, Subscription, Bill +from weboob.tools.backend import BaseBackend, BackendConfig +from weboob.tools.value import ValueBackendPassword +from .browser import EdfBrowser + +__all__ = ['EdfBackend'] + + +class EdfBackend(BaseBackend, ICapBill): + NAME = 'edf' + DESCRIPTION = u'Edf website: French power provider' + MAINTAINER = u'Christophe Gouiran' + EMAIL = 'bechris13250@gmail.com' + VERSION = '0.g' + LICENSE = 'AGPLv3+' + BROWSER = EdfBrowser + CONFIG = BackendConfig(ValueBackendPassword('login', + label='Identifiant', + masked=False), + ValueBackendPassword('password', + label='Password', + masked=True) + ) + BROWSER = EdfBrowser + + def create_default_browser(self): + return self.create_browser(self.config['login'].get(), + self.config['password'].get()) + + def iter_subscription(self): + return self.browser.iter_subscription_list() + + def get_subscription(self, _id): + with self.browser: + subscription = self.browser.get_subscription(_id) + if not subscription: + raise SubscriptionNotFound() + else: + return subscription + + def iter_bills_history(self, subscription): + if not isinstance(subscription, Subscription): + subscription = self.get_subscription(subscription) + with self.browser: + return self.browser.iter_history(subscription) + + def get_details(self, subscription): + if not isinstance(subscription, Subscription): + subscription = self.get_subscription(subscription) + with self.browser: + return self.browser.iter_details(subscription) + + def iter_bills(self, subscription): + if not isinstance(subscription, Subscription): + subscription = self.get_subscription(subscription) + with self.browser: + return self.browser.iter_bills(subscription) + + def get_bill(self, id): + with self.browser: + bill = self.browser.get_bill(id) + if not bill: + raise BillNotFound() + else: + return bill + + def download_bill(self, bill): + if not isinstance(bill, Bill): + bill = self.get_bill(bill) + with self.browser: + return self.browser.readurl(bill._url) diff --git a/modules/edf/browser.py b/modules/edf/browser.py new file mode 100644 index 00000000..63e1e8c2 --- /dev/null +++ b/modules/edf/browser.py @@ -0,0 +1,118 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Gouiran +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + +from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword +from weboob.capabilities.bill import Detail +from decimal import Decimal +from .pages import LoginPage, FirstRedirectionPage, SecondRedirectionPage, OtherPage, AccountPage, BillsPage, LastPaymentsPage, LastPaymentsPage2 + +__all__ = ['EdfBrowser'] + + +class EdfBrowser(BaseBrowser): + PROTOCOL = 'https' + DOMAIN = 'monagencepart.edf.fr' + ENCODING = None + #DEBUG_HTTP = True + #DEBUG_MECHANIZE = True + + PAGES = {'.*page_authentification': LoginPage, + '.*serviceRedirectionAel.*': FirstRedirectionPage, + '.*Routage\?service=.*': SecondRedirectionPage, + '.*routage/Routage.*': SecondRedirectionPage, + '.*page_synthese_client': AccountPage, + '.*autres-pages-.*': OtherPage, + '.*page_mes_factures.*': BillsPage, + '.*portlet_mon_paiement_1.*': LastPaymentsPage, + '.*portlet_echeancier_2.*': LastPaymentsPage2 + } + + loginp = '/ASPFront/appmanager/ASPFront/front?_nfpb=true&_pageLabel=page_authentification' + accountp = '/ASPFront/appmanager/ASPFront/front?_nfls=false&_nfpb=true&_pageLabel=private/page_synthese_client' + billsp = '/ASPFront/appmanager/ASPFront/front?_nfls=false&_nfpb=true&_pageLabel=private/page_mes_factures&portletInstance2=portlet_suivi_consommation_2' + lastpaymentsp = '/ASPFront/appmanager/ASPFront/front?_nfls=false&_nfpb=true&_pageLabel=private/page_mon_paiement&portletInstance=portlet_mon_paiement_1' + + is_logging = False + + def home(self): + if not self.is_logged(): + self.login() + + def is_logged(self): + logged = self.page and self.page.is_logged() or self.is_logging + self.logger.debug('logged: %s' % (logged)) + return logged + + def login(self): + # Do we really need to login? + if self.is_logged(): + self.logger.debug('Already logged in') + return + + self.is_logging = True + + self.location(self.loginp) + self.page.login(self.username, self.password) + + self.is_logging = False + + if not self.is_logged(): + raise BrowserIncorrectPassword() + + def iter_subscription_list(self): + if not self.is_on_page(AccountPage): + self.location(self.accountp) + return self.page.iter_subscription_list() + + def get_subscription(self, id): + assert isinstance(id, basestring) + for sub in self.iter_subscription_list(): + if id == sub._id: + return sub + return None + + def iter_history(self, sub): + if not sub._id.isdigit(): + return [] + if not self.is_on_page(LastPaymentsPage): + self.location(self.lastpaymentsp) + return self.page.iter_payments(sub) + + def iter_details(self, sub): + det = Detail() + det.id = sub.id + det.label = sub.label + det.infos = '' + det.price = Decimal('0.0') + yield det + + def iter_bills(self, sub): + if not sub._id.isdigit(): + return [] + if not self.is_on_page(BillsPage): + self.location(self.billsp) + return self.page.iter_bills(sub) + + def get_bill(self, id): + assert isinstance(id, basestring) + subs = self.iter_subscription_list() + for sub in subs: + for b in self.iter_bills(sub): + if id == b.id: + return b diff --git a/modules/edf/favicon.png b/modules/edf/favicon.png new file mode 100644 index 00000000..17507812 Binary files /dev/null and b/modules/edf/favicon.png differ diff --git a/modules/edf/pages.py b/modules/edf/pages.py new file mode 100644 index 00000000..62c84c60 --- /dev/null +++ b/modules/edf/pages.py @@ -0,0 +1,167 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Gouiran +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from datetime import datetime +import re +import urllib +from decimal import Decimal +from weboob.tools.browser import BasePage +from weboob.capabilities.bill import Subscription, Detail, Bill + +__all__ = ['AccountPage', 'BillsPage', 'EdfBasePage', 'FirstRedirectionPage', 'HomePage', 'LastPaymentsPage', 'LastPaymentsPage2', 'LoginPage', 'OtherPage', 'SecondRedirectionPage'] +base_url = "http://particuliers.edf.com/" + +class EdfBasePage(BasePage): + def is_logged(self): + return (u'Me déconnecter' in self.document.xpath('//a/text()')) \ + or (self.document.xpath('//table[contains(@summary, "Informations sur mon")]')) + + +class LoginPage(EdfBasePage): + def login(self, login, password): + self.browser.select_form("identification") + self.browser["login"] = str(login) + self.browser["pswd"] = str(password) + self.browser.submit() + + +class HomePage(EdfBasePage): + def on_loaded(self): + pass + +class FirstRedirectionPage(EdfBasePage): + def on_loaded(self): + self.browser.select_form("form1") + self.browser.submit() + +class SecondRedirectionPage(EdfBasePage): + def on_loaded(self): + self.browser.select_form("redirectForm") + self.browser.submit() + +class OtherPage(EdfBasePage): + def on_loaded(self): + self.browser.open(base_url) + + +class AccountPage(EdfBasePage): + + def iter_subscription_list(self): + boxHeader = self.document.xpath('//div[@class="boxHeader"]')[0] + subscriber = self.parser.tocleanstring(boxHeader.xpath('.//p')[0]) + contract = self.parser.tocleanstring(boxHeader.xpath('.//p[@class="folderNumber"]')[0]) + if not re.search('^Contrat n\xb0\s*', contract): + return + contract = re.sub('Contrat n\xb0\s*', '', contract) + number = re.sub('[^\d]', '', contract) + sub = Subscription(number) + sub._id = number + sub.label = subscriber + sub.subscriber = subscriber + yield sub + + +class BillsPage(EdfBasePage): + + def iter_bills(self, sub): + + #pdb.set_trace() + years = [None] + self.document.xpath('//ul[@class="years"]/li/a') + + for year in years: + #pdb.set_trace() + if year is not None and year.attrib['href']: + self.browser.location(year.attrib['href']) + + tables = self.browser.page.document.xpath('//table[contains(@summary, "factures")]') + for table in tables: + for tr in table.xpath('.//tr'): + list_tds = tr.xpath('.//td') + if len(list_tds) == 0: + continue + url = re.sub('[\r\n\t]', '', list_tds[0].xpath('.//a')[0].attrib['href']) + date_search = re.search('dateFactureQE=(\d+/\d+/\d+)', url) + if not date_search: + continue + + date = datetime.strptime(date_search.group(1), "%d/%m/%Y").date() + amount = self.parser.tocleanstring(list_tds[2]) + if amount is None: + continue + + # Remove SPACE character + amount = re.sub(u'\xa0', '', amount) + + # Remove euro character + amount = re.sub(u'\u20ac', '', amount) + + bil = Bill() + bil.id = sub._id + "." + date.strftime("%Y%m%d") + bil.date = date + bil.label = u''+amount.strip() + bil.format = u'pdf' + bil._url = url + yield bil + + def get_bill(self, bill): + self.location(bill._url) + +class LastPaymentsPage(EdfBasePage): + + def on_loaded(self): + + # Here we simulate ajax request to following URL: + # https://monagencepart.edf.fr/ASPFront/appmanager/ASPFront/front/portlet_echeancier_2?_nfpb=true&_portlet.contentOnly=true&_portlet.instanceLabel=portlet_echeancier_2&_portlet.contentMode=FRAGMENT&_portlet.async=true&_portlet.pageLabel=page_mon_paiement&_portlet.lafUniqueId=aspDefinitionLabel&_portlet.portalUrl=%2FASPFront%2Fappmanager%2FASPFront%2Ffront&_portlet.portalId=ASPFront%09front&_portlet.contentType=text%2Fhtml%3B+charset%3DUTF-8&_portlet.asyncMode=compat_9_2&_portlet.title=CalendrierpaiementController&_nfsp=true + params = { + '_nfpb': 'true', + '_portlet.async': 'true', + '_portlet.portalId': 'ASPFront\tfront', + '_portlet.contentOnly': 'true', + '_portlet.title': 'CalendrierpaiementController', + '_portlet.pageLabel': 'page_mon_paiement', + '_portlet.asyncMode': 'compat_9_2', + '_portlet.lafUniqueId': 'aspDefinitionLabel', + '_portlet.contentMode': 'FRAGMENT', + '_portlet.instanceLabel': 'portlet_echeancier_2', + '_portlet.contentType': 'text/html; charset=UTF-8', + '_portlet.portalUrl': '/ASPFront/appmanager/ASPFront/front', + '_nfsp': 'true' + } + + self.browser.location('/ASPFront/appmanager/ASPFront/front/portlet_echeancier_2?%s' % urllib.urlencode(params)) + +class LastPaymentsPage2(EdfBasePage): + def iter_payments(self, sub): + + table = self.browser.page.document.xpath('//table[contains(@summary, "Informations sur mon")]')[0] + for tr in table.xpath('.//tr'): + list_tds = tr.xpath('.//td') + if len(list_tds) == 0: + continue + date = datetime.strptime(self.parser.tocleanstring(list_tds[0]), "%d/%m/%Y").date() + amount = self.parser.tocleanstring(list_tds[1]) + if amount is None: + continue + det = Detail() + det.id = sub._id + "." + date.strftime("%Y%m%d") + det.price = Decimal(re.sub('[^\d,-]+', '', amount).replace(',', '.')) + det.datetime = date + det.label = unicode(self.parser.tocleanstring(list_tds[2])) + yield det diff --git a/modules/edf/test.py b/modules/edf/test.py new file mode 100644 index 00000000..83c8cd90 --- /dev/null +++ b/modules/edf/test.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Gouiran +# +# This file is part of weboob. +# +# weboob is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# weboob is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with weboob. If not, see . + + +from weboob.tools.test import BackendTest + + +__all__ = ['EdfTest'] + + +class EdfTest(BackendTest): + BACKEND = 'edf' + + def test_edf(self): + for subscription in self.backend.iter_subscription(): + list(self.backend.iter_bills_history(subscription.id)) + for bill in self.backend.iter_bills(subscription.id): + self.backend.download_bill(bill.id)