Created
March 3, 2012 02:18
-
-
Save aggrolite/1963776 to your computer and use it in GitHub Desktop.
first stab at AP elections interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package WWW::AP::Elections::2012; | |
use Moose; | |
use HTML::TreeBuilder::XPath; | |
use LWP::Simple qw(get); | |
use Geography::States; | |
has states => ( | |
is => 'ro', | |
lazy_build => 1, | |
); | |
my $tree = HTML::TreeBuilder::XPath->new; | |
#will return a hash ref with the state's election results | |
sub get_results { | |
my $self = shift; | |
unless (@_) { return {} } | |
use Data::Dumper; | |
warn Dumper $self->states; | |
return {}; #return state info hash | |
} | |
sub _build_states { | |
my $content = get( | |
'http://hosted.ap.org/dynamic/files/elections/2012/by_state/AZ_Page_0228.html?SITE=CSPANELN&SECTION=POLITICS' | |
) or return {}; | |
$tree->parse($content); | |
my @states; | |
my @list = | |
$tree->findnodes('.//select[@id="States_Menu"]/option[@value=~/\.html/]'); | |
foreach my $state (@list) { | |
my %info; | |
if ( my $name = $state->as_text ) { | |
$name =~ s@.+?\d:\s*@@; | |
$info{name} = $name; | |
} | |
$info{link} = $state->findvalue('./@value'); | |
push @states, \%info if $info{name} && $info{link}; | |
} | |
return return \@states; | |
} | |
sub _absurl { | |
my ($self, $url) = @_; | |
return "http://hosted.ap.org/dynamic/files/elections/2012/by_state/$url"; | |
} | |
sub _check_state { | |
my $state = shift; | |
my $us = Geography::States->new('USA'); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment