How to Integrate Google Searches into Your Application

Posted by : http://developer.aiwgame.com

 

图片

 


Introduction

The first thing coming to mind when we hear Google is search engine. Googlehas been able to turn the search business up-side-down within the last fiveyears. The founders of Google started with an idea in 95 which really becamewidely used and known in 98/99. Today Google is the number one search engine.You can find out more about Google?s history here. Like otherorganizations Google is trying to establish itself as a platform rather then asolution. This means it provides the necessary tools and infrastructure so otherpeople can build their own solutions on top of it. Google provides a web serviceinterface which allows you to integrate Google searches right into yourapplication. You can find out more about the Google web service API at http://www.google.ca/apis .

 

How to get started with the Google API

You can download from the URL above the developer?s kit which comes with anumber of sample applications for different languages like .NET or Java. Youalso need a valid license key, which you need to pass along with every webservice call. To obtain a Google license key visit the URL http://www.google.ca/apis and select?Create Account? on the left side navigation bar. You need to create an accountby entering your email address and a password. This sends an email to the emailaddress you entered to verify its existence. The email you receive has a link tocomplete the account creation by activating it. When done click on the continuelink which brings you back to the account creation page. At the bottom of thepage you see a link ?sign in here?. Follow the link and sign into your accountwith your email address and password. This shows then a page confirming that alicense key has been generated and sent to your email address. Should you looseyour license key, sign in again and Google will resend the license key to youremail address. The license key is for free but limits you to 1,000 calls perday. This will be more then enough to get started. If you need to make more then1,000 calls per day contact Google.

 

How to reference the Google web service API in your project

Create your project in Visual Studio .NET and in the "solution explorer" paneright click on the project. In the popup menu select ?Add Web Reference? andenter as URL the following WSDL URL – http://api.google.com/GoogleSearch.wsdl. This will check the existence of the WSDL, download it and show you in thedialog the web methods available by this web service. Enter under ?web referencename? the name of the web service reference, for example GoogleSearch. When doneclick ?Add Reference? and you are ready to use the Google web service API. Itwill be shown in the ?solution explorer? under ?Web References?. You can rightclick on the web service reference and update it through the ?Update WebReference? menu item or view it in the object explorer through the ?View inObject Browser? popup menu. This shows you that there are four different typesavailable. The type GoogleSearchService exposes the actual web service calls youcan make. It has three different web methods (plus the usual Begin/End methodsif you want to call a web method asynchronously).

 

GoogleSearchService.doSpellingSuggestion()

When you open up Google in your browser and search for a word or phrase yousee sometimes the phrase ?Did you mean: [suggested search term]? at the top ofthe search results page. Google performs a spell check of the search term youentered and then shows you alternative spellings of your search term. This helpsthe user to search for properly spelled words and phrases and the user cansimply click on it to search for the corrected search term. The Google webservice also provides a web method to check for alternate spellings of a searchterm. Here is a code snippet:

public static string SpellingSuggestion(string Phrase)
{
// create an instance of the Google web service
Google.GoogleSearchService GoogleService = new
Google.GoogleSearchService();

// get the new spelling suggestion
string SpellingSuggestion = GoogleService.doSpellingSuggestion(Key,
Phrase);

// null means we have no spelling suggestion
if (SpellingSuggestion == null)
SpellingSuggestion = Phrase;

// release the web service object
GoogleService.Dispose();
return SpellingSuggestion;
}

First we create an instance of the web GoogleSearchService class and then wecall the web method doSpellingSuggestion(). The first argument is the Googlelicense key you pass along and the second one is the search term. The web methodreturns the alternate spelling of the search term or null if there is noalternate spelling. The code snippet above returns the alternate spelling or theoriginal one. At the end it calls Dispose() to free up the underlying unmanagedresource.

 

GoogleSearchService.doGetCachedPage()

Google is constantly crawling the Internet to keep its search index anddirectory up to date. Google?s crawler also caches the content locally on itsservers and allows you to obtain the cached page, which is the content as ofwhen the crawler visited that resource the last time. URL?s can point to manydifferent resources, most typically to HTML pages. But these can also be Worddocuments, PDF files, PowerPoint slides, etc. The cached page is always in HTMLformat. So for any other resources then HTML it also converts the format toHTML. Here is a code snippet:

