Support multiple account for details operation

This commit is contained in:
Florent 2012-08-27 14:54:50 +02:00
commit adce6d0c83
4 changed files with 59 additions and 45 deletions

View file

@ -50,8 +50,7 @@ class FreeMobileBackend(BaseBackend, ICapBill):
self.config['password'].get()) self.config['password'].get())
def iter_subscription(self): def iter_subscription(self):
for subscription in self.browser.get_subscription_list(): return self.browser.get_subscription_list()
yield subscription
def get_subscription(self, _id): def get_subscription(self, _id):
if not _id.isdigit(): if not _id.isdigit():
@ -64,10 +63,14 @@ class FreeMobileBackend(BaseBackend, ICapBill):
raise SubscriptionNotFound() raise SubscriptionNotFound()
def iter_history(self, subscription): def iter_history(self, subscription):
if not isinstance(subscription, Subscription):
subscription = self.get_subscription(subscription)
with self.browser: with self.browser:
for history in self.browser.get_history(): for history in self.browser.get_history(subscription):
yield history yield history
def get_bill(self, id): def get_bill(self, id):
with self.browser: with self.browser:
bill = self.browser.get_bill(id) bill = self.browser.get_bill(id)
@ -76,6 +79,7 @@ class FreeMobileBackend(BaseBackend, ICapBill):
else: else:
raise BillNotFound() raise BillNotFound()
def iter_bills(self, subscription): def iter_bills(self, subscription):
if not isinstance(subscription, Subscription): if not isinstance(subscription, Subscription):
subscription = self.get_subscription(subscription) subscription = self.get_subscription(subscription)
@ -84,12 +88,16 @@ class FreeMobileBackend(BaseBackend, ICapBill):
for bill in self.browser.iter_bills(subscription.id): for bill in self.browser.iter_bills(subscription.id):
yield bill yield bill
# The subscription is actually useless, but maybe for the futur...
def get_details(self, subscription): def get_details(self, subscription):
if not isinstance(subscription, Subscription):
subscription = self.get_subscription(subscription)
with self.browser: with self.browser:
for detail in self.browser.get_details(): for detail in self.browser.get_details(subscription):
yield detail yield detail
def download_bill(self, bill): def download_bill(self, bill):
if not isinstance(bill, Bill): if not isinstance(bill, Bill):
bill = self.get_bill(bill) bill = self.get_bill(bill)

View file

@ -68,22 +68,21 @@ class Freemobile(BaseBrowser):
if not self.is_on_page(HomePage): if not self.is_on_page(HomePage):
self.location('/moncompte/index.php?page=home') self.location('/moncompte/index.php?page=home')
l = self.page.get_list() for a in self.page.get_list():
for a in l:
if a.id == id: if a.id == id:
return a return a
return None return None
def get_history(self): def get_history(self, subscription):
if not self.is_on_page(HistoryPage): if not self.is_on_page(HistoryPage):
self.location('/moncompte/ajax.php?page=consotel_current_month', 'login=' + self.username) self.location('/moncompte/ajax.php?page=consotel_current_month', 'login=' + self.username)
return self.page.get_calls() return self.page.get_calls(subscription)
def get_details(self): def get_details(self, subscription):
if not self.is_on_page(DetailsPage): if not self.is_on_page(DetailsPage):
self.location('/moncompte/index.php?page=suiviconso') self.location('/moncompte/index.php?page=suiviconso')
return self.page.get_details() return self.page.get_details(subscription)
def iter_bills(self, parentid): def iter_bills(self, parentid):
if not self.is_on_page(DetailsPage): if not self.is_on_page(DetailsPage):

View file

