#!/bin/perl
#code for ah.pl
#begin
use strict; use warnings; use diagnostics;
open F,"<$ARGV[0]" or die "Can't open $ARGV[0] due to: $! \n";
my @f=<F>; close F; my $i=0; my %h=(); my $x=0; my $y=-1; my $v=0;
for (@f) {
s/\<em\>//; s/\<\/em\>//;
tr/\000-\037/ /; s/^\s{2,}//; s/\s{2,}$//; s/^\s*$//; next if m/^$/;
if (m/Voters: \<strong\>(\d+)\<\/strong\>/) { $v=$1; }
last if m/Multiple Choice Poll/;
$i=1 if m/View Poll Results/; next if !$i;
if (!m/\Q<\E/ and !$x) {
s/amp;//; ++$y; $h{$y}{'n'}=$_; $x=1; }
if (m/class.*title.*\<strong\>(\d+)\<\/strong\>/) {
$h{$y}{'v'}=$1 if $x; }
if (m/class.*nowrap.*\>(\d+\.\d+)%/) {
$h{$y}{'p'}=$1 if $x; $x=0; } } close F;
my @r=map{[$_->[0], $_->[1], $_->[2]]}
sort{$b->[1] <=> $a->[1]} sort{$a->[0] cmp $b->[0]}
map{[$h{$_}{'n'}, $h{$_}{'v'}, $h{$_}{'p'}]} keys %h;
my @c=qw(red royalblue seagreen yellow darkorchid
deepskyblue sienna magenta darkslateblue yellowgreen);
open O,">ah.out" or die "Can't open ah.out due to: $! \n";
print O "here is a sorted list of top $ARGV[1] individual sets, $v people have voted so far.\n";
print O "ah.pl script executed on "; my $b=`date -u`; chomp $b; my @b=split / /,$b;
if ($b[3]=~m/1$/) { $b[3].='st'; } elsif ($b[3]=~m/2$/) { $b[3].='nd'; }
elsif ($b[3]=~m/3$/) { $b[3].='rd'; } else { $b[3].='th'; }
if ($b[4]=~m/(\d+):(\d+):(\d+)/) { my $h=$1; if (!$h) { $b[4]="12:$2:$3 AM"; }
elsif ( ($h>0) and ($h<12) ) { $b[4]=$h.":$2:$3 AM"; }
elsif ($h==12) { $b[4]="12:$2:$3 PM"; } else { $b[4]=$h-12 .":$2:$3 PM"; } }
print O "[ DATE: $b[0], $b[1] $b[3], $b[6]\tTIME: $b[4] ($b[5]) ]\n\n";
my $m=1; my $k=0; my $j; for ($j=0;$j<@r;++$j) {
last if $j==$ARGV[1]; printf O "%-60s %-40s %-120s\n",
(!$j ? 1 : ($r[$j][1]==$r[$j-1][1] ? $m : ++$m)) . ". " . $r[$j][0],
"[indent][list] " . $r[$j][1] . " [color=darkslategray]" .
'.' x 10 . "[/color] ( " . $r[$j][2] . "% ) [/list]",
"[list] [b][color=" . $c[$k] . "]" . 'x' x $r[$j][1] .
"[/color][/b] [/list][/indent]"; ++$k; $k=0 if $k==scalar @c;
} close O;
#end