Skip to content

Instantly share code, notes, and snippets.

@aggrolite
Created March 3, 2012 02:16
Show Gist options
  • Save aggrolite/1963744 to your computer and use it in GitHub Desktop.
Save aggrolite/1963744 to your computer and use it in GitHub Desktop.
first stab at AP elections interface
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;
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;
}
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;
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;
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;
}
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