#!/usr/bin/perl # COPYRIGHT 2008 Erik Aronesty # GNU Public License # http://creativecommons.org/licenses/GPL/2.0/ # original source: http://www.documentroot.com/votecompare.txt use strict; use WWW::Mechanize::Cached; use Date::Parse; use Data::Dumper; use POSIX qw(strftime); use Getopt::Long; our $opt_debug; our $opt_exclude; GetOptions("debug", "exclude=s"); require Cache::FileCache; my $cache_parms = { default_expires_in => "7d", namespace => 'www-mechanize-cached', }; my $cache = Cache::FileCache->new( $cache_parms ); my $mech = WWW::Mechanize::Cached->new(cache=>$cache); $mech->agent_alias( 'Windows IE 6' ); $mech->get("http://www.vote-smart.org/election_president.php"); my @cans = $mech->content() =~ m|]*>\s*([^<>]+)<.*?party:\s*(\w+)|sig; my %vevs; # each entry is a separate bill voting events my %cans; my %billj; # house/senate bills joined by hrid for (my $i = 0; $i< $#cans; $i+=3) { my ($can, $name, $party) = @cans[$i..$i+3]; my ($nick) = $1 if $name =~ s/\s*'(\S+)'\s*/ /; $cans{$can} = {}; $cans{$can}->{name} = $name; $cans{$can}->{nick} = $nick; $cans{$can}->{party} = $party; print "$can\t$name\t$nick\n" if $opt_debug; $mech->get("http://www.vote-smart.org/voting_category.php?can_id=$can"); next if $mech->content() =~ /does not have any voting records/; # 07/27/1999China Trade resolution
H J Res 57YResolution Failed (House) - H
(170 - 260) my @votes = $mech->content() =~ m|]*>]*>([\d/ -]+)\s*]*>([^<>]+)(.*?)]*>([^<>]*)|gsi; die $mech->content() if !@votes; for (my $i = 0; $i< $#votes; $i+=5) { my ($date, $bid, $bname, $hr, $bv) = @votes[$i..$i+4]; $hr =~ s/<[^<>]*>//g; $hr =~ s/\s+//g; $bv =~ s/<[^<>]*>//g; $bv =~ s/\s+//g; die $bv unless $bv =~ /^(Y|N|G|NV|NA)$/i; my $ymd = strftime "%Y-%m-%d", localtime(str2time($date)); print "$can\t$ymd\t$bid\t$bname\t$bv\n" if $opt_debug; $vevs{$bid}->{name} = $bname; $vevs{$bid}->{date} = $date; $vevs{$bid}->{hr} = $hr; $vevs{$bid}->{vote}->{$can} = $bv; $billj{$hr}->{name} = $bname; $billj{$hr}->{date} = $date; $billj{$hr}->{bids}->{$bid} = 1; $billj{$hr}->{vote}->{$can} = $bv; } } my %include; for my $can (keys(%cans)) { $mech->get("http://www.vote-smart.org/finance.php?can_id=$can"); my ($oscanid) = $mech->content =~ m|opensecrets.org.*?id=(\w+)|; $cans{$can}->{osid} = $oscanid; $include{$can} = 1 unless $cans{$can}->{name} =~ /$opt_exclude/i; } ###### for the election for my $hr (keys(%billj)) { my ($y, $n, $a) = (0,0,0); for my $can (keys(%{$billj{$hr}->{vote}})) { next unless $include{$can}; $y = 1 if $billj{$hr}->{vote}->{$can} eq 'Y'; $n = 1 if $billj{$hr}->{vote}->{$can} eq 'N'; $a = 1 if $billj{$hr}->{vote}->{$can} eq 'NV'; }; my $polar; if ($y && $n) { $polar = 1; } if ($polar) { $billj{$hr}->{polar} = 1; my ($bid) = keys(%{$billj{$hr}->{bids}}); $mech->get("http://www.votesmart.org/issue_keyvote_detail.php?cs_id=$bid"); my (@issues) = $mech->content() =~ m|]*>\s*([^<>]+?)\s*|gsi; $billj{$hr}->{issues} = \@issues; my $line; my $ref = ""; $line .= ""; $line .= "$ref" . $billj{$hr}->{name} . ""; my $j = ''; for my $can (keys(%{$billj{$hr}->{vote}})) { $j .= "$cans{$can}->{name}, " if $billj{$hr}->{vote}->{$can} eq 'Y'; }; $j =~ s/,\s*$//; $line .= "$j"; $j = ''; for my $can (keys(%{$billj{$hr}->{vote}})) { $j .= "$cans{$can}->{name}" if $billj{$hr}->{vote}->{$can} eq 'N'; } $j =~ s/,\s*$//; $line .= "$j"; $line .= "\n"; $billj{$hr}->{line} = $line; } } my @hr = sort {($billj{$a}->{issues}->[0] . $billj{$a}->{name}) cmp ($billj{$b}->{issues}->[0] . $billj{$b}->{name})} keys(%billj); my $prev_issue; for my $hr (@hr) { next unless $billj{$hr}->{line}; if (!($billj{$hr}->{issues}->[0] eq $prev_issue)) { $prev_issue = $billj{$hr}->{issues}->[0]; print "$prev_issue\n"; } print $billj{$hr}->{line}; } exit 0; ###### for the primary for my $party (('Democratic','Republican')) { print "$party Nominee Polarizing Votes:\n"; for my $hr (keys(%billj)) { my ($y, $n, $a) = (0,0,0); for my $can (keys(%{$billj{$hr}->{vote}})) { next unless $cans{$can}->{party} eq $party; $y = 1 if $billj{$hr}->{vote}->{$can} eq 'Y'; $n = 1 if $billj{$hr}->{vote}->{$can} eq 'N'; $a = 1 if $billj{$hr}->{vote}->{$can} eq 'NV'; }; my $polar; if ($y && $n) { $polar = 1; } if ($polar) { print ">> " . $billj{$hr}->{name}; print " yes:"; for my $can (keys(%{$billj{$hr}->{vote}})) { next unless $cans{$can}->{party} eq $party; print " $cans{$can}->{name}" if $billj{$hr}->{vote}->{$can} eq 'Y'; }; print " no: "; for my $can (keys(%{$billj{$hr}->{vote}})) { next unless $cans{$can}->{party} eq $party; print " $cans{$can}->{name}" if $billj{$hr}->{vote}->{$can} eq 'N'; } print "\n"; } } }