From d56303d2e9b9ee1cb76b21e72c2f743083ba2dea Mon Sep 17 00:00:00 2001 From: Damien Mat Date: Wed, 15 Apr 2020 18:12:42 +0200 Subject: [PATCH] [cragr] Switch account_space only when needed A number of iter_ functions use go_to_account_space() function in this module. Sometimes using it with argument '0' is enough and avoids trying to switch space when not needed, especially when the next request is retried up to 6 times upon response code 500. This commit extends that use of the function to all accounts where no switch is needed. This saves on requests number and avoids error 500 for users with only one space. --- modules/cragr/browser.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/modules/cragr/browser.py b/modules/cragr/browser.py index 4c5c80105b..58bd6e4832 100644 --- a/modules/cragr/browser.py +++ b/modules/cragr/browser.py @@ -187,6 +187,7 @@ def __init__(self, website, *args, **kwargs): super(CreditAgricoleBrowser, self).__init__(*args, **kwargs) self.website = website self.accounts_url = None + self.total_spaces = None # Netfinca browser: self.weboob = kwargs.pop('weboob') @@ -341,10 +342,11 @@ def check_space_connection(self, contract): @need_login def iter_spaces(self): - # Determine how many spaces are present on the connection - total_spaces = self.page.count_spaces() - self.logger.info('The total number of spaces on this connection is %s.', total_spaces) - for contract in range(total_spaces): + if not self.total_spaces: + # Determine how many spaces are present on the connection + self.total_spaces = self.page.count_spaces() + self.logger.info('The total number of spaces on this connection is %s.', self.total_spaces) + for contract in range(self.total_spaces): # Switch to another space if not self.check_space_connection(contract): self.logger.warning( @@ -567,6 +569,13 @@ def switch_account_to_per(self, account): @need_login def go_to_account_space(self, contract): + if self.total_spaces == 1: + self.location(self.accounts_url) + if not self.accounts_page.is_here(): + self.logger.warning('We have been loggged out, relogin.') + self.do_login() + return + # This request often returns a 500 error on this quality website for tries in range(4): try: -- GitLab