[Bump] MyThree Bandwidth Usage Widget

Download it ‘ere…

I currently have a mobile broadband account (HSDPA) provided by three.com.au, and after a hexy surcharge from the previous month when exceeding my limit, thought it would be prudent to build a dashboard widget that tracks my monthly bandwidth usage.

Building on a previous post that automates calls to a https website, I followed a nifty tutorial here to turn that perl script into a Mac OSX dashboard widget. I have since revised my original code and opted for a Ruby version of the script, as I found Ruby is easier to setup for a noob as opposed to Perl and its dependencies …

Since Mac OSX already comes with a suitable version of Ruby, the only pre-installation requirement for this widget is to install the relevant gem that I use to automate http. So from a console type and accept the defaults.
sudo gem install -y mechanize

picture-1.png

Not anticipating a huge readership that wants this magic explained, I will just post the source code instead for your own use. You can download the widget here. After you decompress the archive, you can look at the source code by right mouse clicking on the widget and show package contents.

The main difference between my code and the example code is that I’m not making a system call to the curl binary as per the following:
widget.system("/usr/bin/curl
http://www.example.com/dashboard.php?return=sales",
null).outputString;

Rather, I am making a call to the Perl Ruby binary, which calls the mech script I had created in the previous post as per the following:
/usr/bin/ruby ~/Library/Widgets/ThreeUsageMeter.wdgt/scripts/MyThreeV2.rb ' + mobile + ' ' + password + ' ' + pin;

I have also added some fields that retain their values (for configuration) for your mobile number, password and pin.

If you are keen to just look/run the Ruby code on its own without the dashboard widget feel free to copy it from below.

Let me know if you have any difficulties installing the widget …

#!/usr/bin/env ruby
#sigh, my very first Ruby script ... Will I ditch Perl ? 8^)
#sudo gem install -y mechanize

require 'rubygems'
require 'mechanize'

login = ARGV[0]
password = ARGV[1]
pin = ARGV[2]

url = "https://www.my.three.com.au/My3/jfn"

# init agent
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Windows IE 6'

# get first page
page = agent.get(url)

# login page
form = page.forms.find {|f| f.name == 'login'}
form.fields.find {|f| f.name == 'login'}.value = login
form.fields.find {|f| f.name == 'password'}.value = password
page = agent.submit(form, form.buttons.first)

# pin page
form = page.forms.find {|f| f.name == 'myForm'}
form.fields.find {|f| f.name == 'pin'}.value = pin
page = agent.submit(form, form.buttons.first)

# get rc1
if page.body.match /Mobiles on this account.+?src='(.+?)'/m
url = $1
oid = url.match /;(.+)?mfunc/m
oid = oid[1]
url = url.gsub("&","&")
page = agent.get url
end

# get rc2
if page.body.match /paddingText">.+? url = $1.gsub("amp;&",oid)
url = url.gsub("&","&")
page = agent.get url
end

# get rc3
if page.body.match /iframe src='(.+?)'/m
url = $1.gsub("&","&")
page = agent.get url
end

# spend data
if page.body.match /bill.+?TextRPHeadings">(.+?)<\/span>/m
spend = $1.gsub(/\n\t\s+/m,"")
end

# other data
results = page.body.scan(/span class="paddingText">\s+(Mobile Broadband.+?)<\/tr>/m)

results.each { |e|
# size
if e.to_s.match /\(([\w\d]+)\)/m
size = $1
end

# remain
if e.to_s.match / \s+([\d\.]+)\s+<\/td>/m
remain = $1
end

# date
if e.to_s.match /rightPadding">(.+?) date = $1.gsub(/\n\t\s+/m,"")
end

print spend,"|",size,"|",remain,"|",date,"\n"
}

Social tagging:

