Skip to content

Instantly share code, notes, and snippets.

@bencollerson
Created September 28, 2012 12:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bencollerson/3799421 to your computer and use it in GitHub Desktop.
Save bencollerson/3799421 to your computer and use it in GitHub Desktop.
Scrape usage from the iinet toolbox site. (iinet is an Australian ISP)
#!/usr/bin/env perl
# iiscraper.pl
#
# This script gets your login details from your ~/.netrc file:
#
# machine iinet.net.au login <iinet-username> password <iinet-password>
#
# From there set up a crontab like this for daily updates:
#
# 0 7 * * * $HOME/bin/iiscraper.pl | mailx -s 'iinet usage' <your email>
#
###############################################################################
use Modern::Perl;
use HTML::TreeBuilder::XPath;
use WWW::Mechanize;
use Net::Netrc;
my $SITE = 'iinet.net.au';
my $LOGIN_URL = 'https://toolbox.iinet.net.au';
my $MOBILE_DATA_URL = 'https://toolbox.iinet.net.au/cgi-bin/new/showmvsummary.cgi';
my $mech = WWW::Mechanize->new();
my $usage = {};
login($mech, $LOGIN_URL, $SITE) or die "unable to login to iinet toolbox";
adsl_usage($mech, $LOGIN_URL, $usage);
mobile_usage($mech, $MOBILE_DATA_URL, $usage);
output($usage);
sub login {
my ($mech, $url, $site) = @_;
# get login details from .netrc
my $login = Net::Netrc->lookup($site);
$mech->get($url);
$mech->submit_form(
form_name => 'form_toolbox_login',
fields => {
username => $login->login,
password => $login->password,
},
);
return $mech->success
}
sub adsl_usage {
my ($mech, $url, $usage) = @_;
my $tree = HTML::TreeBuilder::XPath->new;
$mech->get($url);
$tree->parse( $mech->content);
$usage->{adsl} = $tree->findvalue('//div[@class="usage_text"]');
}
sub mobile_usage {
my ($mech, $url, $usage) = @_;
my $tree = HTML::TreeBuilder::XPath->new;
$mech->get($url);
$tree->parse( $mech->content);
@$usage{qw(
data
free
roam
iinet
voice
)} = $tree->findvalues('//div[@class="usage_text"]');
}
sub output {
my ($usage) = @_;
# These fields are ordered according to my priorities
for my $field (qw(
voice
data
free
iinet
roam
adsl
)) {
# omit values that appear to be empty.
printf("%-6s%s\n", $field, $usage->{$field})
unless $usage->{$field} =~ /^\$?0\.00/;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment