Difference between revisions of "User:CommanderC/winners spells.pl"
CommanderC (talk | contribs) (Some scripts) |
(No difference)
|
Revision as of 16:36, 24 December 2012
#!/usr/bin/env perl use strict; use warnings; use LWP::Simple; #get() my %spells; my $wins = 0; my $with_spells = 0; my $read_from_pastebin = 0; sub main { my @lines; if ($read_from_pastebin) { print "- Downloading dcss_0.11_tourney_players.txt.\n"; my $tournament_players = get('http://pastebin.com/raw.php?i=bAU5bAx6'); die unless defined $tournament_players; @lines = split /\n/, $tournament_players; } else { print "- Reading from dcss_0.11_tourney_players.txt.\n"; open my $file, "< dcss_0.11_tourney_players.txt" or die; @lines = <$file>; close($file); } shift @lines; #skip first line foreach my $player (@lines) { my ($player_name, $player_url) = split ',', $player; print " |\n"; print " |- Downloading $player_url\n"; my $player_profile = get($player_url); die unless defined $player_profile; handle_profile($player_profile); } # All the information is in %spells print "Winners : $wins\n"; print "With spells : $with_spells\n"; my $no_spells = $wins - $with_spells; print "Without spells : $no_spells\n"; my @k = sort { $spells{$a} <=> $spells{$b} } keys %spells; for (reverse @k) { print "$_ : $spells{$_}\n"; } } sub handle_profile { my $profile = shift; my @lines = split /\n/, $profile; my $wins_section = 0; foreach my $line (@lines) { if ($line =~ m{<h3>Wins</h3>}) { $wins_section = 1; next;} if ($line =~ m{</table>}) { $wins_section = 0; next;} if ($wins_section) { if ($line =~ /href="([^"]+)"/) { $wins++; my $morgue = $1; print " | |\n"; print " | |- Downloading $morgue\n"; my $morgue_content = get($morgue); die unless defined $morgue_content; handle_morgue($morgue_content); } } } } sub handle_morgue { my $morgue = shift; my @lines = split /\n/, $morgue; my ($spell_section, $blank_line, $prev_blank_line) = (0, 0 ,0); foreach (@lines) { $prev_blank_line = $blank_line; $blank_line = length == 0 ? 1 : 0; $spell_section = 0 if ($prev_blank_line && $blank_line); if ($spell_section) { next if not /-/; my $spell_name = substr($_, 4, 26 - 4); if (not defined($spells{$spell_name})) { $spells{$spell_name} = 0; } $spells{$spell_name}++; } elsif (/You knew the following spells:/) { $spell_section = 1; $with_spells++; } else { next; } } } main;