# ga_lib.pl # library of routines for Google API scripts # see http://www.staggernation.com for info # By Kevin Shay, kevin [AT] staggernation [DOT] com # last modified 8/6/02 # from http://interconnected.org/home/more/GoogleSearch.pl.txt: # boolean workaround for 1999 schema - see http://groups.yahoo.com/group/soaplite/message/895 *SOAP::XMLSchema1999::Deserializer::as_boolean = *SOAP::XMLSchemaSOAP1_1::Deserializer::as_boolean = \&SOAP::XMLSchema2001::Deserializer::as_boolean; sub cgi_params { # return a ref to a hash with the parameters that were passed to # the CGI script my ($cgi) = @_; my ($name, $value); my %params = (); for $name ($cgi->param) { # handle multiple instances of same field $value = join(',', $cgi->param($name)); $params{$name} = $value; } return \%params; } sub google_search { # do a search with the Google API my ($service, $key, $q, $start, $max, $filter) = @_; # assign these default values to variables for clarity my ($restrict, $safe, $lr, $ie, $oe) = ('', 0, '', 'latin1', 'utf8'); my $response = $service->doGoogleSearch( $key, $q, $start, $max, $filter, $restrict, $safe, $lr, $ie, $oe); if ($service->call->fault) { # caller will check this to see if there was an error $response->{'err'} = $service->call->faultstring; # make the message a little more friendly if ($response->{'err'} =~ /exceeded for key.*$/) { $response->{'err'} = q{Sorry, but we're occasionally too popular for our own good. Our Google API key has a limit of 1000 queries per day, and if you're seeing this message, we've exceeded it. You can either get your own key and enter it in the "License key" field above, or come back tomorrow.

Nous sommes désolés, mais parfois notre site est trop populaire. Notre «API Key» de Google est limitée à 1000 questions par jour. Si vous voyez ce message, la limite est déjà dépassée. Pour contourner cette limite, vous pouvez soit obtenir votre propre «API Key» auprès de Google, et ensuite l'inscrire dans la zone «Licence key» ci-dessus, soit réessayer demain. Nous nous excusons de la gène occassionnée.}; #$response->{'err'} =~ s/(?<=exceeded for key).*$/$err_addendum/; } } return $response; } sub insert_commas { # put commas in a large number my ($num) = @_; return $num if ($num < 1000); $num = reverse($num); $num =~ s/(\d{3})(?=\d)(?!\d*\.)/$1,/g; return scalar reverse($num); } sub html_stylesheet { # stylesheet for main page, taken wholesale from Google return <<"EOH"; EOH } sub html_page_header { # generate the heading for one of the Google API scripts my ($page_title, $script_name, $readme_path) = @_; my $output = "$page_title"; $output .= html_stylesheet(); $output .= <<"EOH"; Google API $script_name
From staggernation.com - Read Me - GAPS - GARBO - GAWSH EOH return $output; } sub key_message { return <<'EOT'; If you have your own Google API license key, we would appreciate your entering it here. It will be used only for the searches you do with this script, and it will not be stored anywhere or used in any other way. EOT } # special-character conversion # from code by Jarkko Hietaniemi in sub utf8_to_latin { my ($str, $cache) = @_; $str =~ s/([\xC0-\xDF])([\x80-\xBF])/$cache->{$1 . $2} ||= chr(ord($1) << 6 & 0xC0 | ord($2) & 0x3F)/eg; return $str; } sub latin_to_utf8 { my ($str, $cache) = @_; $str =~ s/([\x80-\xFF])/$cache->{$1} ||= chr(0xC0 | ord($1) >> 6) . chr(0x80 | ord($1) & 0x3F)/eg; return $str; } 1;