#!/usr/bin/perl -T -w # # $Id: pacific-quota-check,v 1.1 2004/06/19 11:50:37 suter Exp $ # Copyright (C) 2004 Mark Suter # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # [MJS 26 Feb 2004] WWW::Mechanize script against Pacific Internet's Highlander # [MJS 24 May 2004] Convert into a CGI script after feedback from Nathan Bocskay use strict; use IO::File; use WWW::Mechanize; ## Use CGI module safely use CGI qw(-no_xhtml -nosticky -newstyle_urls); $CGI::DISABLE_UPLOADS = 1; # no uploads $CGI::POST_MAX = 1024; # max 1K POSTs ## Unbuffer STDOUT $| = 1; ## Initialise CGI object my $q = new CGI; ## Start the page print $q->header(-expires => "+1h"), $q->start_html(-title => "Pacific Quota Check", -author => 'suter@humbug.org.au', -style => '.alert { color : red; background : white; font-weight : bold; border: thin solid red; }'), $q->h1("Pacific Quota Check"), "\n"; ## Catch any problems eval { ## Get the authentication details from the POST my $user = $q->param("user") or die "Username missing!\n"; my $pass = $q->param("pass") or die "Password missing!\n"; ## Let the user know what's happening print $q->p("Retrieving user $user\'s traffic details", 'from Pacific Internet\'s', 'Highlander system.'), "\n"; # Ready the UserAgent for use my $ua = WWW::Mechanize->new(autocheck => 1, keep_alive => 32); $ua->agent("pacific-quota-check/" . (qw$Revision: 1.1 $)[-1] . " " . $ua->agent); ## Login to Highlander $ua->get('https://www.pacific.net.au/highlander/'); $ua->set_visible($user, "pacific.net.au", $pass); $ua->submit(); ## Look at the Traffic Usage ... $ua->follow_link(text => "Show Usage"); ## ... for the previous period and the current period foreach my $period (1, 0) { ## Ask for a specific period (X billing cycles back) $ua->submit_form(fields => { past => $period, mode => "tabular" }); $ua->follow_link(url_regex => qr/zoom=period/); ## Extract all the rows from the table my @data = $ua->content =~ m{ .*? # start of row .*? (\d{4}-\d{2}-\d{2}) .*? .*? # Date ([-.,\d]+?) .*? # Upload ([-.,\d]+?) .*? # Download # end of row }gimsx; ## Munge each row and keep totals my @rows = ([ "", "Upload", "Download" ]); my ($total_up, $total_down) = (0, 0); while (my ($date, $up, $down) = map { tr/,//d; $_ eq "-" ? 0 : $_ } splice @data, 0, 3) { push @rows, [ $date, $up, $down ]; $total_up += $up; $total_down += $down; } push @rows, [ "Totals", $total_up, $total_down ]; ## Display the data (taking advantage of the CGI module) print $q->table({ -summary => "date, upload, download" }, $q->Tr([ map { $q->td($_) } @rows ])), "\n"; } ## Logout of Highlander $ua->follow_link(text => "Logout"); }; $@ and print $q->p({class=>"alert"}, "There was a problem: $@"), "\n"; ## End the webpage print $q->end_html, "\n";