Cingular scraper

Here's a script I wrote to scrape Cingular's Web site and download my cell phone usage data. It was my first experience using mechanize. It's a very nice framework.

The script takes your phone number and password for Cingular's Web site. It outputs a CSV file with the calls that have been made in your current billing cycle.

You can also checkout the script from the subversion repository.

require 'rubygems'
require 'mechanize'
require 'fastercsv'

CELL_NUMBER = ARGV[0]
PASSWORD = ARGV[1]
RUN_TIME = Time.now

agent = WWW::Mechanize.new
agent.get('https://www.wireless.att.com/olam/dashboardAction.olamexecute')

login = agent.page.form('loginActionForm')
login.wireless_num = CELL_NUMBER
login.pass = PASSWORD
agent.submit login

agent.submit agent.page.form('loginInProgressForm')
agent.click agent.page.links.text('View Details')

headers = (((agent.page / '#curRow') / 'tr').first / :th).map{|col| col.inner_text.strip}
data = ((agent.page / '#curRow') / 'tr')[1..-3].map {|row| (row / :td).map{|col| col.inner_text.strip}}

headers = headers[1..-1] + ['RUN TIME']
clean = data.map do |row| 
  time = Time.parse(row[0] + ' ' + row[1]).strftime('%Y-%m-%d %H:%M:%S')
  ([time] + row[2..-1] + [RUN_TIME.strftime('%Y-%m-%d %H:%M:%S')])
end

puts headers.to_csv
puts clean.map{|r| r.to_csv}.join('')

Trackback URL for this post:

https://www.attemptry.com/trackback/4