#!/usr/bin/perl -w
use lib "/hsphere/local/home/chovy/modules";
use strict;
use SOAP::Lite;
use Data::Dumper;
use CGI;
use CGI::Carp 'fatalsToBrowser';
use URI::Escape;
use HTML::Entities;
my $key='nEe5G/BQFHKnsQcbc0ZGtD5deWPe5zK0';
my $cgi = new CGI;
#my $query="web developer consultant";
my $query = $cgi->param('query');
#my $max = 20;
my $max = $cgi->param('max');
my $size = 10;
#my $match_url = 'apwebdesign.com';
my $match_url= $cgi->param('match_url');
my $match_count = 0;
my @positions;
my $pos_idx = 0;
my $pos_count = 0;
#my $googleSearch = SOAP::Lite -> service("file:GoogleSearch.wsdl");
#my $result = $googleSearch -> doGoogleSearch($key, $query, 5, 5, "false", "", "false", "", "latin1", "latin1");
#my @queries;
#my @results;
print "Content-Type: text/html\n\n";
#print Dumper($result->{'resultElements'});
&printHeader();
if ($query) {
#print Dumper($query);
&setupQuery();
}
&printFooter();
sub setupQuery {
my $start = 0;
#my $end = 5;
my $i = 0;
while($start < $max) {
#print "start=$start max=$max
\n";
my $googleSearch = SOAP::Lite -> service("file:GoogleSearch.wsdl");
my $result = $googleSearch -> doGoogleSearch($key, $query, $start, $size, "false", "", "false", "", "latin1", "latin1");
&printResults($result);
$start = $start+$size;
#$end = $end+5;
}
}
sub printResults {
my ($result) = @_;
foreach my $arrayRef (@{$result->{'resultElements'}}) {
$pos_count++;
if ($match_url) { #match results to match_url exists?
if ($arrayRef->{'URL'} =~ m/$match_url/sig) {
&printMatch($arrayRef);
$match_count++;
$positions[$pos_idx] = $pos_count;
$pos_idx++;
} elsif ($pos_count <= 10) {
&printNoMatch($arrayRef);
}
} elsif ($pos_count <= 10) {
&printNoMatch($arrayRef);
}
}
}
sub printMatch {
my ($arrayRef) = @_;
print qq{$pos_count) $arrayRef->{'title'}
};
print "$arrayRef->{'snippet'}
";
print qq{$arrayRef->{'URL'}
};
}
sub printNoMatch {
my ($arrayRef) = @_;
print qq{$pos_count) $arrayRef->{'title'}
};
print "$arrayRef->{'snippet'}
";
print qq{$arrayRef->{'URL'}
};
}
sub printFooter {
print qq{Results: $match_url found $match_count times
};
print qq{positions: };
print join(", ", @positions);
print "
";
}
sub printHeader {
my $safe_query = encode_entities($query);
print <
query:
match_url:
max results:
HTML
}
###### NOT USED #####
=head
sub printResultsOld {
foreach my $hashRef ($result->{'resultElements'}) {
foreach my $arrayRef (@$hashRef) {
my %hash = %$arrayRef;
foreach my $key (keys %hash) {
print "$key = $hash{$key}
\n";
}
}
}
}
sub printResults {
print "title = $result->{'resultElements'}->[0]->{'title'}
";
print "url = $result->{'resultElements'}->[0]->{'URL'}
";
print "snippet = $result->{'resultElements'}->[0]->{'snippet'}
";
}
=cut