browser.py 4.75 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
# Copyright(C) 2013-2015      Christophe Lampin
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# 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 <http://www.gnu.org/licenses/>.

Nicolas Gattolin's avatar
Nicolas Gattolin committed
20 21
from __future__ import unicode_literals

22
from weboob.browser import LoginBrowser, URL, need_login
23
from weboob.exceptions import BrowserIncorrectPassword, ActionNeeded
24 25 26
from .pages import (
    LoginPage, HomePage, CguPage, AccountPage, LastPaymentsPage, PaymentsPage, PaymentDetailsPage, Raw, UnavailablePage,
)
ntome's avatar
ntome committed
27
from weboob.tools.compat import basestring
28

29 30 31

__all__ = ['AmeliBrowser']

32

33 34
class AmeliBrowser(LoginBrowser):
    BASEURL = 'https://assure.ameli.fr'
35

Nicolas Gattolin's avatar
Nicolas Gattolin committed
36 37 38 39 40 41 42
    loginp = URL(r'/PortailAS/appmanager/PortailAS/assure\?.*_pageLabel=as_login_page', LoginPage)
    homep = URL(r'/PortailAS/appmanager/PortailAS/assure\?_nfpb=true&_pageLabel=as_accueil_page', HomePage)
    cgup = URL(r'/PortailAS/appmanager/PortailAS/assure\?_nfpb=true&_pageLabel=as_conditions_generales_page', CguPage)
    accountp = URL(r'/PortailAS/appmanager/PortailAS/assure\?_nfpb=true&_pageLabel=as_info_perso_page', AccountPage)
    paymentsp = URL(r'/PortailAS/appmanager/PortailAS/assure\?_nfpb=true&_pageLabel=as_paiements_page', PaymentsPage)
    paymentdetailsp = URL(r'/PortailAS/paiements.do\?actionEvt=chargerDetailPaiements.*', PaymentDetailsPage)
    lastpaymentsp = URL(r'/PortailAS/paiements.do\?actionEvt=afficherPaiements.*', LastPaymentsPage)
43
    pdf_page = URL(r'PortailAS/PDFServletReleveMensuel.dopdf\?PDF.moisRecherche=.*', Raw)
44
    unavailablep = URL(r'/vu/INDISPO_COMPTE_ASSURES.html', UnavailablePage)
45

46 47
    def do_login(self):
        self.logger.debug('call Browser.do_login')
48

49 50
        # set this cookie to get login form in response
        self.session.cookies['hbc'] = ''
51 52 53
        self.loginp.stay_or_go()
        if self.homep.is_here():
            return True
54 55 56

        self.page.login(self.username, self.password)

57 58 59 60
        error = self.page.is_error()
        if error:
            raise BrowserIncorrectPassword(error)

61 62 63 64 65
        self.page.locate_to_cgu_page()
        if self.cgup.is_here():
            raise ActionNeeded(self.page.get_cgu())

        self.homep.stay_or_go()  # Redirection not interpreted by browser. Manually redirect on homep
66 67

        if not self.homep.is_here():
68
            raise BrowserIncorrectPassword()
Kitof's avatar
Kitof committed
69

70
    @need_login
71
    def iter_subscription_list(self):
72 73
        self.logger.debug('call Browser.iter_subscription_list')
        self.accountp.stay_or_go()
74 75
        return self.page.iter_subscription_list()

76
    @need_login
77
    def get_subscription(self, id):
78
        self.logger.debug('call Browser.get_subscription')
79 80 81
        assert isinstance(id, basestring)
        for sub in self.iter_subscription_list():
            if id == sub._id:
82
                return sub
83 84
        return None

85
    @need_login
86
    def iter_history(self, sub):
87
        transactions = []
88
        self.logger.debug('call Browser.iter_history')
Kitof's avatar
Kitof committed
89 90 91 92
        self.paymentsp.stay_or_go()
        payments_url = self.page.get_last_payments_url()
        self.location(payments_url)
        assert self.lastpaymentsp.is_here()
93 94 95
        urls = self.page.iter_last_payments()
        for url in urls:
            self.location(url)
96
            assert self.paymentdetailsp.is_here()
97
            for payment in self.page.iter_payment_details(sub):
98 99 100 101 102 103 104 105
                transactions.append(payment)

        # go to a page with a "deconnexion" link so that logged property
        # stays True and next call to do_login doesn't crash when using the
        # blackbox
        self.accountp.go()

        return transactions
106

107
    @need_login
108 109
    def iter_documents(self, sub):
        self.logger.debug('call Browser.iter_documents')
Kitof's avatar
Kitof committed
110 111 112 113 114 115
        self.paymentsp.stay_or_go()
        payments_url = self.page.get_last_payments_url()
        self.location(payments_url)
        assert self.lastpaymentsp.is_here()
        for document in self.page.iter_documents(sub):
            yield document
116

117
    @need_login
118 119
    def get_document(self, id):
        self.logger.debug('call Browser.get_document')
120 121 122
        assert isinstance(id, basestring)
        subs = self.iter_subscription_list()
        for sub in subs:
123
            for b in self.iter_documents(sub):
124 125
                if id == b.id:
                    return b
126
        return False