#!/usr/bin/perl # itr2 -- by greenfly # a quick web scrubber that grabs and outputs stats from # www.internettrafficreport.com in a colorized format # This fits best in a 47x9 character monospace terminal use POSIX; # lets us use ceil $seconds = "470"; # number of seconds between refreshes $increment = $seconds / 47; # set to 47 dots before refresh $increment = ceil($increment); # set some colors $red = "\033[31m"; $green = "\033[32m"; $yellow = "\033[33m"; $col_brighten = "\033[01m"; $col_normal = "\033[0m"; while(1){ $at_stats = 0; # grab the latest stats from www.internettrafficreport.com # # if you don't have and don't want to install the LWP::UserAgent module # from CPAN, uncomment the following lines, and comment out the lines below # until you reach end grabbing html # # open ITR, "wget -q -O - http://www.internettrafficreport.com/main.htm |"; # @itr = ; # close ITR; # # for the sake of portability, here's the same thing as above # uncomment the following lines and comment out the above open to close: # use LWP::UserAgent; my $ua = new LWP::UserAgent; my $req = new HTTP::Request GET => "http://www.internettrafficreport.com/main.htm"; my $res = $ua->request($req); if($res->is_success) { $itr[0] = $res->content; } # end grabbing html # strip out all the html tags @itr = split /\n/, strip_tags(@itr); # parse through and print out the results for($i = 0; $i < $#itr; $i++) { $itr[$i] =~ s/^\t//g; $itr[$i] =~ s/\t$//g; if($itr[$i] eq "Continent\tCurrent Index\tAvg. Response Time \(ms\)\tAvg. Packet Loss \(\%\)") { $at_stats = 1; print " Internet Traffic Report\n"; print "Continent Index Response Time Packet Loss\n"; print "-" x 47 . "\n"; next; } if($at_stats) { ($country, $index, $response, $pktloss) = split /\t/, $itr[$i]; $country =~ s/Australia/Au/; $country =~ s/North America/N.A./; $country =~ s/South America/S.A./; $pktloss =~ s/ //g; #print "$country\t$index\t$response\t$pktloss\n"; if($index < 50) { $color = $col_brighten . $red; } elsif($index < 80) { $color = $col_brighten . $yellow; } else { $color = $col_brighten . $green; } print $color; printf "%-9s %5d %13d %11s\n", $country,$index,$response,$pktloss; print $col_normal; # write; last if($country eq "S.A."); } } # comment out the following code up to the commented-out sleep($seconds) # command if you don't want to print the interactive dots $| = 1; for($i = $seconds; $i > 0; $i = $i - $increment) { print "."; if($i < $increment) { sleep($i); } else { sleep($increment); } } print "\n"; $| = 0; # sleep($seconds); } # this subroutine strips out all the html tags it finds in the array # it receives as an argument sub strip_tags { @local = shift; my $tmp; foreach(@local) { s//\n/g; s/<\/tr>/\n/g; s/<.*?>/\t/g; s/\t+/\t/g; $tmp .= $_; } return $tmp; }