Skip to content

Instantly share code, notes, and snippets.

@scorta
Created August 30, 2013 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scorta/6385955 to your computer and use it in GitHub Desktop.
Save scorta/6385955 to your computer and use it in GitHub Desktop.
public struct SearchType
{
public string url;
public string title;
public string content;
public FindingEngine engine;
public enum FindingEngine { Google, Bing, GoogleAndBing };
}
public interface ISearchResult
{
SearchType.FindingEngine Engine { get; set; }
string SearchExpression { get; set; }
List<SearchType> Search();
}
public class GoogleSearch : ISearchResult
{
public GoogleSearch(string searchExpression)
{
this.Engine = SearchType.FindingEngine.Google;
this.SearchExpression = searchExpression;
}
public SearchType.FindingEngine Engine { get; set; }
public string SearchExpression { get; set; }
public List<SearchType> Search()
{
const string urlTemplate = @"http://ajax.googleapis.com/ajax/services/search/web?v=1.0& \
rsz=large&safe=off&q={0}&start={1}&gl=vn&hl=vi&userip=113.190.242.149";
var resultsList = new List<SearchType>();
int[] offsets = { 0, 8, 16, 24, 32, 40, 48 };
foreach (var offset in offsets)
{
var searchUrl = new Uri(string.Format(urlTemplate, SearchExpression, offset));
var page = new WebClient().DownloadString(searchUrl);
var o = (JObject)JsonConvert.DeserializeObject(page);
var resultsQuery =
from result in o["responseData"]["results"].Children()
select new SearchType
{
url = result.Value<string>("url").ToString(),
title = result.Value<string>("title").ToString(),
content = result.Value<string>("content").ToString(),
engine = this.Engine
};
resultsList.AddRange(resultsQuery);
}
return resultsList;
}
}
ISearchResult searchClass = new GoogleSearch("hosting");
var list = searchClass.Search();
foreach (var searchType in list)
{
//Console.WriteLine(searchType.title);
textBox1.Text += searchType.title + Environment.NewLine;
textBox1.Text += searchType.url + Environment.NewLine;
textBox1.Text += searchType.content + Environment.NewLine;
textBox1.Text += Environment.NewLine;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment