[Bump] MyThree Bandwidth Usage Widget

7 February, 2008 – 9:00 pm

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">.+?<a href='(.+?)'/m
  url = $1.gsub("amp;&amp;",oid)
  url = url.gsub("&amp;","&")
  page = agent.get url
end
 
# get rc3
if page.body.match /iframe src='(.+?)'/m
  url = $1.gsub("&amp;","&")
  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 /<td>\s+([\d\.]+)\s+<\/td>/m
    remain = $1
  end
  
  # date
  if e.to_s.match /rightPadding">(.+?)</m
    date = $1.gsub(/\n\t\s+/m,"")
  end
  
  print spend,"|",size,"|",remain,"|",date,"\n"
}

Share it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • Slashdot
  • Technorati
  • YahooMyWeb
  1. 12 Responses to “[Bump] MyThree Bandwidth Usage Widget”

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

    By Ted on Sep 26, 2007

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

    By Ted on Oct 28, 2007

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

    By Richard Clement on Nov 8, 2007

  5. 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.

    By Ted on Dec 4, 2007

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

    Enjoy

    By Tim on Feb 8, 2008

  7. 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.

    By Gabriel on Apr 3, 2008

  8. 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. <input id="mobile", type="text", size=10 value="041123456">

    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

    By Tim on Apr 3, 2008

  9. 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)

    By Paul on Apr 4, 2008

  10. 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).

    By Andrew on Jun 14, 2008

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

    By Andrew on Jun 14, 2008

  12. 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

    By Tim on Jun 14, 2008

  13. 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.

    By Andrew on Jun 21, 2008

Post a Comment

*
To prove that you're not a bot, enter this code
Anti-Spam Image