From 95cf812db3b9687f1ec082f9e13156847f002d75 Mon Sep 17 00:00:00 2001 From: Lucas Ficheux Date: Thu, 19 Sep 2019 18:23:21 +0200 Subject: [PATCH] [caissedepargne] Added document retrieval for connections without accounts The module would crash when adding connections without an account on the website, even if some documents were present. Now I create a fake Subscription object to bind all documents with. Closes : 12826@zendesk --- modules/caissedepargne/browser.py | 29 ++++++++++++++++++++++++++++- modules/caissedepargne/pages.py | 6 ++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/modules/caissedepargne/browser.py b/modules/caissedepargne/browser.py index 1ce662c340..d51ad41b5c 100644 --- a/modules/caissedepargne/browser.py +++ b/modules/caissedepargne/browser.py @@ -22,6 +22,7 @@ import re import datetime import json +from hashlib import sha256 from decimal import Decimal from dateutil import parser @@ -31,6 +32,7 @@ from weboob.browser.url import URL from weboob.capabilities.bank import Account, AddRecipientStep, Recipient, TransferBankError, Transaction, TransferStep from weboob.capabilities.base import NotAvailable, find_object +from weboob.capabilities.bill import Subscription from weboob.capabilities.profile import Profile from weboob.browser.exceptions import BrowserHTTPNotFound, ClientError, ServerError from weboob.exceptions import ( @@ -171,6 +173,7 @@ def __init__(self, nuser, *args, **kwargs): 'market_url', 'https://www.caisse-epargne.offrebourse.com', ) + self.has_subscription = True super(CaisseEpargne, self).__init__(*args, **kwargs) @@ -1088,6 +1091,10 @@ def new_recipient(self, recipient, **params): self.page.set_browser_form() raise AddRecipientStep(self.get_recipient_obj(recipient), Value('sms_password', label=self.page.get_prompt_text())) + def go_documents_without_sub(self): + self.home_tache.go(tache='CPTSYNT0') + assert self.subscription.is_here(), "Couldn't go to documents page" + @need_login def iter_subscription(self): self.home.go() @@ -1098,6 +1105,20 @@ def iter_subscription(self): if self.unavailable_page.is_here(): # some users don't have checking account self.home_tache.go(tache='EPASYNT0') + if self.garbage.is_here(): # User has no subscription, checking if they have documents, if so creating fake subscription + self.has_subscription = False + self.home_tache.go(tache='CPTSYNT0') + if not self.subscription.is_here(): # Looks like there is nothing to return + return [] + self.logger.warning("Couldn't find subscription, creating a fake one to return documents available") + + profile = self.get_profile() + + sub = Subscription() + sub.label = sub.subscriber = profile.name + sub.id = sha256(profile.name.lower().encode('utf-8')).hexdigest() + + return [sub] self.page.go_subscription() if not self.subscription.is_here(): # if user is not allowed to have subscription we are redirected to IndexPage @@ -1111,6 +1132,9 @@ def iter_subscription(self): @need_login def iter_documents(self, subscription): self.home.go() + if not self.has_subscription: + self.go_documents_without_sub() + return self.page.iter_documents(sub_id=subscription.id, has_subscription=self.has_subscription) self.home_tache.go(tache='CPTSYNT1') if self.unavailable_page.is_here(): # some users don't have checking account @@ -1118,11 +1142,14 @@ def iter_documents(self, subscription): self.page.go_subscription() assert self.subscription.is_here() - return self.page.iter_documents(sub_id=subscription.id) + return self.page.iter_documents(sub_id=subscription.id, has_subscription=self.has_subscription) @need_login def download_document(self, document): self.home.go() + if not self.has_subscription: + self.go_documents_without_sub() + return self.page.download_document(document).content self.home_tache.go(tache='CPTSYNT1') if self.unavailable_page.is_here(): # some users don't have checking account diff --git a/modules/caissedepargne/pages.py b/modules/caissedepargne/pages.py index 426a07b184..c7cf92201d 100644 --- a/modules/caissedepargne/pages.py +++ b/modules/caissedepargne/pages.py @@ -1857,14 +1857,16 @@ class iter_documents(ListElement): ignore_duplicate = True @property def item_xpath(self): - return '//h3[contains(text(), "%s")]//following-sibling::div[@class="panel"][1]/table/tbody/tr' % Env('sub_id')(self) + if Env('has_subscription')(self): + return '//h3[contains(text(), "%s")]//following-sibling::div[@class="panel"][1]/table/tbody/tr' % Env('sub_id')(self) + return '//div[@id="MM_CONSULTATION_RELEVES_COURRIERS_EDOCUMENTS_divRelevesCourriers"]/table/tbody/tr' class item(ItemElement): klass = Document obj_type = DocumentTypes.OTHER obj_format = 'pdf' - obj_url = Regexp(Link('.//td[@class="telecharger"]/a'), r'WebForm_PostBackOptions\("(\S*)"') + obj_url = Regexp(Link('.//td[@class="telecharger"]//a'), r'WebForm_PostBackOptions\("(\S*)"') obj_id = Format('%s_%s_%s', Env('sub_id'), CleanText('./td[2]', symbols='/', replace=[(' ', '_')]), Regexp(CleanText('./td[3]'), r'([\wé]*)')) obj_label = Format('%s %s', CleanText('./td[3]'), CleanText('./td[2]')) obj_date = Date(CleanText('./td[2]'), dayfirst=True) -- GitLab