Pages

Monday, March 21, 2011

Google Search API & Bing Search API in C#

Hi all,
Making use of the Google search or Bing search in our business application is a very common and general task, with the help of these search API's we can make our application very effective and user friendly. The 2 top search engine that are Google and Bing has provided API that can be easily used in our application. What is required is a class that can handle the response from these search engine. here I am going to explain the general search result class that can be used to accommodate both the search engines. here is the sample code for the same.

public class result
{
public string url;
public string title;
public string content;
public Engine engine;

public enum Engine { bing,google };

public Result(string url, string title, string content, Engine engine)
{
this.url = url;
this.title = title;
this.content = content;
this.engine = engine;
}
}



this is a sample Wrapper for the result handling. Class having a constructor that can initialize all its member, having normal member variables like URL, Content, Engine and the Title. Both the search engines result contains all these field values so I have simply created a warper using the above fields..

Now Lets c the main API accommodation of the Google search API

public class result
{
public string url;
public string title;
public string content;
public FindingEngine engine;

public enum Engine { bing,google };

public Result(string url, string title, string content, Engine engine)
{
this.url = url;
this.title = title;
this.content = content;
this.engine = engine;
}public static List<Result> GoogleSearch(string expression,
Dictionary<string, object> stats_dict)
{
var template = "http://mannpandey.blogspot.com";

Uri search_url;
var results_list = new List<SearchResult>();

int[] offsets = { 0, 8, 16, 24, 32, 40, 48 };

foreach (var offset in offsets)
{
searchuri = new Uri(string.Format(template, expression, offset));

var page = new WebClient().DownloadString(searchuri);

JObject obj = (JObject) JsonConvert.DeserializeObject(page);

var results =
from result in obj["responseData"]["results"].Children()
select new SearchResult(
url: result.Value<string>("url").ToString(),
title: result.Value<string>("title").ToString(),
content: result.Value<string>("content").ToString(),
engine: SearchResult.FindingEngine.google
);

foreach (var result in results)
results_list.Add(result);
}

return results_list;
}
}


In this class we are simply passing the search expression and the dictionary object. Where the URL is passed and the response coming from the API is deserialize using the JSON deserialization.

What ever member variable we have used in the above class are coming from the APi result. like TITLE, CONTENT etc. The de serilizer can be found in the JSOn library that is Json.NET .

Here is the API accommodation for the Bing search

public static List<SearchResult> BingSearch(string expression, Dictionary<string, object> stats_dict)
{
var template = "http://mannpandey.blogspot.com";

var offset_template = "&Web.Offset={1}";
var listresult= new List<Result>();
Uri search_url;
int[] offsets = { 0, 50, 100, 150 };

foreach (var offset in offsets)
{
if (offset == 0)
searchurl = new Uri(string.Format(template, search_expression));
else
searchurl = new Uri(string.Format(template + offset_template, expression, offset));

var page = new WebClient().DownloadString(searchurl);

JObject obj = (JObject)JsonConvert.DeserializeObject(page);

var results_query =
from result in obj["SearchResponse"]["Web"]["Results"].Children()
select new SearchResult
(
url: result.Value<string>("Url").ToString(),
title: result.Value<string>("Title").ToString(),
content: result.Value<string>("Description").ToString(),
engine: SearchResult.FindingEngine.bing
);

foreach (var result in results_query)
listresults.Add(result);
}

return listresult;
}


Bing search API is also similar to the Google API except some difference in there Offset, here also I am making use of the JSON serializer because its very easy to use and can be found in our .net library..


Now you can easily create the object of these classes or put them under separate project that can be used as a DLL. The idea for the same article was taken from one of the popular publishing site "Oreilly", that publishes so many books in the C# and ASP. You can found the details here. URL

I Hope you like this article, if you have any Query please feel free to contact me.. Thanks.

Anil Kumar Pandey
MVP (Visual C# 2009-2010)

Kontera