Friday, August 14, 2009

Google Web APIs

Google Web APIs are for developers and researchers interested in using Google as a resource in their applications and enables them to easily find and manipulate information on the web and to query more than 8 billion web documents directly from their own computer programs. Google uses the SOAP and WSDL standards to act as an interface between the user’s program and Google API and officually support Java, .NET, Ruby and Perl. By using the API developers can issue search requests to Google's web pages index and receive results as structured data (number of results, URI's, Snippets, Query Time, etc.). Additionally developers can access information in the Google cache and can check the spelling of words. To start using the API one needs to download and install the API package from http://www.google.com/apis/ and create an account to get an license key (however the google FAQs state that google is no longer issuing new API keys, so this step can be problematic). The key I received last year is limited to 1,000 queries/day. Last but not least one will need a SOAP implementation like e.g. Apache Axis, SOAP::Lite for Perl, SOAP4R if Ruby is the language of choice, etc. The installed API package contains:

  • googleapi.jar - Java library for accessing the Google Web APIs service.

  • GoogleAPIDemo.java - Example program that uses googleapi.jar. dotnet/

  • Example .NET - programs that uses Google Web APIs.

  • APIs_Reference.html - Reference doc for the API. Describes semantics of all calls and fields.

  • Javadoc - Documentation for the example Java libraries.

  • Licenses - Licenses for Java code that is redistributed in this package.

  • GoogleSearch.wsdl -WSDL description for Google SOAP API.

  • soap-samples/ - Different examples

Following an small example, I found somewhere on the web, of using the SOAP::Lite SOAP implementation to make a Google query:

Example: query.pl

#!/usr/local/bin/perl –w
use SOAP::Lite;
# Configuration
$key = "The Google API Key Goes Here";
# Initialize with local SOAP::Lite file
$service = SOAP::Lite
-> service('file:GoogleSearch.wsdl');
$query= “Viadrina”;
$result = $service
-> doGoogleSearch(
$key, # key
$query, # search query
0, # start results
10, # max results
"false", # filter: boolean to turn on/off automatic filtering
"", # restrict (string) , e.g. "linux"
"false", # safeSearch: boolean
"", # language restrict e.g. lang_de
"", # input encoding
"" # output emcoding
);

if(defined($result->{resultElements})) {
print join "\n",
"Found:",
$result->{resultElements}->[0]->{title},
$result->{resultElements}->[0]->{URL},
$result->{resultElements}->[0]->{snippet} . "\n"
}

print "\n The search took ";
print $result->{searchTime};
print "\n\n";
print "The estimated Number of results for your query is: ";
print $result->{estimatedTotalResultsCount};
print "\n\n";

No comments: