From 8cdcb9d18b7cf2bfa1fbfc30de6189c341c4f8c6 Mon Sep 17 00:00:00 2001 From: Thibault Douge Date: Fri, 30 Oct 2020 15:51:07 +0100 Subject: [PATCH] [myfoncia] get new documents, management report --- modules/myfoncia/browser.py | 16 ++++++++++--- modules/myfoncia/module.py | 47 ++++++++++++++++++------------------- modules/myfoncia/pages.py | 25 +++++++++++++++++--- 3 files changed, 58 insertions(+), 30 deletions(-) diff --git a/modules/myfoncia/browser.py b/modules/myfoncia/browser.py index 3c6f029d83..6bdaf17054 100644 --- a/modules/myfoncia/browser.py +++ b/modules/myfoncia/browser.py @@ -23,7 +23,7 @@ from weboob.browser import LoginBrowser, need_login, URL from weboob.exceptions import BrowserIncorrectPassword -from .pages import LoginPage, MonBienPage, MesChargesPage +from .pages import LoginPage, MonBienPage, MesChargesPage, DocumentsPage class MyFonciaBrowser(LoginBrowser): @@ -32,6 +32,7 @@ class MyFonciaBrowser(LoginBrowser): login = URL(r'/login', LoginPage) monBien = URL(r'/espace-client/espace-de-gestion/mon-bien', MonBienPage) mesCharges = URL(r'/espace-client/espace-de-gestion/mes-charges/(?P.+)', MesChargesPage) + documents = URL(r'/espace-client/espace-de-gestion/mes-documents/(?P.+)/(?P[A-Z])', DocumentsPage) def do_login(self): self.login.stay_or_go().do_login(self.username, self.password) @@ -45,5 +46,14 @@ def get_subscriptions(self): return self.monBien.stay_or_go().get_subscriptions() @need_login - def get_documents(self, subscription): - return self.mesCharges.stay_or_go(subscription=subscription).get_documents() + def get_documents(self, subscription_id): + # the last char of subscription_id is a letter, we need this to put this at the end of the url + if not subscription_id[-1:].isupper(): + self.logger.debug('The last char of subscription id is not an uppercase') + self.documents.go(subscription=subscription_id, letter=subscription_id[-1:]) + for doc in self.page.iter_documents(): + yield doc + + self.mesCharges.go(subscription=subscription_id) + for bill in self.page.get_documents(): + yield bill diff --git a/modules/myfoncia/module.py b/modules/myfoncia/module.py index bcc4476c6d..a86f9d362f 100644 --- a/modules/myfoncia/module.py +++ b/modules/myfoncia/module.py @@ -22,9 +22,11 @@ from weboob.tools.backend import Module, BackendConfig from weboob.capabilities.base import find_object -from weboob.capabilities.bill import (CapDocument, Bill, DocumentNotFound, - Subscription) -from weboob.tools.value import Value, ValueBackendPassword +from weboob.capabilities.bill import ( + CapDocument, DocumentNotFound, + Subscription, DocumentTypes, Document, +) +from weboob.tools.value import ValueBackendPassword from .browser import MyFonciaBrowser @@ -40,20 +42,18 @@ class MyFonciaModule(Module, CapDocument): LICENSE = 'LGPLv3+' VERSION = '2.1' CONFIG = BackendConfig( - Value( - 'login', - label='Email address or Foncia ID' - ), - ValueBackendPassword( - 'password', - label='Password' - ) + ValueBackendPassword('login', label='Email address or Foncia ID'), + ValueBackendPassword('password', label='Password'), ) BROWSER = MyFonciaBrowser + accepted_document_types = (DocumentTypes.BILL, DocumentTypes.REPORT,) + def create_default_browser(self): - return self.create_browser(self.config['login'].get(), - self.config['password'].get()) + return self.create_browser( + self.config['login'].get(), + self.config['password'].get() + ) def iter_subscription(self): return self.browser.get_subscriptions() @@ -65,18 +65,17 @@ def iter_documents(self, subscription): subscription_id = subscription return self.browser.get_documents(subscription_id) - def get_document(self, bill): - return find_object( - self.iter_documents(bill.split("#")[0]), - id=bill, - error=DocumentNotFound - ) + def get_document(self, _id): + subid = _id.rsplit('_', 1)[0] + subscription = self.get_subscription(subid) + + return find_object(self.iter_documents(subscription), id=_id, error=DocumentNotFound) - def download_document(self, bill): - if not isinstance(bill, Bill): - bill = self.get_document(bill) + def download_document(self, document): + if not isinstance(document, Document): + document = self.get_document(document) - if not bill.url: + if not document.url: return None - return self.browser.open(bill.url).content + return self.browser.open(document.url).content diff --git a/modules/myfoncia/pages.py b/modules/myfoncia/pages.py index d353238c98..9246ca4264 100644 --- a/modules/myfoncia/pages.py +++ b/modules/myfoncia/pages.py @@ -21,10 +21,10 @@ from weboob.browser.pages import HTMLPage, LoggedPage from weboob.browser.filters.standard import CleanDecimal, CleanText, Date, Env, Format -from weboob.browser.filters.html import Attr, Link, XPathNotFound +from weboob.browser.filters.html import Attr, Link, XPathNotFound, AbsoluteLink from weboob.browser.elements import ItemElement, ListElement, method from weboob.capabilities.base import NotAvailable -from weboob.capabilities.bill import Bill, Subscription +from weboob.capabilities.bill import Bill, Subscription, Document, DocumentTypes from weboob.tools.compat import urljoin @@ -72,7 +72,7 @@ class item(ItemElement): klass = Bill obj_id = Format( - '%s#%s', + '%s_%s', Env('subscription'), Attr('.', 'id') ) @@ -100,3 +100,22 @@ def obj_url(self): ) except XPathNotFound: return NotAvailable + + +class DocumentsPage(LoggedPage, HTMLPage): + @method + class iter_documents(ListElement): + item_xpath = '//main[@role="main"]//article' + + class item(ItemElement): + klass = Document + + def condition(self): + return CleanText('.//p[@data-behat="descOfUtilityRecord"]')(self) == 'CRG' + + obj_id = Format('%s_%s', Attr('.', 'id'), Env('subscription')) + obj_date = Date(CleanText('.//p[@data-behat="dateOfUtilityRecord"]'), dayfirst=True) + obj_label = CleanText('.//p[@data-behat="descOfUtilityRecord"]') + obj_url = AbsoluteLink('.//a[@class="Download"]') + obj_format = 'pdf' + obj_type = DocumentTypes.REPORT -- GitLab