Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development

  • Print
  • + Share This
  • 💬 Discuss
Like this article? We recommend

A .NET Google Fight

One of the more entertaining—and, frankly, more useless—applications on the web to utilize Google is Google Fight, where you enter two search terms and click a button to have them "fight it out," with the winner being the term that gets the most hits. Using the Google Web API, this app is trivial to replicate.

First, let's throw together a simple (read: ugly) user interface to allow a user to enter two search terms and start the fight (see Figure 1).

Figure 1

Figure 1 A simple GUI in Visual Studio.

The next thing we need to do is get access to the Google interface. This is done in one of two ways:

  • Run wsdl.exe on GoogleSearch.wsdl, the WSDL file that ships with the API. wsdl.exe takes the file and generates a C# class that can be used directly in your apps.
  • In Visual Studio, choose Project, Add Web Reference, and follow the instructions to locate and include the web reference.

Regardless of which approach you choose, the result is a .NET class against which you can make direct calls that will be transmitted to Google via SOAP.

With that class included, all we need to do is fill in the code to perform the search when the user clicks the Fight! button:

private void searchButton_Click(object sender, System.EventArgs e)
{
    //Get text from our search boxes
    String searchOne = leftSearchBox.Text;
    String searchTwo = rightSearchBox.Text;

    //Create the search object and run the searches
    GoogleSearchService googleSearch = new GoogleSearchService();
    GoogleSearchResult resultOne =
       googleSearch.doGoogleSearch(KEY, searchOne,
        0, 1, false, "", false, "", "", "");
    GoogleSearchResult resultTwo =
       googleSearch.doGoogleSearch(KEY, searchTwo,
        0, 1, false, "", false, "", "", "");

    //Count and display the hits
    int hitsOne = resultOne.estimatedTotalResultsCount;
    int hitsTwo = resultTwo.estimatedTotalResultsCount;
    leftResultsLabel.Text = Convert.ToString(hitsOne);
    rightResultsLabel.Text = Convert.ToString(hitsTwo);

    //Pick and display the winner
    if(hitsOne > hitsTwo)
        winnerBox.Text = leftSearchBox.Text;
    else
        winnerBox.Text = rightSearchBox.Text;
}

There are a few important classes to know about. The first is GoogleSearchService, the class generated from the WSDL file providing access to Google's services. The code calls doGoogleSearch() twice to receive two GoogleSearchResults. Let's take a closer look at the doGoogleSearch() call:

googleSearch.doGoogleSearch(key, query, startIndex, maxResults,
   useFilter, restrict, useSafeSearch, languageRestrict,
    inputEncoding, outputEncoding);

Term

Description

key

The unique key that you receive when you register with Google. This key identifies you and ensures that your application performs a maximum of 1,000 searches per day.

query

The search term. This string can use any of the advanced features of Google search that you can access via the Google search box (including date filtering, searching within specific domains, and so on).

startIndex

Indicates which result should be first in the list. Because you can receive a maximum of 10 results per search, you must manually retrieve the next 10 by doing a second search, setting the start index at 10.

maxResults

The maximum number to return; 10 is the maximum allowable.

useFilter

Uses filtering to remove similar pages from the search results.

restrict

The name of one of a few Google search areas to which you want to restrict your search, such as "Linux".

useSafeSearch

Indicates whether to use SafeSearch filtering to restrict mature-oriented results.

languageRestrict

One or more language codes restricting the results to the specified language(s).

inputEncoding, outputEncoding

(Deprecated) Filtered the input and outputs to specific encodings, but these are no longer used. You should provide UTF-8 and expect it back.

The GoogleSearchResult objects returned from this call give us access to both the first set of results of our search (only one result has been retrieved here), and to info about the search itself. For this example, we can ignore the results; all we need to know is the number of search results, received via the estimatedTotalResultsCount field.

By comparing the two counts, we determine the winner (see Figure 2).

Figure 2

Figure 2 In the battle of who gets the most hits, .NET is the victor.

Simple! Now let's move on to something a little more complicated.

  • + Share This
  • 🔖 Save To Your Account

Discussions

comments powered by Disqus