CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Yahoo LocalSearchHelper for WinRT

783 Views   
/// <summary>Search Yahoo Local</summary> /// <see cref="http://developer.yahoo.com/search/local/V3/localSearch.html"/> public class LocalSearchHelper { private string YahooKey; public LocalSearchHelper(string yahooKey) { this.YahooKey = yahooKey; } /// <summary>Search Yahoo Local</summary> /// <param name="query">The query to search for. Using a query of "*" returns all values that match other criteria in the search (category, radius, and so on). </param> /// <param name="results">The number of results to return. </param> /// <param name="start">The starting result position to return (1-based). </param> /// <param name="radious">How far (in miles) from the specified location to search for the query terms. The default radius varies according to the location given.</param> /// <param name="latitude">The latitude of the starting location. If both latitude and longitude are specified, they will take priority over all other location data. If only one of latitude or longitude is specified, both will be ignored.</param> /// <param name="longitude">The longitude of the starting location. If both latitude and longitude are specified, they will take priority over all other location data. If only one of latitude or longitude is specified, both will be ignored.</param> /// <see cref="http://developer.yahoo.com/javascript/json.html">Using JSON with Yahoo Services</see> /// <returns></returns> public async Task<List<Result>> Search(string query, double? latitude, double? longitude, int results = 10, int start = 1, float? radius = null) { if (!latitude.HasValue || !longitude.HasValue) throw new ArgumentException("latitude and longitude are required"); var _Url = "http://local.yahooapis.com/LocalSearchService/V3/localSearch?output=json&"; _Url += string.Format("appid={0}&", System.Uri.EscapeDataString(YahooKey.ToString())); _Url += (string.IsNullOrWhiteSpace(query)) ? string.Empty : string.Format("query={0}&", System.Uri.EscapeDataString(query)); _Url += string.Format("results={0}&", System.Uri.EscapeDataString(results.ToString())); _Url += string.Format("start={0}&", System.Uri.EscapeDataString(start.ToString())); _Url += (!radius.HasValue) ? string.Empty : string.Format("radius={0}&", System.Uri.EscapeDataString(radius.ToString())); _Url += string.Format("latitude={0}&", System.Uri.EscapeDataString(latitude.ToString())); _Url += string.Format("longitude={0}&", System.Uri.EscapeDataString(longitude.ToString())); _Url = _Url.TrimEnd('&'); string _JsonString = string.Empty; try { // fetch from rest service var _HttpClient = new System.Net.Http.HttpClient(); var _HttpResponse = await _HttpClient.GetAsync(_Url); _JsonString = await _HttpResponse.Content.ReadAsStringAsync(); // check for error if (_JsonString.Contains("{\"Error\":")) throw new InvalidResultException { JSON = _JsonString }; // deserialize json to objects var _JsonBytes = Encoding.Unicode.GetBytes(_JsonString); using (MemoryStream _MemoryStream = new MemoryStream(_JsonBytes)) { var _JsonSerializer = new DataContractJsonSerializer(typeof(RootObject)); var _Result = (RootObject)_JsonSerializer.ReadObject(_MemoryStream); return _Result.ResultSet.Result; } } catch (InvalidSomethingException) { throw; } catch (Exception e) { throw new InvalidSomethingException(e) { JSON = _JsonString }; } } public class InvalidSomethingException : Exception { public InvalidSomethingException() { } public InvalidSomethingException(Exception e) : base(string.Empty, e) { } public string JSON { get; set; } } public class InvalidCredentialsException : InvalidSomethingException { } public class InvalidResultException : InvalidSomethingException { } public class Rating { public string AverageRating { get; set; } public string TotalRatings { get; set; } public string TotalReviews { get; set; } public string LastReviewDate { get; set; } public string LastReviewIntro { get; set; } } public class Categories { public object Category { get; set; } } public class Result { public string id { get; set; } public string Title { get; set; } public string Address { get; set; } public string City { get; set; } public string State { get; set; } public string Phone { get; set; } public string Latitude { get; set; } public string Longitude { get; set; } public Rating Rating { get; set; } public string Distance { get; set; } public string Url { get; set; } public string ClickUrl { get; set; } public string MapUrl { get; set; } public Categories Categories { get; set; } public string BusinessUrl { get; set; } public string BusinessClickUrl { get; set; } } public class ResultSet { public string totalResultsAvailable { get; set; } public string totalResultsReturned { get; set; } public string firstResultPosition { get; set; } public string ResultSetMapUrl { get; set; } public List<Result> Result { get; set; } } public class RootObject { public ResultSet ResultSet { get; set; } } }
by Jerry Nixon
  June 14, 2012 @ 1:29pm
Tags:
Add a comment


Report Abuse
brought to you by:
West Wind Techologies