« Clean out old items from your Downloads folder using find & crontab | Main | Troubleshooting: Cisco 877 on Eircom ADSL2+ »
Sunday
Mar132011

Ruby Script: Remaining Mobile BB Data Allowance

I recently changed to Three.ie as my mobile broadband provider. They provide a nice handy way of checking remaining monthly data allowance via their "my3.three.ie" service. Signing in is handled automatically on their side based on the network you're using. Handy!

Since I've been messing around with Ruby a bit I figured I'd try and make a ruby script that would check my remaining data for me! My efforts are below!

Please comment on any improvements that you think I can make. Especially relating to the instance vs. class variables!

I use this script in combination with a bash script to display data on my desktop using GeekTool. The bash script attempts to ping the mobile gateway's IP address. If it is available, the ruby script is executed. Simples!

 

 

#!/bin/bash
#
#	Pings once to see if my Huawei B260a mobile gateway is reachable
#	if yes, execute Ruby data allowance checker
#	else, exit
#
ping -c 1 -t 2 192.168.4.1 >/dev/null
if [[ $? == 0 ]]; then
	ruby ~/Programming/Ruby/MyStuff/threeMobileBroadband.rb
else
	exit
fi

 

 

#!/usr/bin/env ruby
#
require 'rubygems'
require 'mechanize'

class ThreeMobileBroadband

@@baseURL = "https://my3.three.ie"
  
def login
  @agent = Mechanize.new

  # First Single Sign On page
  @landing1 = @agent.get 'https://my3.three.ie/myaccount/postPayFreeUnits.do'
  @landing2 = @agent.get @landing1.links[0].uri
  # Then click the continue button presented to get to account options section
  @@usagePage = @agent.post("#{@@baseURL}#{@landing2.forms[0].action}")
end

def getRemainingData
  # Parse for remaining data table
  @usageValue =  @@usagePage.parser.xpath('//div/div/div/div/table/tbody/tr/td').to_s
  # Get the data, and sub out HTML, then sub out whitespace, then split into array at a close bracket
  @data = @usageValue.gsub(/<\/?[^>]*>/, "").gsub(/\s+/, "").split(/\)/)
  return @data[1]#.inspect
end

def remainingDataAsOf
  # Parse for caption containing the date & time the remaining data was checked
  @asOfDate = @@usagePage.parser.xpath('//div/div/div/caption/span').to_s.gsub(/<\/?[^>]*>/, "").gsub(/\*\s+/, "")
  return @asOfDate
end

end

three = ThreeMobileBroadband.new
three.login

puts "3 Mobile Broadband Pro"
puts "-----------"
puts "#{three.getRemainingData} MB remaining\n"
print "#{three.remainingDataAsOf}\n"
puts "-----------"

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
All HTML will be escaped. Hyperlinks will be created for URLs automatically.