17 Responses to [Bump] MyThree Bandwidth Usage Widget

  1. Ted

    Looks like I need to convert this to a bigpond cable widget

  2. Ted

    Actually its been done, and I can’t be bothered.

  3. Richard Clement

    Please can you advise on how to install the other bits and bobs I need to make this widget work. Many Thanks…

  4. Ted

    Since Tim is busy elsewhere, try this on your Mac to install those two perl modules.
    Open Terminal.app and then do something like this:

    sudo perl -MCPAN -e shell

    Put your user password in. Perl will then sytep you through setting up MCPAN
    Once that is complete, re-run MCPAN and install the two modules like this:

    ted$ sudo perl -MCPAN -e shell
    Password:
    Terminal does not support AddHistory.

    cpan shell -- CPAN exploration and modules installation (v1.7601)
    ReadLine support available (try 'install Bundle::CPAN')

    cpan> install WWW::Mechanize Crypt::SSLeay
    CPAN: Storable loaded ok
    Going to read /Users/ted/.cpan/Metadata
    Database was generated on Wed, 14 Nov 2007 12:36:44 GMT
    CPAN: LWP::UserAgent loaded ok
    Fetching with LWP:

    A whole lot of stuff will display while it installs. Then it is done. Any problems you may have are beyond this comment.

  5. Tim

    I have since updated the script to use Ruby, which has easier pre-installation requirements …

    Enjoy

  6. Gabriel

    Installs beautifully, but what are the three magic boxes on the top right of the widget for? Sorry if this is a really dumb question.

  7. Tim

    Sorry not a dumb question :)

    From top to bottom
    - mobile number
    - password
    - pin

    And apologies, I haven’t figured out a way to make those changes persist. So to make it permanent for your installation:

    1. in finder go to Users//Library/Widgets
    2. right mouse click on ThreeUsageMeter.wdgt and click Show Package Contents
    3. edit the ThreeUsageMeter.html file and provide default values for mobile, password and pin inputs …
    e.g.

    Then you’ll have to add that modified widget to your dashboard again (double click on it)

    Maybe someone with more widget experience can help me out here :)

    Cheers
    Tim Koopmans

  8. Paul

    The ruby script fails for me. From the command line:

    ~ $/usr/bin/ruby ~/Library/Widgets/ThreeUsageMeter.wdgt/scripts/MyThreeV2.rb 0414xxxxxx xxxxx xxxxx
    /Users/paul/Library/Widgets/ThreeUsageMeter.wdgt/scripts/MyThreeV2.rb:28: undefined method `fields’ for nil:NilClass (NoMethodError)

  9. Andrew

    Hi,

    I’m having a (probably very simple) problem with installation.

    I open terminal and (immediately) type:
    sudo: gem: command not found
    then enter my password, but then I get the error:
    “sudo: gem: command not found”

    I’ve never used the command line before.. do I need to be in the correct directory to run this? (I don’t actually know how to move directories, either).

  10. Andrew

    Oh, I noticed I had an error in that last post.
    I actually type
    sudo gem install -y mechanize
    in the terminal

  11. Tim

    Hi Andrew, do you have Ruby? You can check with the -v argument and also which ruby from the command line in terminal to see if Ruby is installed.

    e.g.

    90kts:~ koops$ ruby -v
    ruby 1.8.6 (2007-09-24 patchlevel 111) [universal-darwin9.0]
    90kts:~ koops$ which ruby
    /usr/bin/ruby
    90kts:~ koops$

    Regards,
    Tim Koopmans

  12. Andrew

    Thanks a lot for your help..

    Here’s what I get when I type: ruby -v
    ruby 1.8.2 (2004-12-25) [universal-darwin8.0]

    and here’s what I get when I type: which ruby
    /usr/bin/ruby

    So I guess that means it’s installed?

    Not sure what to do now, though.

  13. drew

    hey
    thanks for the widget, its the only three one out there so thanks for the initiative :)

    however it does not work for me, as i have both my mobile phone and internet accounts under the one mythree login. is it possible to add a feature that allows you to choose which number you want to monitor?
    currently the widget just shows ‘nil, nil, nil’
    drew

  14. Finally I can use this now I have my three mobile broadband. It’s so cheap for 2GB!

  15. Andrew

    HI all, I am struggling with this widget…I entered in all the info in the boxes, but the refresh button doesn’t work, and it just reads nil nil nil. any help?? Is there a version 2 of this widget yet??

    thanks a lot.

  16. I’m writing a new version of this. It should handle accounts with multiple mobiles on it as well. It’s low pri so it won’t be ready tomorrow. Koops probably won’t update it since he has moved provider. I’ll update my blog when it is done.

  17. Tim

    Indeed! I’ve since moved to NextG land so Ted will have an appropriate replacement soon. At which point I’ll link directly to his blog for the widget.

    Hurry up Ted! ;-)