Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


26 posts

Geek


Topic # 58728 17-Mar-2010 23:07
Send private message

Wrote this a couple weeks ago and figured some here might be interested. It's a Python script that connects to the Telecom broadband usage meter website and reads your current usage. I've tested it on a few accounts and it's worked fine on them all.

(If the forum messes up the formatting heres the link: http://tom.henderson.net.nz/post/454031326/check-xtra-broadband-usage-using-python)

Hope someone finds it useful. 


[code]

#!/usr/local/bin/python

import sys, re
from mechanize import Browser
from BeautifulSoup import BeautifulSoup

# Optional: uncomment and hard code your account details here:
#myUsername = ''
#myPassword = ''

if len(sys.argv) == 3:
    username, password = sys.argv[1:]
else:
    try:
        username, password = myUsername, myPassword
    except:
        print "usage: %s username password" % sys.argv[0]
        sys.exit(1)

# Browser
br = Browser()

# Load the login page:
url = 'http://www.telecom.co.nz/broadband/usage' # 26.07kb
br.open(url)

# Select the first form:
br.select_form(nr=2)

# Fill in and submit:
br.form['USER'] = username
br.form['PASSWORD'] = password
br.submit()

# Navigate to the 'Current Usage Details' page:
req = br.click_link(text='Current Usage Details')
br.open(req)
html = br.response().read()

# Load html into BeautifulSoup.
# We need to adjust the line that contains ""
# as it confuses the html interpreter.
myMassage = [(re.compile(""), lambda match: '')]
soup = BeautifulSoup(html,  markupMassage=myMassage)

# Extract some data and print:
account = soup.findAll('span')[2].string
if len(account.split(' ')[2]) != 8:
    print 'Sorry, the account must be a landline.'
else:
    print '         Usage for:', account
    print 'Current usage plan:', soup.findAll('table')[3].findAll('td')[9].string.strip()
    print '                   ', soup.findAll('table')[3].findAll('td')[7].string
    print ''
    data = soup.findAll('table')[9].findAll('td')
    print '   Total Downloads:', data[1].string
    print '     Total Uploads:', data[3].string
    print '       Total Usage:', data[5].string
[/code]

Create new topic
78 posts

Master Geek


  Reply # 308556 17-Mar-2010 23:37
Send private message

Thanks! I might try to adapt it for another ISP or two. :)



26 posts

Geek


  Reply # 308564 17-Mar-2010 23:56
Send private message

I should have mentioned that you'll need to install mechanize and BeautifulSoup for this script to work. Mechanize handles the browser object (links / forms / cookies etc), and BeautifulSoup makes it easy to parse the html and pull out the data.

Charles, it should be easy enough to modify for other ISPs, as long as they use html and not flash / java etc. Post back if you get anything working.

4288 posts

Uber Geek
+1 received by user: 82

Moderator
Trusted
Lifetime subscriber

  Reply # 308575 18-Mar-2010 06:10
Send private message

So what does this give you, that TUC doesnt?



26 posts

Geek


  Reply # 308644 18-Mar-2010 10:25
Send private message

What do you mean by TUC?

All this does is let you get your usage from a command line. At work I have to keep an eye on all our broadband plans so I have a script that goes through them one by one and spits out the current usage. Makes it easy to keep an eye on them.

25 posts

Geek


  Reply # 308648 18-Mar-2010 10:38
Send private message

Lovely, thanks Tom.

A real pity XTRA don't have an API that you can adopt to make things easier.

4288 posts

Uber Geek
+1 received by user: 82

Moderator
Trusted
Lifetime subscriber

  Reply # 308668 18-Mar-2010 11:02
Send private message

tomhenderson: What do you mean by TUC?

All this does is let you get your usage from a command line. At work I have to keep an eye on all our broadband plans so I have a script that goes through them one by one and spits out the current usage. Makes it easy to keep an eye on them.


TUC, is traffic usage checker. Handy little app that sits in your taskbar. www.tuc.co.nz



26 posts

Geek


  Reply # 308681 18-Mar-2010 11:28
Send private message

Thats a neat little app. My machine is a mac so not an option for me though. I do use a 'Consume' which is a great iPhone app which works with xtra. You can load in all your accounts and it shows them all as progress bars.

2010 posts

Uber Geek
+1 received by user: 767

Trusted

  Reply # 309205 19-Mar-2010 16:23
Send private message

rscole86: So what does this give you, that TUC doesnt?


Access to the figures from a unix shell prompt? Thus making it scriptable?

Not really interesting for most people, but I think it's quite neat. Now, make it work with TCL please ;)

Tim



26 posts

Geek


  Reply # 429270 20-Jan-2011 17:37
Send private message

Updated this recently when telecom changed their system slightly. This version now cycles through every line listed on your account, doesn't break if you have a mobile plan listed, and prints usage from previous months.

Edit: Sorry guys, this forum won't seem to let me paste code with the formatting correct (no idea how I did it last time). Since this is python it won't run if the indentation is incorrect. The script at the link in my original post is updated to what I was trying to post here.

Create new topic

Twitter »

Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:



Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.

Alternatively, you can receive a daily email with Geekzone updates.