Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created September 27, 2013 10:39
Show Gist options
  • Save martinnormark/6726771 to your computer and use it in GitHub Desktop.
Save martinnormark/6726771 to your computer and use it in GitHub Desktop.
Simple C# API Client for the Google Search Suggestions API
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ConsoleApplication1
{
public class SearchSuggestionsAPI
{
/// <summary>
/// The Google Suggest search URL.
/// </summary>
/// <remarks>
/// Add gl=dk for Google Denmark. Add lr=lang_da for danish results. Add hl=da to indicate the language of the UI making the request.
/// </remarks>
private const string _suggestSearchUrl = "http://www.google.com/complete/search?output=toolbar&q={0}&hl=en";
/// <summary>
/// Gets the search suggestions from Google.
/// </summary>
/// <param name="query">The query.</param>
/// <returns>A list of <see cref="GoogleSuggestion"/>s.</returns>
public async Task<List<GoogleSuggestion>> GetSearchSuggestions(string query)
{
if (String.IsNullOrWhiteSpace(query))
{
throw new ArgumentException("Argument cannot be null or empty!", "query");
}
string result = String.Empty;
using (HttpClient client = new HttpClient())
{
result = await client.GetStringAsync(String.Format(_suggestSearchUrl, query));
}
XDocument doc = XDocument.Parse(result);
var suggestions = from suggestion in doc.Descendants("CompleteSuggestion")
select new GoogleSuggestion
{
Phrase = suggestion.Element("suggestion").Attribute("data").Value
};
return suggestions.ToList();
}
}
/// <summary>
/// Encapsulates a suggestion from Google.
/// </summary>
public class GoogleSuggestion
{
/// <summary>
/// Gets or sets the phrase.
/// </summary>
/// <value>The phrase.</value>
public string Phrase { get; set; }
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return this.Phrase;
}
}
}
@geniovi
Copy link

geniovi commented Feb 18, 2014

hello, if i search two or more words not it works this class

@amjadislamamjad
Copy link

is there anyone can tell me how to use this in C#

@darkn1gh7
Copy link

:|

@DineshSolanki
Copy link

is there anyone can tell me how to use this in C#

https://github.com/DineshSolanki/SearchSuggestionsAPI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment