From e0917d9317894b928f22496fbfb39c72a1c08a04 Mon Sep 17 00:00:00 2001 From: Christophe Lampin Date: Mon, 22 Jul 2013 16:11:28 +0200 Subject: [PATCH] Add module for Ameli website (French Health Insurance) Signed-off-by: Christophe Lampin --- modules/ameli/__init__.py | 24 +++++++ modules/ameli/backend.py | 91 +++++++++++++++++++++++ modules/ameli/browser.py | 119 ++++++++++++++++++++++++++++++ modules/ameli/favicon.png | Bin 0 -> 6570 bytes modules/ameli/pages.py | 147 ++++++++++++++++++++++++++++++++++++++ modules/ameli/test.py | 34 +++++++++ 6 files changed, 415 insertions(+) create mode 100644 modules/ameli/__init__.py create mode 100755 modules/ameli/backend.py create mode 100755 modules/ameli/browser.py create mode 100755 modules/ameli/favicon.png create mode 100755 modules/ameli/pages.py create mode 100755 modules/ameli/test.py diff --git a/modules/ameli/__init__.py b/modules/ameli/__init__.py new file mode 100644 index 00000000..2d8d3b89 --- /dev/null +++ b/modules/ameli/__init__.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Lampin +# +# 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 AmeliBackend + + +__all__ = ['AmeliBackend'] diff --git a/modules/ameli/backend.py b/modules/ameli/backend.py new file mode 100755 index 00000000..f501f19c --- /dev/null +++ b/modules/ameli/backend.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Lampin +# +# 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 . + +import urllib +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 AmeliBrowser + +__all__ = ['AmeliBackend'] + + +class AmeliBackend(BaseBackend, ICapBill): + NAME = 'ameli' + DESCRIPTION = u'Ameli website: French Health Insurance' + MAINTAINER = u'Christophe Lampin' + EMAIL = 'weboob@lampin.net' + VERSION = '0.g' + LICENSE = 'AGPLv3+' + BROWSER = AmeliBrowser + CONFIG = BackendConfig(ValueBackendPassword('login', + label='numero de SS', + masked=False), + ValueBackendPassword('password', + label='Password', + masked=True) + ) + BROWSER = AmeliBrowser + + 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,urllib.urlencode(bill._args)) diff --git a/modules/ameli/browser.py b/modules/ameli/browser.py new file mode 100755 index 00000000..246e63b0 --- /dev/null +++ b/modules/ameli/browser.py @@ -0,0 +1,119 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Lampin +# +# 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 . + +import urllib +import mechanize +from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword +from weboob.capabilities.bill import Detail +from decimal import Decimal +from .pages import AmeliBasePage, LoginPage, HomePage, AccountPage, LastPaymentsPage, PaymentDetailsPage, BillsPage + +__all__ = ['AmeliBrowser'] + +class AmeliBrowser(BaseBrowser): + PROTOCOL = 'https' + DOMAIN = 'assure.ameli.fr' + ENCODING = None + + PAGES = {'.*_pageLabel=as_login_page.*': LoginPage, + '.*_pageLabel=as_accueil_page.*': HomePage, + '.*_pageLabel=as_etat_civil_page.*': AccountPage, + '.*_pageLabel=as_revele_mensuel_presta_page.*': BillsPage, + '.*_pageLabel=as_dernier_paiement_page': LastPaymentsPage, + '.*_actionOverride=%2Fportlets%2Fpaiements%2Fdetailpaiements&paiements.*' : PaymentDetailsPage + } + + loginp = '/PortailAS/appmanager/PortailAS/assure?_somtc=true&_pageLabel=as_login_page' + homep = '/PortailAS/appmanager/PortailAS/assure?_nfpb=true&_pageLabel=as_accueil_page' + accountp = '/PortailAS/appmanager/PortailAS/assure?_nfpb=true&_pageLabel=as_etat_civil_page' + billsp = '/PortailAS/appmanager/PortailAS/assure?_nfpb=true&_pageLabel=as_revele_mensuel_presta_page' + lastpaymentsp = '/PortailAS/appmanager/PortailAS/assure?_nfpb=true&_pageLabel=as_dernier_paiement_page' + + is_logging = False + + def home(self): + if not self.is_logged(): + self.login() + self.location(self.homep) + + 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 self.is_on_page(LastPaymentsPage): + self.location(self.lastpaymentsp) + urls = self.page.iter_last_payments() + for url in urls: + self.location(url) + assert self.is_on_page(PaymentDetailsPage) + for payment in self.page.iter_payment_details(sub): + yield payment + + 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/ameli/favicon.png b/modules/ameli/favicon.png new file mode 100755 index 0000000000000000000000000000000000000000..f3b2c28d4d340fd735eb65b506d574fe877ae2ac GIT binary patch literal 6570 zcmV;b8CB+qP)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D02*{fSaefwW^{L9 za%BKeVQFr3E>1;MAa*k@H7+qQF!XYv000>oNklev6JGK;x6!TBlpJ+%Oi{#;p+a6Wm<1RS-v@%}ZhDg4{!<9h1vPK>?? z+9v`kZFAGmiu?GM3Hbi6&Mauu;)?2Tem7$7%R3-oWiG=WN-bzmaL>*T{q65Y%zZHk z5TOTHp%WBGp`3-x7mWz*vqDK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uBzcTER6f0g8g;Fo{I5XjUXc?t3#(vZ?R!6S#iG1eL7q2ZnrNRRj1S$T@AVDk ziH|>c5YKW0RX5_pNoRIF`%vPXH2QY} zD=s&kC(3n-gP_7>=2hkN!4kz4_)+5XTPA>I^h%@?>>jVuaS1`Qgq#(2PkZsmYsN-| zU?tpce~k-%;i81kkH83Z#4 z^xEy%a_~5Axc?P;b~%6?;zlQmXeNqOCP)O32vJ zDART+S&{%@K&wO86lUpfi)jSn3_N^e=&|)-&l@N3NL39s{H7M$Pcr{>U(b;eMhI0fm0}!vpa0>WK`%9UX*= za7Kn0%?&Y%97HHIfN-cEVde|**-v;0cf_LdIU*wrekL%)$n(^g75jRIQPVbv+SWld zbXqVl?Pg3OQ%hDYg8-cbHGrB*i^=i?NJ&M2_+;?{oU^mC@rk3r4Ns<&K%7$|lF##+ z<#Uqh5xx(iY(l9VVtN=^zMEmWbU8*OkRkGF0~KMuu!7vnW-7-#8NQQz>2i|W;Tigt z9D}X`f>hR@%PWKk`Qlc#${cI3x4B7}%%kWYCd z9T0CW?. + + +from datetime import datetime +import re +from decimal import Decimal +import locale +from weboob.tools.browser import BasePage +from weboob.capabilities.bill import Subscription, Detail, Bill + + +__all__ = ['AmeliBasePage', 'LoginPage', 'HomePage', 'AccountPage', 'LastPaymentsPage', 'PaymentDetailsPage', 'BillsPage'] + +# Ugly array to avoid the use of french locale +FRENCH_MONTHS = [u'janvier', u'février', u'mars', u'avril', u'mai', u'juin', u'juillet', u'août', u'septembre', u'octobre', u'novembre', u'décembre'] + +class AmeliBasePage(BasePage): + def is_logged(self): + return len(self.document.xpath('//a[@id="logout"]')) > 0 + +class LoginPage(AmeliBasePage): + def login(self, login, password): + self.browser.select_form('connexionCompteForm') + self.browser["connexioncompte_2numSecuriteSociale"] = login.encode('utf8') + self.browser["connexioncompte_2codeConfidentiel"] = password.encode('utf8') + self.browser.submit() + +class HomePage(AmeliBasePage): + + def on_loaded(self): + pass + + +class AccountPage(AmeliBasePage): + + def iter_subscription_list(self): + idents = self.document.xpath('//div[contains(@class, "blocfond")]') + enfants = 0 + for ident in idents: + if len(ident.xpath('.//h4')) == 0: + continue + + name = self.parser.tocleanstring(ident.xpath('.//h4')[0]) + lis = ident.xpath('.//li') + if len(lis) > 3: + number = re.sub('[^\d]+', '', ident.xpath('.//li')[3].text) + else: + enfants = enfants + 1 + number = "AFFILIE" + str(enfants) + sub = Subscription(number) + sub._id = number + sub.label = unicode(name) + sub.subscriber = unicode(name) + yield sub + +class LastPaymentsPage(AmeliBasePage): + + def iter_last_payments(self): + list_table = self.document.xpath('//table[@id="ligneTabDerniersPaiements"]') + if len(list_table) > 0: + table = list_table[0].xpath('.//tr') + for tr in table: + list_a = tr.xpath('.//a') + if len(list_a) == 0: + continue + yield list_a[0].attrib.get('href') + +class PaymentDetailsPage(AmeliBasePage): + + def iter_payment_details(self, sub): + if sub._id.isdigit(): + idx = 0 + else: + idx = sub._id.replace('AFFILIE','') + if len(self.document.xpath('//div[@class="centrepage"]/h3')) > idx or self.document.xpath('//table[@id="DetailPaiement3"]') > idx: + id_str = self.document.xpath('//div[@class="centrepage"]/h3')[idx].text.strip() + m = re.match('.*le (.*) pour un montant de.*', id_str) + if m: + id_str = m.group(1) + id_date = datetime.strptime(id_str, '%d/%m/%Y').date() + id = sub._id + "." + datetime.strftime(id_date, "%Y%m%d") + table = self.document.xpath('//table[@id="DetailPaiement3"]')[idx].xpath('.//tr') + line = 1 + for tr in table: + tds = tr.xpath('.//td'); + if len(tds) == 0: + continue + date_str = tds[0].text + det = Detail() + det.id = id + "." + str(line) + det.label = unicode(tds[1].text.strip()) + if date_str is None or date_str == '': + det.infos = u'' + det.datetime = last_date + else: + det.infos = u'Payé ' + unicode(re.sub('[^\d,-]+', '', tds[2].text)) + u'€ / Base ' + unicode(re.sub('[^\d,-]+', '', tds[3].text)) + u'€ / Taux ' + unicode(re.sub('[^\d,-]+', '', tds[4].text)) + '%' + det.datetime = datetime.strptime(date_str, '%d/%m/%Y').date() + last_date = det.datetime + det.price = Decimal(re.sub('[^\d,-]+', '', tds[5].text).replace(',','.')) + line = line + 1 + yield det + +class BillsPage(AmeliBasePage): + + def iter_bills(self,sub): + table = self.document.xpath('//table[@id="tableauDecompte"]')[0].xpath('.//tr') + for tr in table: + list_tds = tr.xpath('.//td') + if len(list_tds) == 0: + continue + date_str = list_tds[0].text + month_str = date_str.split()[0] + date = datetime.strptime(re.sub(month_str,str(FRENCH_MONTHS.index(month_str) + 1),date_str),"%m %Y").date() + amount = list_tds[1].text + if amount is None: + continue + amount = re.sub(' euros','',amount) + bil = Bill() + bil.id = sub._id + "." + date.strftime("%Y%m") + bil.date = date + bil.label = u''+amount.strip() + bil.format = u'pdf' + filedate = date.strftime("%m%Y") + bil._url = '/PortailAS/PDFServletReleveMensuel.dopdf' + bil._args = {'PDF.moisRecherche': filedate} + yield bil + + def get_bill(self,bill): + self.location(bill._url, urllib.urlencode(bill._args)) + diff --git a/modules/ameli/test.py b/modules/ameli/test.py new file mode 100755 index 00000000..8df432ae --- /dev/null +++ b/modules/ameli/test.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- + +# Copyright(C) 2013 Christophe Lampin +# +# 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__ = ['AmeliTest'] + + +class AmeliTest(BackendTest): + BACKEND = 'ameli' + + def test_ameli(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)