#! /usr/bin/python2.6 # coding:utf-8 # # vodafone_fetch_csv.py -- Fetches billing data from mobile carrier Vodafone IT. # Written by Andreas Baumgartner # Licensed under CC-BY-NC # Last modified: 2010-03-01 # # TODO: # - login(): check if login was successful # - login(): add support for multiple phone numbers # - fetch_data(): raise an exception if something goes wrong (e.g. not logged in or data not available) import sys import time import mechanize class GetVodafoneBillingData(): global mech mech = mechanize.Browser() def login(self,username,password): """Performs a login on vodafone.it with the provided username and password.""" mech.open('http://www.vodafone.it/') mech.select_form(name='loginForm') mech['username'] = username mech['password'] = password response = mech.submit() def fetch_data(self,startdate,enddate): """Requests billing data of a specified date range and fetches them as CSV export.""" try: time.strptime(startdate, '%d/%m/%Y') time.strptime(enddate, '%d/%m/%Y') except ValueError: sys.stderr.write('Error: Please provide valid start and end dates.\n') sys.exit() mech.open('http://www.vodafone.it/190/trilogy/jsp/md.do?method=commit') mech.open('https://www.areaprivati.vodafone.it/190/jpbx/DettaglioChiamate.do?ls=1&channelId=-18046&programId=536881487&pageTypeId=9604&tk=9604%2Cc&ty_key=fdt_pri_analisi_dettaglio_costi_ric&ty_nocache=true') mech.open('https://www.areaprivati.vodafone.it/190/jpbx/mx/SommarioChiamateGN.do?dataInizio=%s&dataFine=%s' % (startdate, enddate)) mech.open('https://www.areaprivati.vodafone.it/190/jpbx/mx/SommarioChiamateCB.do?dataInizio=%s&dataFine=%s' % (startdate, enddate)) mech.open('https://www.areaprivati.vodafone.it/190/jpbx/mx/SommarioChiamate.do?dataInizio=%s&dataFine=%s' % (startdate, enddate)) response = mech.open('https://www.areaprivati.vodafone.it/190/jpbx/DownloadCSV.do', 'cat=1%2C2%2C3%2C4%2C10%2C15&callFromSummary=true') return response.read() def save_data(self,data,filename='vodafone_export.csv'): """Saves the data into a CSV file.""" try: f = file(filename, 'w') f.write(data) f.close() except IOError, io_exception: sys.stderr,write('Error: Could not write into file "%s"\n' % (io_exception.filename)) sys.exit() # EXAMPLE: vf = GetVodafoneBillingData() vf.login('mario.rossi', 'animale99') data = vf.fetch_data('01/01/2010', '31/01/2010') vf.save_data(data, 'mariorossi_gennaio_2010.csv')