Commit 46824e07 authored by Florian Duguet's avatar Florian Duguet Committed by Vincent A

[orange] Handle pagination for ContractsPage

and avoid duplication of subscription
parent 42cb290a
......@@ -53,7 +53,7 @@ class OrangeBillBrowser(LoginBrowser, StatesMixin):
password_page = URL(r'', PasswordPage)
captcha_page = URL(r'', CaptchaPage)
contracts = URL(r'\?page=1&nbcontractsbypage=15', ContractsPage)
contracts = URL(r'', ContractsPage)
contracts_api = URL(r'\?filter=telco,security', ContractsApiPage)
subscriptions = URL(r'\?zonetype=bandeau&idPage=gt-home-page', SubscriptionsPage)
......@@ -158,13 +158,18 @@ class OrangeBillBrowser(LoginBrowser, StatesMixin):
# this only works when there are pro subs.
nb_sub = 0
subscription_id_list = []
for sub in self.contracts.go().iter_subscriptions():
params = {
'page': 1,
'nbcontractsbypage': 15
for sub in
sub.subscriber =
yield sub
nb_sub =['totalContracts']
# assert pagination is not needed
assert nb_sub < 15
except ServerError:
......@@ -175,8 +180,10 @@ class OrangeBillBrowser(LoginBrowser, StatesMixin):
"X-Orange-Origin-ID": "ECQ",
for sub in self.contracts_api.go(headers=headers).iter_subscriptions():
nb_sub += 1
yield sub
# subscription returned here may be duplicated with the one returned by contracts page
if not in subscription_id_list:
nb_sub += 1
yield sub
except (ServerError, ClientError) as e:
# The orange website will return odd status codes when there are no subscriptions to return
# I've seen the 404, 500 and 503 response codes
......@@ -25,7 +25,7 @@ try:
except ImportError:
import HTMLParser
from weboob.browser.pages import HTMLPage, LoggedPage, JsonPage
from weboob.browser.pages import HTMLPage, LoggedPage, JsonPage, pagination
from weboob.capabilities.bill import Subscription
from weboob.browser.elements import DictElement, ListElement, ItemElement, method, TableElement
from weboob.browser.filters.standard import (
......@@ -39,7 +39,7 @@ from weboob.browser.filters.json import Dict
from weboob.capabilities.base import NotAvailable
from weboob.capabilities.bill import DocumentTypes, Bill
from import parse_french_date
from import urlencode
from import urlencode, urlparse, parse_qsl
class BillsApiProPage(LoggedPage, JsonPage):
......@@ -215,10 +215,26 @@ class SubscriptionsApiPage(LoggedPage, JsonPage):
class ContractsPage(LoggedPage, JsonPage):
class iter_subscriptions(DictElement):
item_xpath = 'contracts'
def next_page(self):
params = dict(parse_qsl(urlparse(
page_number = int(params['page'])
nbcontractsbypage = int(params['nbcontractsbypage'])
nb_subs = page_number * nbcontractsbypage
# sometimes totalContracts can be different from real quantity
# already seen totalContracts=39 with 38 contracts in json
# so we compare nb contracts received in this response with number per page to make sure we stop
# even if there is oneday totalContracts=7677657689 but just 8 contracts
doc =
if nb_subs < doc['totalContracts'] and len(doc['contracts']) == nbcontractsbypage:
params['page'] = page_number + 1
class item(ItemElement):
klass = Subscription
......@@ -249,7 +265,8 @@ class ContractsApiPage(LoggedPage, JsonPage):
CleanText(Dict('holder/firstName', default=""))(self),
CleanText(Dict('holder/lastName', default=""))(self),
assert any(names), "At least one name field should be populated. Has the page changed?"
if not any(names):
return NotAvailable
return ' '.join([n for n in names if n])
def obj__is_pro(self):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment