public SuggestionsList Suggest(string query) { AuditServiceItem auditServiceItem = AuditService.Register("SuggestionsService", "Suggest", query); AuditService.Start(auditServiceItem); SuggestionsList suggestionsList = SuggestionsCacheHelper.ReadSuggestionsList(query); if (suggestionsList == null) { try { IProviderSuggestionsService googleSuggestionsService = new GoogleSuggestionsService(); SuggestionsContext googleSuggestionsContext = new SuggestionsContext(ProviderEnum.Google, query); suggestionsList = googleSuggestionsService.GetSuggestions(googleSuggestionsContext); SuggestionsCacheHelper.CacheSuggestionList(query, suggestionsList); } catch (Exception exception) { ErrorService.Log("SuggestionsService", "Suggest", query, exception); } } AuditService.End(auditServiceItem); return suggestionsList; }
public SuggestionsList GetSuggestions(SuggestionsContext suggestionsContext) { SuggestionsList suggestionsList = new SuggestionsList(); try { string query = suggestionsContext.Query; string urlTemplate = "http://google.com/complete/search?output=toolbar&q={0}"; string url = string.Format(urlTemplate, query); XmlDocument xmlDocument = new XMLConnector().GetXMLDocument(url); ; XmlNodeList resultNodes = xmlDocument.SelectNodes("toplevel/CompleteSuggestion"); if (resultNodes != null) { foreach (XmlNode resultNode in resultNodes) { string name = null; double numberQueries = 0; XmlNode suggestionNode = resultNode.SelectSingleNode("suggestion"); if (suggestionNode != null) { XmlAttribute dataAttribute = suggestionNode.Attributes["data"]; if (dataAttribute != null) { name = dataAttribute.Value; } } XmlNode numQueriesNode = resultNode.SelectSingleNode("num_queries"); if (numQueriesNode != null) { XmlAttribute intAttribute = numQueriesNode.Attributes["int"]; if (intAttribute != null) { string numQuery = intAttribute.Value; try { numberQueries = double.Parse(numQuery); } catch (Exception) {} } } if (name != null && numberQueries > 0) { SuggestionItem suggestionItem = new SuggestionItem(name); suggestionItem.Provider = ProviderEnum.Google; suggestionItem.NumberQueries = numberQueries; suggestionItem.Weight = CalculateWeight(numberQueries); suggestionsList.Add(suggestionItem); } } } } catch (Exception exception) { ErrorService.Log("GoogleSuggestionsService", "GetSuggestions", suggestionsContext.ToString(), exception); } return suggestionsList; }