Searching Google From C#

We have a client that may be needing this soon and, since the old way of calling a Google search web service is going away, I thought I would throw together a class that performs the work in the new manner which is to use the Google AJAX Search API. Make sure you follow their terms which are more limiting than before.

First of all, the new API uses REST to invoke the search and returns the results as JSON. So there would be a couple more things to learn. However I’ve used a JSON library I’ve found on CodeProject that makes things easy: http://www.codeproject.com/KB/recipes/JSON.aspx (Thanks Andy Kernahan).  I added this project to a solution that will be referenced by two new classes I’ve written – a GoogleResults class for doing the search and a class for each single result called GoogleResult (singular). I won’t go into the single result class here since it just exposes the data returned by the search as an object with properties.

My GoogleResults class has one static method called GetResults.  It’s called like this:

string query = "billupton";
List results = GoogleResults.GetResults(query);
foreach (GoogleResult result in results)
{
    //...Do something with the result here...
}

Click the new Google AJAX Search API link to see all the parms you can put on the search. In my example I’m paging through results until I do not get a status of 200 (a better way might limit the page count to pull back).  Here’s what the code looks like:

//property to enforce consistent calling of the search api
private static string _searchUrl = 
   "http://ajax.googleapis.com/ajax/services/search/web?" +
   "v=1.0&filter=1&rsz=large&start={0}&q={1}";


/// 
/// Static function to get results from Google.
/// 
/// 
/// 
public static List GetResults(string query)
{
    List list = new List();
    int curRec = 0;
    JsonObject account;
    JsonObject responseData;
    JsonNumber responseStatus;

    //Retrieve in chunks of 8 until we get a bad status code.
    WebRequest request = WebRequest.Create(
        string.Format(_searchUrl, curRec, query));
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(
        response.GetResponseStream());
    string json = reader.ReadLine();
    reader.Close();

    //Parse it into usable objects
    using (JsonParser parser = new JsonParser(new 
        StringReader(json), true))
    {
        account = parser.ParseObject();
        responseStatus = (JsonNumber)account["responseStatus"];
    }

    while (responseStatus == 200)
    {
        responseData = (JsonObject)account["responseData"];
        JsonObject responseCursor = 
            (JsonObject)responseData["cursor"];
        JsonString estimatedCount = 
            (JsonString)responseCursor["estimatedResultCount"];
        JsonArray results = (JsonArray)responseData["results"];
        foreach (JsonObject result in results)
        {
            list.Add(new GoogleResult(result));
        }

        //Keep reading pages until there are no more
        curRec += 8;
        request = WebRequest.Create(string.Format(_searchUrl, 
            curRec, query));
        response = request.GetResponse();
        reader = new StreamReader(
            response.GetResponseStream());
        json = reader.ReadLine();
        reader.Close();

        using (JsonParser parser = new JsonParser(new 
            StringReader(json), true))
        {
            account = parser.ParseObject();
            responseStatus = 
                (JsonNumber)account["responseStatus"];
        }
    }
    return list;
}
Advertisements

About this entry