public static void GetCachedPageAndSaveToFile(string PageUrl, string
FileName)
{
// create an instance of the Google web service
Google.GoogleSearchService GoogleService = new
Google.GoogleSearchService();

// get the cached page content
byte[] CachedPage = GoogleService.doGetCachedPage(Key, PageUrl);

// file writer to write a stream to the file & a binary writer to write
data to
FileStream FileWriter = new FileStream(FileName, FileMode.Create);
BinaryWriter Writer = new BinaryWriter(FileWriter);

// write the page content to the file and close the streams;
Writer.Write(CachedPage);
Writer.Close();
FileWriter.Close();

// release the web service object
GoogleService.Dispose();
}

First we again create an instance of the GoogleSearchService class and thenwe call the web method doGetCachedPage(). We pass along the Google license keyplus the URL of the page we are looking for. This returns a byte array, usingbase64 encoding, which contains the HTML content of the cached page. Next wecreate a FileStream which we use to write the obtained page to a local file.With FileMode.Create we tell it to create the file, which overwrites anyexisting file. Then we create a BinaryWriter which uses as output theFileStream. Then we write the returned byte array to the BinaryWriter which inturn writes it to the FileStream, which in turn writes it to the local file.Then we close the FileStream and BinaryWriter. At the end we call againDispose() to free up underlying unmanaged resources.

 

GoogleSearchService.doGoogleSearch()

The web method doGoogleSearch() allows you to perform searches. You passalong the search term and then certain filter criteria?s to filter the contentfor exa

mple to
a specific country, language, topic, etc. Here are the argumentsyou pass along to the web method:

 

  • Key ? The Google license key.
  • QueryTerm ? The actual search term. This can be a simple word, a phrase (to search for the phrase you need to put it under double quotes otherwise it searches for the occurrence of all individual words), a list of words (you can use the AND or OR operator; when no operator is used between the words AND is assumed), etc. You can also exclude words or phrases by putting a minus sign in front of it. The Google reference athttp://www.google.ca/apis/reference.html explains all query term capabilities.
  • Start ? A zero based index of the first result to be returned. This allows you to page through the result set. The search result returned by this web method can not be more then MaxResults, therefore you need to make multiple calls and set Start appropriately to get the next results and so forth. If you provide a user interface which allows the user to page through the complete result set, then you would set Start accordingly, to return the results for each page. For example the first call would set it to 0, the next to 11, followed by 21, etc. (assuming MaxResults is set to 10).
  • MaxResults ? The maximum number of results to be returned by the query. This can be a value between one and ten.
  • Filter ? When set to true it filters out duplicate or near-duplicate search results. Near duplicate results are results with the same title and snippets (snippet is the summary text shown for each search result). This also limits the number of search results coming from the same host. So if a web site would return ten records matching the search term then this would only return the first two (called host crowding).
  • Restricts ? Allows to restrict the search to results from one or more countries or one or more topics. For example you can restrict the search to content within the US by setting this value to "countryUS". You can restrict the search to content centered around Linux by setting this value to "linux". The Google reference athttp://www.google.ca/apis/reference.html lists all the possible values.
  • SafeSearch ? Filters out adult content when set to true.
  • LanguageRestrict ? This allows you to restrict the search within one or more languages. The Google reference at http://www.google.ca/apis/reference.html lists all the possible values.
  • InputEncoding ? This value is ignored. All requests should be encoded using UTF-8.
  • OutputEncoding ? This value is ignored. All returned results are encoded using UTF-8.
  •  

This web method allows you to perform simple or complex search queriesagainst Google. It also allows you to filter the search result as well as pagethrough the search result. Here is a code snippet:

