Viewing your Net-Vírtua Consumption
September 6, 2008
Last month, I received an email saying that I had consumed 80% of my link quota 2 weeks earlier to end the month.
It’s boring to have to access the page at Net-Vírtua site to view your monthly consumption. To facilitate this work, I made a simple Python script:
#!/usr/bin/python
import urllib
import urllib2
import datetime
import re
class Virtua():
__virtua_url = "http://consumo.virtua.com.br/"
__virtua_action = "consumo.php"
def __init__(self):
pass
def __prettyfy(self,html):
separator = '\n ------------- ------ '
preformatted = separator + '\n'
preformatted += '| date | GB |'
preformatted += separator
regex_space = re.compile('\s+')
regex_date = re.compile('<TD CLASS="bg_azulT azul b onze" nowrap>')
regex_cons = re.compile('<TD CLASS="bg_cinza azulE onze">')
regex_cell = re.compile('</TD>')
# <TD CLASS="bg_cinza azulE onze">1.04</TD> --> GB received
# <TD CLASS="bg_cinza azulE onze">0.06</TD> --> GB sended
# <TD CLASS="bg_cinza azulE onze">1.10</TD> --> GB consumed
# <TD CLASS="bg_cinza azulE onze">1.10</TD> --> Total
# when count == 3, it's signifies the daily consum
count = 0
seq = 0
date = ''
cons = ''
# html is a log phrase (all text in one line, it's bad)
html_formatted = html.split('\n')
for line in html_formatted:
if re.findall(regex_date,line): # get date
date = re.sub(regex_date,'',line)
date = date[:-5]
date = re.sub(regex_space,'',date) # take off blank spaces
if re.findall(regex_cons,line): # get daily consum
count += 1
cons = re.sub(regex_cons,'',line)
cons = re.sub(regex_cell,'',cons)
cons = re.sub(regex_space,'',cons) # take off blank spaces
if ( count % 4 == 0 ): # completes a sequence (one seq == 4 TD's)
seq += 1
if (count == (3 + 4 * seq)): # daily consum
preformatted += '\n| %s | %s |' % (date,cons)
preformatted += separator
preformatted += '\n\nTotal: %s GB' % cons
return preformatted
def getConsum(self,mac="cc0044bbcc99",month="",year=""):
target = self.__virtua_url + self.__virtua_action
month = datetime.date.today().month
year = datetime.date.today().year
data = {
'macadd':mac,
'mes':month,
'ano':year
}
input = urllib.urlencode(data)
req = urllib2.Request(target,input)
res = urllib2.urlopen(req)
print self.__prettyfy(res.read())
if __name__ == "__main__":
Virtua().getConsum()







