module.py 2.98 KB
Newer Older
1 2
# -*- coding: utf-8 -*-

3
# Copyright(C) 2013-2015      Christophe Lampin
4
#
5
# This file is part of a weboob module.
6
#
7
# This weboob module is free software: you can redistribute it and/or modify
8 9 10 11
# 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.
#
12
# This weboob module is distributed in the hope that it will be useful,
13 14 15 16 17
# 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
18
# along with this weboob module. If not, see <http://www.gnu.org/licenses/>.
19

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

22
from weboob.capabilities.bill import DocumentTypes, CapDocument, SubscriptionNotFound, DocumentNotFound, Subscription, Bill
23
from weboob.tools.backend import Module, BackendConfig
24 25 26
from weboob.tools.value import ValueBackendPassword
from .browser import AmeliBrowser

Florent Fourcot's avatar
Florent Fourcot committed
27
__all__ = ['AmeliModule']
28 29


30
class AmeliModule(Module, CapDocument):
31
    NAME = 'ameli'
Nicolas Gattolin's avatar
Nicolas Gattolin committed
32 33
    DESCRIPTION = 'Ameli website: French Health Insurance'
    MAINTAINER = 'Christophe Lampin'
34
    EMAIL = 'weboob@lampin.net'
Romain Bignon's avatar
Romain Bignon committed
35
    VERSION = '1.6'
36 37
    LICENSE = 'AGPLv3+'
    BROWSER = AmeliBrowser
38 39
    CONFIG = BackendConfig(ValueBackendPassword('login', label='Numero de SS', regexp=r'^\d{13}$', masked=False),
                           ValueBackendPassword('password', label='Password', masked=True))
40

41 42
    accepted_document_types = (DocumentTypes.BILL,)

43 44 45 46 47 48 49 50
    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):
51
        subscription = self.browser.get_subscription(_id)
52 53 54 55 56
        if not subscription:
            raise SubscriptionNotFound()
        else:
            return subscription

57
    def iter_documents_history(self, subscription):
58 59
        if not isinstance(subscription, Subscription):
            subscription = self.get_subscription(subscription)
60
        return self.browser.iter_history(subscription)
61

62
    def iter_documents(self, subscription):
63 64
        if not isinstance(subscription, Subscription):
            subscription = self.get_subscription(subscription)
65
        return self.browser.iter_documents(subscription)
66

67 68
    def get_document(self, id):
        bill = self.browser.get_document(id)
69
        if not bill:
70
            raise DocumentNotFound()
71 72 73
        else:
            return bill

74
    def download_document(self, bill):
75
        if not isinstance(bill, Bill):
76
            bill = self.get_document(bill)
Nicolas Gattolin's avatar
Nicolas Gattolin committed
77 78 79 80
        response = self.browser.open(bill.url, stream=True)
        if not response or response.headers['content-type'] != "application/pdf":
            return None
        return response.content