#!/usr/bin/perl
use CGI qw(:standard);
use LWP::Simple qw/get/;
use HTML::Parser;
use HTML::Element;
use URI::URL;
$what = param("txtAddress");
$what_directory = param("directory");
# output a document
print header(), start_html("Address Details for $what"),
p("
Christchurch Address Details for '$what' using $what_directory pages
You typed: ", tt(escapeHTML($what))), "
";
#end_html();
#
$count=0;
$targetCount=60;
if ($what_directory = "white") {
#$get_address = "http://www.whitepages.co.nz/quick/search?key=$what&lkey=Christchurch&loc=CH&x=22&y=23&listing_type=ALL&exact_spelling=0";
$get_address = "http://www.whitepages.co.nz/Pages/WhitePages_SearchResultsDefault/0,2121,a1281_b1516,00.html?fpQuery=$what&fplkey=Christchurch&fpLoc=1546&fpListType=&fpExact=off&x=22&y=23";
}
else {
$get_address = "http://www.yellowpages.co.nz/quick/search?key=$what&lkey=Christchurch&stype=&search.x=0&search.y=0";
}
#print $get_address;
my $content = get($get_address); #put site html in $content.
die "get failed" if (!defined $content);
# create parser object
my $parser = HTML::Parser->new(api_version=>3,
start_h=>[\&startTag, 'tag, attr'] ,
end_h=>[\&endTag, 'tag'] ,
text_h=>[\&textElem, 'text']
);
#parse object.
$parser->parse($content);
sub startTag
{
my ($tag, $attrHash) = @_;
#$test=>attrHash{class};
#print "$test";
if ($tag =~ "td") {
$isAddress = $attrHash->{class};
if ($isAddress =~ "result1") {
$printAddress = 'yes';
}
if (($isAddress =~ "result1") || ($isAddress =~ "result2") || ($isAddress =~ "result_title") || ($isAddress =~ "result")) {
#$printAddress = 'yes';
$printText = 'yes';
}
#print "TAG: $tag \n";
#$printText = 'yes';
}
#print "ATTR HASH: " , Dumper $attrHash , "\n";
#print "-----\n";
}
sub endTag
{
my $tag = shift;
#print "END TAG: $tag \n";
#print "-----\n";
}
sub textElem
{
my $text = shift;
if ($printText =~ 'yes') {
#print "$isAddress";
if ($isAddress =~ "result1") {
print "
";
}
if ($isAddress =~ "result_title") {
print "";
}
if ($isAddress =~ "result2") {
print "
";
}
#($test2) = $text =~ m{([A-Z]+\s+[0-9]+[-]+[0-9]+\s+[A-Z]+\s+[0-9])}gix;
($test2) = $text =~ m{([0-9][0-9]$)};
($test3) = $text =~ m{(^RESULTS)};
#print $test3;
$int = 0 + $test2;
#print "Number of pages : " . $int;
if (($int > 20) && ($test3 =~ 'RESULTS')){
print "
Search has multiple pages. Click here to refine search - ";
}
print "$text ";
$count=$count+1;
#print "$count ";
if ($count =~ $targetCount) {
$targetCount=$targetCount+12;
print "
";
}
}
if ($printAddress =~ 'yes') {
($test1) = $text =~ m{([A-Z]+\s)}gix;
$lengthOfAddress=length($test1);
#print "$lengthOfAddress";
if (($test1 =~ "Level") || ($test1 =~ "PO") || ($test1 =~ "Unit") || ($lengthOfAddress=~2)) {
print "
Goto Map Search";
}
else {
print "
Goto Map for ".$test1."";
}
}
$printText = 'no';
$printAddress = 'no';
#print "-----\n";
}
print "
Want to find another address? Click here to go back
";
print "
";
print end_html();