I’m the one that pays for the broadband and phone bill at my house. It’s part of the rent agreement with my parents, I can get a super-fast Internet connection - if I pay for it. When choosing broadband provider it was obvious that Virgin Media offered the best speed, and that it would be cheaper if I transferred the telephone line along with it. After all, it was only an extra £2 per month plus call usage. That shouldn’t really amount to much.
I seem to be the only person who doesn’t use the telephone - but everybody else does. So when the bill dropped into my inbox this month I had a little bit of a shock. I wanted to find out who my family were phoning. The online billing facility on the Virgin web site allows you to view which were the most expensive phone calls. This information is useful, but it only gives you a list of numbers. I could manually look up the numbers myself, but I figured there was an easier way of doing this.
The Hack
I’ve written a script which automatically logs onto the Virgin web site, downloads the latest billing information and looks up each number in my Google contacts returning a name - which is much more useful. Obviously if I don’t have that number in Google Contacts it isn’t able to translate the number into a name. I was able to extract some meaningful information from the results.
The Result
ogriffin@inspiron530:~/code/who-called-oz$ ruby who-called-oz.rb Summary: You called Bethany for 21:00 minutes on Fri 09 Oct at 18:08 this cost 2.06! You called Bethany for 25:00 minutes on Mon 12 Oct at 10:31 this cost 3.5! You called Grandma D for 25:00 minutes on Tue 13 Oct at 09:39 this cost 1.47! You called Bethany for 15:00 minutes on Tue 13 Oct at 11:50 this cost 2.13! You called Bethany for 12:00 minutes on Thu 15 Oct at 17:26 this cost 1.73! You called Bethany for 12:00 minutes on Thu 15 Oct at 17:40 this cost 1.73! You called Grandma & Grandpa for 11:00 minutes on Thu 22 Oct at 21:00 this cost 0.7!
You can see that my script has identified that my little sister is a very expensive individual to ring. I’ve informed my household that it makes more financial sense (to me at least) to ring her from their own mobiles!
The Code
The script is divided into 3 parts; downloading the billing information from Virgin Media, downloading a list of my Google contacts and a short piece of code which joins the two together.
Downloading billing information from Virgin Media
Looking on the Virgin Media web site it was quickly obvious that they didn’t have a publicly accessible API. If they had it would have made the task much easier. But I don’t expect they have much customer demand for this information. I had 2 options for downloading the data.
- Downloading a PDF statement, converting it into text and then extracting the billing information from the result
- Extracting the information directly from the webpage HTML
I discovered both methods would require authenticating with the web site and traversing the pages. My script would be posing as a web browser, and clicking on the relevant links. To do this I used the WWW::Mechanize Ruby module. Since I would be downloading the web page content to do this it made more sense to pursue option 2 and extract the data directly from the HTML.
For example the following code simulates clicking on the ‘Show Me This Bill’ link on the web page. It does this by submitting the ‘billSummaryForm’ <pre class="brush: ruby">form = get_form_by_id(page, ‘billSummaryForm’) form[‘billSummaryForm:_id39:0:_id66’]=’Show Me This Bill’ page = @agent.submit form</pre> Obviously the script will break horribly when Virgin Media change their web site
Reading the Google address book
Extracting the information from Google Contacts was extremely easy in comparison. There are plenty of Ruby libraries available which offer this, but I found it easier to use the Net::HTTP class - with some help from various web sites.
You can download the source code for my script here: