/) {
my %hash = ();
push(@data, \%hash);
$data_idx++;
#print "New day\n";
}
#| digg.com/view/all/popular/today/page3 | 7 |
if (m/(.*?)(\/.*|)<\/a><\/td> | (\d+)<\/td><\/tr>/) {
my ($url, $site, $hits) = ($1, $2, $4);
process($url, $site, $hits);
}
# | | engtech.wordpress.com/tag/nokia-6682 | 4 |
elsif (m/(.*?)(\/.*|)<\/td> | (\d+)<\/td><\/tr>/) {
my ($url, $site, $hits) = ("$1$2", $1, $3);
process($url, $site, $hits);
}
}
close($ifh);
}
sub process {
my ($url, $site, $hits) = @_;
#print "$hits, $url\n";
my $ref = $data[$data_idx];
# Normalize site urls
if (defined $translate{$site}) {
$site = $translate{$site};
}
$sites{$site} = $data_idx; # newest to oldest
# Keep track of URLs per site
if (not defined $urls{$site}) {
my %hash = ();
$urls{$site} = \%hash;
}
$urls{$site}{$url} = 1;
# Keep count
if (not defined $ref->{$site}) {
$ref->{$site} = 0;
}
$ref->{$site} += $hits;
}
sub output {
delete($sites{'REMOVE'});
delete($urls{'REMOVE'});
my $ofile = $prefix."_out.html";
my $ofh = new FileHandle($ofile, "w") || die "could not write '$ofile': $!";
print $ofh "\n";
print $ofh "| Site | ";
for(my $i=$#data; $i>=0; $i--) {
my $day = $#data - $i + 1;
print $ofh "Day $day | ";
}
print $ofh "Totals | \n";
my @text = ();
my @totals = ();
foreach my $site (sort {sortSites($a, $b)} keys %sites) {
push(@text, "| $site | ");
push(@totals, 0);
}
for(my $i=$#data; $i>=0; $i--) {
my $index = 0;
foreach my $site (sort {sortSites($a, $b)} keys %sites) {
# Normalize
if (not defined $data[$i]->{$site}) {
$data[$i]->{$site} = 0;
}
my $value = $data[$i]->{$site};
$totals[$index] += $value;
$text[$index] .= "".$value." | ";
$totalsFromSite{$site} = $totals[$index];
$index++;
}
}
for(my $i=0; $i<=$#text; $i++) {
print $ofh $text[$i] . "" . $totals[$i] . " | \n";
}
print $ofh " \n";
print $ofh "\n";
foreach my $site (sort {sortSites($a, $b)} keys %sites) {
my $total = $totalsFromSite{$site};
my $url_text = "";
foreach my $url (sort keys %{$urls{$site}}) {
$url_text .= "- $url
";
}
$url_text .= " ";
print $ofh "| $site | $total | $url_text | ";
}
print $ofh " \n";
close($ofh);
}
sub sortSites {
my ($a, $b) = @_;
my $vala = $sites{$a};
my $valb = $sites{$b};
if ($vala == $valb) {
return($a cmp $b);
}
else {
return($valb <=> $vala);
}
}
sub generateGraph {
my @graph_high = ();
my @graph_low = ();
my $max_high = 0;
my $max_low = 0;
my $day = 0;
foreach my $ref (reverse @data) {
my @row_high = ();
push(@row_high, $day);
foreach my $site (@sites_high) {
my $value = $ref->{$site};
if ($value > $max_high) {
$max_high = $value;
}
push(@row_high, $value);
delete($sites{$site});
}
push(@graph_high, \@row_high);
$day++;
}
image($graph_high, $max_high, \@graph_high, \@sites_high);
$day = 0;
foreach my $ref (reverse @data) {
my @row_low = ();
push(@row_low, $day);
foreach my $site (sort {sortSites($a, $b)} keys %sites) {
my $value = $ref->{$site};
if ($value > $max_low) {
$max_low = $value;
}
push(@row_low, $value);
}
push(@graph_low, \@row_low);
$day++;
}
my @legend = ();
foreach my $site (sort {sortSites($a, $b)} keys %sites) {
push(@legend, $site);
}
image($graph_low, $max_low, \@graph_low, \@legend);
}
sub image {
my ($file, $max, $gref, $lref) = @_;
my @graph = @{$gref};
my @legend = @{$lref};
my $gdata = GD::Graph::Data->new();
foreach my $row (@graph) {
$gdata->add_point(@{$row});
}
my @colours = ("black", "blue", "purple", "green", "red", "gray", "dgray");
my $chart = GD::Graph::lines->new(600,375);
$chart->set_legend(@legend);
$chart -> set_x_axis_font("/usr/X11R6/lib/X11/fonts/TTF/lusimbi.ttf", 10);
$chart -> set_y_axis_font("/usr/X11R6/lib/X11/fonts/TTF/luximbi.ttf", 10);
$chart -> set_x_label_font("/usr/X11R6/lib/X11/fonts/TTF/luximb.ttf", 12);
$chart -> set_y_label_font("/usr/X11R6/lib/X11/fonts/TTF/luximb.ttf", 12);
$chart -> set_legend_font("/usr/X11R6/lib/X11/fonts/TTF/luximbi.ttf", 10);
$chart->set
(
y_label => "Traffic",
x_label => "Days",
y_max_value => $max,
line_width => 3,
y_long_ticks => 1,
dclrs => [@colours]
);
open(IMAGE, ">$file") or
die "Cannot open $file output png file for writing: $!";
print IMAGE $chart->plot($gdata)->png;
close IMAGE;
}
|