Benutzer:El Cazangero/Skript

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
#!/usr/bin/perl -w
use strict;
use encoding "utf8";

use HTML::TreeBuilder;
use LWP;
use URI::Escape;

my $pfx = "http://de.wikipedia.org/wiki/";
my $path = "Wikipedia:Schiedsgericht/Wahl/November_2007/";
my @cand = ( "Achates", "Björn Bornhöft", "Bodhi-Baum", "Bubo bubo",
        "Florian Adler", "Fossa", "Karsten11", "Mo4jolo", "Rainer Lippert",
        "Rainer Zenz", "Schlesinger", "sebmol", "Stefan64", "SVL", "Thogo",
        "Tinz", "Umschattiger" );
my %score = ();
my $rank = 0;
my $date = scalar localtime();

my $ua = LWP::UserAgent->new();
foreach my $cand (@cand) {
        my $url = sprintf "%s%s%s", $pfx, $path,
                URI::Escape::uri_escape_utf8($cand);
        my $req = HTTP::Request->new(GET => $url);
        my $res = $ua->request($req);
        if ($res->is_success()) {
                my $tree = HTML::TreeBuilder->new();
                $tree->utf8_mode(1);
                $tree->parse_content($res->content());
                my @pro_votes = ($tree->find("ol"))[1]->find("li");
                $score{$cand} = scalar @pro_votes;
        } else {
                die sprintf "Error: GET %s: %s", $url, $res->status_line();
        }
}

print <<"EOT";
{| class="wikitable"
|+ | $date
|- class="hintergrundfarbe5"
! Platz || Kandidat || Stimmen
|-
EOT
foreach my $key (sort { $score{$b} <=> $score{$a} } keys %score) {
        printf "| align=\"right\" | %2d\n", ++$rank;
        printf "| align=\"right\" | [[%s%s|%s]]\n",
                $path, URI::Escape::uri_escape_utf8($key), $key;
        printf "| align=\"right\" | %3d\n", $score{$key};
        print "|-\n";
}
print "|}\n";