@ -22,7 +22,7 @@ from datetime import datetime, date, time
from decimal import Decimal from decimal import Decimal
from weboob.tools.browser import BasePage from weboob.tools.browser import BasePage
from weboob.capabilities.bill import Detail, Bill from weboob.capabilities.bill import Detail, Bill, Subscription
import re import re
@ -41,18 +41,25 @@ def convert_price(div):
class DetailsPage(BasePage): class DetailsPage(BasePage):
def on_loaded(self): def on_loaded(self):
self.details = [] self.details = {}
self.datebills = [] self.datebills = []
num = self.document.xpath('//div[@class="infosLigneDetail pointer"]')[0].text for div in self.document.xpath('//div[@class="infosLigneDetail pointer"]'):
num = num.split("-")[2].strip() phonenumber = div.text
phonenumber = phonenumber.split("-")[2].strip()
virtualnumber = div.attrib['onclick'].split('(')[1][1]
self.details['num' + str(phonenumber)] = virtualnumber
for div in self.document.xpath('//div[@class="infosConso"]'):
num = div.attrib['id'].split('_')[1][0]
self.details[num] = []
# National parsing # National parsing
divnat = self.document.xpath('//div[@class="national"]')[0] divnat = div.xpath('div[@class="national"]')[0]
self.parse_div(divnat, "National : %s | International : %s", False) self.parse_div(divnat, "National : %s | International : %s", num, False)
# International parsing # International parsing
divint = self.document.xpath('//div[@class="international hide"]')[0] divint = div.xpath('div[@class="international hide"]')[0]
self.parse_div(divint, u"Appels émis : %s | Appels reçus : %s", True) self.parse_div(divint, u"Appels émis : %s | Appels reçus : %s", num, True)
for trbill in self.document.xpath('//tr[@class="derniereFacture"]'): for trbill in self.document.xpath('//tr[@class="derniereFacture"]'):
mydate = unicode(trbill.find('td').text.split(":")[1].strip()) mydate = unicode(trbill.find('td').text.split(":")[1].strip())
@ -60,7 +67,7 @@ class DetailsPage(BasePage):
bill.label = unicode(mydate) bill.label = unicode(mydate)
billid = mydate.replace('-', '') billid = mydate.replace('-', '')
billid = billid[4:8] + billid[2:4] + billid[0:2] billid = billid[4:8] + billid[2:4] + billid[0:2]
bill.id = num + "." + billid bill.id = phonenumber + "." + billid
bill.date = date(*reversed([int(x) for x in mydate.split("-")])) bill.date = date(*reversed([int(x) for x in mydate.split("-")]))
alink = trbill.find('td/a') alink = trbill.find('td/a')
if alink.attrib.get("class") == "linkModal tips": if alink.attrib.get("class") == "linkModal tips":
@ -71,15 +78,15 @@ class DetailsPage(BasePage):
bill._url = alink.attrib.get('href') bill._url = alink.attrib.get('href')
self.datebills.append(bill) self.datebills.append(bill)
def parse_div(self, divglobal, string, inter=False): def parse_div(self, divglobal, string, num, inter=False):
divs = divglobal.xpath('div[@class="detail"]') divs = divglobal.xpath('div[@class="detail"]')
# Two informations in one div... # Two informations in one div...
div = divs.pop(0) div = divs.pop(0)
voice = self.parse_voice(div, string, inter) voice = self.parse_voice(div, string, inter)
self.details.append(voice) self.details[num].append(voice)
self.iter_divs(divs, inter) self.iter_divs(divs, num, inter)
def iter_divs(self, divs, inter=False): def iter_divs(self, divs, num, inter=False):
for div in divs: for div in divs:
detail = Detail() detail = Detail()
@ -89,7 +96,7 @@ class DetailsPage(BasePage):
detail.infos = unicode(div.find('div[@class="consoDetail"]/p').text_content().lstrip()) detail.infos = unicode(div.find('div[@class="consoDetail"]/p').text_content().lstrip())
detail.price = convert_price(div) detail.price = convert_price(div)
self.details.append(detail) self.details[num].append(detail)
def parse_voice(self, div, string, inter=False): def parse_voice(self, div, string, inter=False):
voice = Detail() voice = Detail()
@ -103,8 +110,10 @@ class DetailsPage(BasePage):
return voice return voice
def get_details(self): def get_details(self, subscription):
return self.details print self.details
num = self.details['num' + subscription.id]
return self.details[num]
def date_bills(self): def date_bills(self):
return self.datebills return self.datebills
@ -136,5 +145,5 @@ class HistoryPage(BasePage):
self.calls.append(detail) self.calls.append(detail)
def get_calls(self): def get_calls(self, subscription):
return sorted(self.calls, key=_get_date, reverse=True) return sorted(self.calls, key=_get_date, reverse=True)

View file

@ -30,18 +30,16 @@ class HomePage(BasePage):
def get_list(self): def get_list(self):
l = [] l = []
divabo = self.document.xpath('//div[@class="idAbonne pointer"]')[0] for divabo in self.document.xpath('//div[@class="idAbonne pointer"]'):
owner = unicode(divabo.xpath('p')[0].text.replace(' - ', '')) owner = unicode(divabo.xpath('p')[0].text.replace(' - ', ''))
phone = unicode(divabo.xpath('p/span')[0].text) phone = unicode(divabo.xpath('p/span')[0].text)
self.browser.logger.debug('Found ' + owner + ' has subscriber') self.browser.logger.debug('Found ' + owner + ' as subscriber')
self.browser.logger.debug('Found ' + phone + ' has phone number') self.browser.logger.debug('Found ' + phone + ' as phone number')
phoneplan = unicode(self.document.xpath('//div[@class="forfaitChoisi"]')[0].text.lstrip().rstrip()) phoneplan = unicode(self.document.xpath('//div[@class="forfaitChoisi"]')[0].text.lstrip().rstrip())
self.browser.logger.debug('Found ' + phoneplan + ' has subscription type') self.browser.logger.debug('Found ' + phoneplan + ' as subscription type')
subscription = Subscription(phone) subscription = Subscription(phone)
subscription.label = phone + ' - ' + phoneplan subscription.label = phone + ' - ' + phoneplan
subscription.subscriber = owner subscription.subscriber = owner
l.append(subscription) yield subscription
return l