1 <% objToJson($return) %>
6 my $url='http://www.ffiec.gov/Geocode/default.aspx';
8 my $sub = $cgi->param('sub');
15 use HTTP::Request::Common qw( GET POST );
18 if ( $sub eq 'censustract' ) {
20 my %arg = $cgi->param('arg');
21 warn join('', map "$_: $arg{$_}\n", keys %arg )
24 my $ua = new LWP::UserAgent;
25 my $res = $ua->request( GET( $url ) );
30 unless ($res->code eq '200') {
32 $error = $res->message;
36 my $content = $res->content;
37 my $p = new HTML::TokeParser \$content;
40 while (my $token = $p->get_tag('input') ) {
41 if ($token->[1]->{name} eq '__VIEWSTATE') {
42 $viewstate = $token->[1]->{value};
44 if ($token->[1]->{name} eq '__EVENTVALIDATION') {
45 $eventvalidation = $token->[1]->{value};
47 last if $viewstate && $eventvalidation;
50 unless ($viewstate && $eventvalidation ) {
52 $error = "either no __VIEWSTATE or __EVENTVALIDATION found";
56 my($zip5, $zip4) = split('-',$arg{zip});
58 #ugh workaround a mess at ffiec
59 $arg{year} = " $arg{year}" unless $arg{year} = "2010";
61 __VIEWSTATE => $viewstate,
62 __EVENTVALIDATION => $eventvalidation,
63 ddlbYear => $arg{year},
65 txtAddress => $arg{address},
66 txtCity => $arg{city},
67 ddlbState => $arg{state},
69 btnSearch => 'Search',
71 warn join("\n", @ffiec_args )
74 push @{ $ua->requests_redirectable }, 'POST';
75 $res = $ua->request( POST( $url, \@ffiec_args ) );
79 unless ($res->code eq '200') {
81 $error = $res->message;
85 my @id = qw( MSACode StateCode CountyCode TractCode );
86 $content = $res->content;
87 warn $res->content if $DEBUG > 1;
88 $p = new HTML::TokeParser \$content;
89 my $prefix = 'UcGeoResult11_lb';
91 sub { my $t=shift; scalar( grep { lc($t) eq lc("$prefix$_")} @id ) };
93 while (my $token = $p->get_tag('span') ) {
94 next unless ( $token->[1]->{id} && &$compare( $token->[1]->{id} ) );
95 $token->[1]->{id} =~ /^$prefix(\w+)$/;
96 $return->{lc($1)} = $p->get_trimmed_text("/span");
99 $error = "No census tract found" unless $return->{tractcode};
100 $return->{tractcode} .= ' '
101 unless $error || $JSON::VERSION >= 2; #broken JSON 1 workaround
103 } #unless ($res->code eq '200')
105 } #unless ($viewstate)
107 } #unless ($res->code eq '200')
109 $error = "FFIEC Geocoding error: $error" if $error;
110 $return->{'error'} = $error;