From 1706f87451ac00e8ba89d5b23a9287a80c233ca0 Mon Sep 17 00:00:00 2001 From: Florian Duguet Date: Thu, 20 Dec 2018 15:31:56 +0100 Subject: [PATCH] [edf] fix iter_documents change the way we get subscription to retrieve all documents this will change a little bit subscription.id value but allow us to match correclty documents to the good subscription and retrieve documents from old subscriptions --- modules/edf/pro/browser.py | 62 ++++++++++++++++++++------------------ modules/edf/pro/pages.py | 52 ++++++++++++++++++++++++++------ 2 files changed, 75 insertions(+), 39 deletions(-) diff --git a/modules/edf/pro/browser.py b/modules/edf/pro/browser.py index 5733bb405c..4ac3843d74 100644 --- a/modules/edf/pro/browser.py +++ b/modules/edf/pro/browser.py @@ -19,8 +19,6 @@ from __future__ import unicode_literals -import json - from datetime import datetime, timedelta from weboob.browser import LoginBrowser, URL, need_login @@ -30,7 +28,7 @@ from .pages import ( LoginPage, HomePage, AuthPage, ErrorPage, LireSitePage, - SubscriptionsPage, BillsPage, DocumentsPage, ProfilePage, + SubscriptionsPage, SubscriptionsAccountPage, BillsPage, DocumentsPage, ProfilePage, ) @@ -43,15 +41,14 @@ class EdfproBrowser(LoginBrowser): error = URL(r'/page_erreur/', ErrorPage) home = URL('/ice/content/ice-pmse/homepage.html', HomePage) liresite = URL(r'/rest/homepagemp/liresite', LireSitePage) - contracts = URL('/rest/contratmp/consultercontrats', SubscriptionsPage) + subscriptions = URL('/rest/homepagemp/lireprofilsfacturation', SubscriptionsPage) + contracts = URL('/rest/contratmp/consultercontrats', SubscriptionsAccountPage) bills = URL('/rest/facturemp/getnomtelechargerfacture', BillsPage) documents = URL('/rest/facturemp/recherchefacture', DocumentsPage) profile = URL('/rest/servicemp/consulterinterlocuteur', ProfilePage) def __init__(self, config, *args, **kwargs): self.config = config - self.cache = {} - self.cache['docs'] = {} kwargs['username'] = self.config['login'].get() kwargs['password'] = self.config['password'].get() super(EdfproBrowser, self).__init__(*args, **kwargs) @@ -60,7 +57,7 @@ def do_login(self): self.login.go('/openam/json/authenticate', method='POST') login_data = self.page.get_data(self.username, self.password) try: - self.login.go(data=json.dumps(login_data), headers={'Content-Type': 'application/json'}) + self.login.go(json=login_data) except ClientError as e: raise BrowserIncorrectPassword(e.response.json()['message']) @@ -78,41 +75,48 @@ def do_login(self): @need_login def get_subscription_list(self): - self.liresite.go(data=json.dumps({"numPremierSitePage": 0, "pageSize": 100000, "idTdg": None, - "critereFiltre": [], "critereTri": []})) + self.liresite.go(json={"numPremierSitePage": 0, "pageSize": 100000, "idTdg": None, + "critereFiltre": [], "critereTri": []}) id_site_list = self.page.get_id_site_list() if not id_site_list: raise ActionNeeded("Vous ne disposez d'aucun contrat actif relatif à vos sites") - if "subs" not in self.cache.keys(): - self.contracts.go(data=json.dumps({'refDevisOMList': [], 'refDevisOHList': id_site_list})) + data = { + 'critereFiltre': [], + 'critereTri': [], + 'idTdg': None, + 'pageSize': 100000, + 'startRowNum': 0 + } + + sub_page = self.subscriptions.go(json=data) + self.contracts.go(json={'refDevisOMList': [], 'refDevisOHList': id_site_list}) - self.cache['subs'] = [s for s in self.page.get_subscriptions()] - return self.cache['subs'] + for sub in sub_page.get_subscriptions(): + self.page.update_subscription(sub) + yield sub @need_login def iter_documents(self, subscription): - if subscription.id not in self.cache['docs']: - try: - self.documents.go(data=json.dumps({ - 'dateDebut': (datetime.now() - timedelta(weeks=156)).strftime('%d/%m/%Y'), - 'dateFin': datetime.now().strftime('%d/%m/%Y'), - 'element': subscription._refdevis, - 'typeElementListe': 'CONTRAT' - })) - - self.cache['docs'][subscription.id] = [d for d in self.page.get_documents()] - except ServerError: - self.cache['docs'][subscription.id] = [] - return self.cache['docs'][subscription.id] + try: + self.documents.go(json={ + 'dateDebut': (datetime.now() - timedelta(weeks=156)).strftime('%d/%m/%Y'), + 'dateFin': datetime.now().strftime('%d/%m/%Y'), + 'element': subscription._account_id, + 'typeElementListe': 'ID_FELIX' + }) + + return self.page.get_documents() + except ServerError: + return [] @need_login def download_document(self, document): if document.url is not NotAvailable: try: - self.bills.go(data=json.dumps({'date': int(document.date.strftime('%s')), - 'iDFelix': document._account_billing, - 'numFacture': document._bill_number})) + self.bills.go(json={'date': int(document.date.strftime('%s')), + 'iDFelix': document._account_billing, + 'numFacture': document._bill_number}) return self.open('%s/rest/facturemp/telechargerfichier?fname=%s' % ( self.BASEURL, self.page.get_bill_name())).content diff --git a/modules/edf/pro/pages.py b/modules/edf/pro/pages.py index e2340d1572..fee4a1e398 100644 --- a/modules/edf/pro/pages.py +++ b/modules/edf/pro/pages.py @@ -24,7 +24,6 @@ from weboob.browser.pages import JsonPage, HTMLPage, RawPage, LoggedPage from weboob.browser.elements import DictElement, ItemElement, method from weboob.browser.filters.standard import CleanDecimal, CleanText -from weboob.browser.filters.html import CleanHTML from weboob.browser.filters.json import Dict from weboob.capabilities.bill import DocumentTypes, Subscription, Bill from weboob.exceptions import ActionNeeded @@ -65,21 +64,54 @@ def get_id_site_list(self): return [site['idSite'] for site in self.doc['site']] -class SubscriptionsPage(LoggedPage, JsonCguPage): +class SubscriptionsPage(LoggedPage, JsonPage): @method class get_subscriptions(DictElement): - item_xpath = 'listeContrat' + item_xpath = 'profilFacturation' class item(ItemElement): klass = Subscription - obj_id = CleanText(Dict('refDevisLabel')) - obj__refdevis = CleanText(Dict('refDevis')) - obj_label = CleanText(CleanHTML(Dict('nomOffreModele'))) - - def obj_subscriber(self): - return ('%s %s' % (Dict('prenomIntPrinc')(self).lower(), - Dict('nomIntPrinc')(self).lower())).title() + obj_id = CleanText(Dict('idPFLabel')) + obj__account_id = CleanText(Dict('idCompteDeFacturation')) + + +class SubscriptionsAccountPage(LoggedPage, JsonCguPage): + @classmethod + def _get_similarity_among_id(cls, sub_id, account_id): + """ + sometimes there are several sub_id and several account_id + sub_id looks like 1-UD8Z6FPO + and account_id 1-UD8Z6F7S + when a sub_id and an account_id are related their id are not completely identical but close + this function count numbers of char that are identical from the beginning until one char is different + more the count value is high more there is a chance that both id are from related objects (subscription and account) + """ + _, sub_id_value = sub_id.split('-', 1) + _, account_id_value = account_id.split('-', 1) + + count = 0 + for idx, c in enumerate(sub_id_value): + if idx >= len(account_id_value): + return count + if account_id_value[idx] != c: + return count + count += 1 + + return count + + def update_subscription(self, subscription): + good_con = None + best_matching = 0 + for con in self.doc['listeContrat']: + matching = self._get_similarity_among_id(subscription.id, con['refDevisLabel']) + if matching > best_matching: + best_matching = matching + good_con = con + + if good_con: + subscription.label = good_con['nomOffreModele'] + subscription.subscriber = (good_con['prenomIntPrinc'] + ' ' + good_con['nomIntPrinc']).title() class BillsPage(LoggedPage, JsonPage): -- GitLab