use LWP::UserAgent;
use HTTP::Request;
use HTTP::Request::Common qw( GET POST );
+use HTTP::Cookies;
use HTML::TokeParser;
-use URI::Escape;
+use URI::Escape 3.31;
use Data::Dumper;
$DEBUG = 0;
warn Dumper($location, $year) if $DEBUG;
- my $url='http://www.ffiec.gov/Geocode/default.aspx';
+ my $url = 'http://www.ffiec.gov/Geocode/default.aspx';
my $return = {};
my $error = '';
- my $ua = new LWP::UserAgent;
+ my $ua = new LWP::UserAgent('cookie_jar' => HTTP::Cookies->new);
my $res = $ua->request( GET( $url ) );
warn $res->as_string
if $DEBUG > 1;
- unless ($res->code eq '200') {
+ if (!$res->is_success) {
$error = $res->message;
last if $viewstate && $eventvalidation;
}
- unless ($viewstate && $eventvalidation ) {
+ if (!$viewstate or !$eventvalidation ) {
$error = "either no __VIEWSTATE or __EVENTVALIDATION found";
my($zip5, $zip4) = split('-',$location->{zip});
- $year ||= '2011';
- #ugh workaround a mess at ffiec
- $year = " $year" if $year ne '2011';
+ $year ||= '2012';
my @ffiec_args = (
__VIEWSTATE => $viewstate,
__EVENTVALIDATION => $eventvalidation,
+ __VIEWSTATEENCRYPTED => '',
ddlbYear => $year,
- ddlbYear => '2011', #' 2009',
txtAddress => $location->{address1},
txtCity => $location->{city},
ddlbState => $location->{state},
$return->{lc($1)} = $p->get_trimmed_text("/span");
}
- $error = "No census tract found" unless $return->{tractcode};
+ unless ( $return->{tractcode} ) {
+ warn "$error: $content ". Dumper($return) if $DEBUG;
+ $error = "No census tract found";
+ }
$return->{tractcode} .= ' '
unless $error || $JSON::VERSION >= 2; #broken JSON 1 workaround