public static XmlNode Search(string QueryTerm, int Start, int
MaxResults, bool Filter, string Restricts,
bool SafeSearch, string LanguageRestrict, string InputEncoding, string
OutputEncoding)
{
// create an instance of the Google web service
Google.GoogleSearchService GoogleService = new
Google.GoogleSearchService();

// perform search
Google.GoogleSearchResult SearchResult = GoogleService.doGoogleSearch(Key,
QueryTerm, Start,
MaxResults, Filter, Restricts, SafeSearch, LanguageRestrict,
InputEncoding, OutputEncoding);

// we return the result back as a XML document
XmlDocument ResultXml = CreateXmlDocument(SearchResultXmlNode);

// add the search result
StringValueOfObject(ResultXml.DocumentElement, SearchResult);

// add the result elements and directory categories root node
XmlElement ResultElementsParentNode =
AddChildElement(ResultXml.DocumentElement, "ResultElements");
XmlElement CategoriesParentNode =
AddChildElement(ResultXml.DocumentElement, "DirectoryCategories");

// now add all result elements
foreach (Google.ResultElement ResultElement in SearchResult.resultElements)
StringValueOfObject(ResultElementsParentNode, ResultElement);

// now add all directory categories
foreach (Google.DirectoryCategory DirectoryCategory in
SearchResult.directoryCategories)
StringValueOfObject(CategoriesParentNode, DirectoryCategory);

// release the web service object
GoogleService.Dispose();
return ResultXml;
}

First we create an instance of the GoogleSearchService class and then we callthe web method doGoogleSearch(). We pass along all the arguments as describedabove. This performs the search and returns its result as an instance of theGoogleSearchResult class. The code snippet then takes all values of theGoogleSearchResult object and puts them into a XML document. Please refer to theattached sample application for the complete code. First it creates a XMLdocument with the method CreateXmlDocument(). It then calls the methodStringValueOfObject() which creates a XML element for the object in the XMLdocument using the name of the object as the name of the XML element. The methoduses then reflection to walk the returned GoogleSearchResult object and for eachfield it finds in the object it adds an attribute to the created XML element. Itof course adds to each created attribute the value of the associated objectfield. The returned GoogleSearchResult object has two fields which hold an arrayof ResultElement and DirectoryCategory objects. The method StringValueOfObject()is not able to walk each object in those arrays. Therefore we create two rootXML elements in the XML document using the method AddChildElement(). We thenloop through both arrays and call for each object StringValueOfObject() so wecan convert each object to a XML element adding all its fields as attributes.Finally we call again Dispose() to free up the underlying unmanaged resourcesand then return the XML document which contains all search information of theGoogleSearchService object. This enables you to run XPath queries against thesearch result XML document to find the required search result information.

 

The attached sample application

The attached sample application provides a wrapper class for all Google webmethods. It also provides a simple user interface demonstrating the use of eachweb method. You can enter a search term and get alternate spelling suggestions,you can download the cached HTML page of a URL and display it and you canperform a search entering all the search arguments. Please make sure to obtainyour own Google license key and enter it in the app.config file.

Download Source

 

Summary

The Google web service API is very easy to use. It enables you to search theInternet from within your application. Complex query terms and filteringcapabilities assure relevancy of the search results to your application needs.The Google web service is one of many other emerging ones, like Amazon?s webservice or eBay?s web service. By introducing a web service interface thesecompanies moved to a platform, enabling third parties to build solutions non topof them. For these companies an ever increasing number of requests and businesstransactions are coming through these web service interfaces. If you havecomments on this article or this topic, please contact me @ [email protected]

m . I wantto hear if you learned something new. Contact me if you have questions aboutthis topic or article.

 

About the author

Klaus Salchner has worked for 14 years in the industry, nine years in Europeand another five years in North America. As a Senior Enterprise Architect withsolid experience in enterprise software development, Klaus spends considerabletime on performance, scalability, availability, maintainability,globalization/localization and security. The projects he has been involved inare used by more than a million users in 50 countries on three continents.

Klaus calls Vancouver, British Columbia his home at the moment. His next biggoal is doing the New York marathon in 2005. Klaus is interested in guestspeaking opportunities or as an author for .NET magazines or Web sites. He canbe contacted at[email protected] or http://www.enterprise-minds.com/ .

Enterprise application architecture and design consulting services areavailable. If you want to hear more about it contact me! Involve me in yourprojects and I will make a difference for you. Contact me if you have an ideafor an article or research project. Also contact me if you want to co-author anarticle or join future research projects!

猜你喜欢

转载自androidmaster.iteye.com/blog/1443238