//, string iExcludeTerms) public IList<Google.Apis.Customsearch.v1.Data.Result> Search(string iMatchExact, string iQuery) { //string query = "4-way active inactive ingredients"; var service = new Google.Apis.Customsearch.v1.CustomsearchService( new BaseClientService.Initializer { ApiKey = GoogleAPIKey }); var listRequest = service.Cse.List(iQuery); listRequest.Cx = CustomSearchEngineID; listRequest.ExactTerms = iMatchExact; //listRequest.ExcludeTerms = iExcludeTerms; listRequest.Num = 10; IList<Google.Apis.Customsearch.v1.Data.Result> results = null; try { var search = listRequest.Execute(); results = search.Items; } catch (Exception ex) { //TODO: Log the error } return results; }
private void SearchGoogle() { HideNoResults(); string apiKey = "AIzaSyBdOQTWftGb-DFF_Y-uJCcy8wgRLLmnx3E"; string cx = "015695191635768647286:alyjydblwfs"; var svc = new Google.Apis.Customsearch.v1.CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey }); var listRequest = svc.Cse.List(searchInput); // ListRequest num = 10 - default(Range 1-10). listRequest.Cx = cx; listRequest.SearchType = CseResource.ListRequest.SearchTypeEnum.Image; var search = listRequest.Execute(); if (search != null && search.Items != null) { foreach (var result in search.Items) { if (result.Link != null && result.Link != "") { imageSources.Add(result.Link); } } LoadImages(); } else { NoResults(); } }
public string[] NewsFocus(string[] topics) { for (int j = 0; j < topics.Length; j++) { if (String.IsNullOrEmpty(query)) { query = topics[j] + " "; } else { query += topics[j] + " "; } } string apiKey = "AIzaSyAdGYEvLJhlTZQ-Uxo_9IcoPuxwvbOcKV4"; string cseKey = "018422199925324649720:fcnzsnkhzhu"; var bcsi = new BaseClientService.Initializer { ApiKey = apiKey }; var css = new Google.Apis.Customsearch.v1.CustomsearchService(bcsi); var listRequest = css.Cse.List(query); listRequest.Cx = cseKey; var search = listRequest.Execute(); for (int k = 0; k < 10; k++) { i[k] = search.Items[k].Link; } return(i); }
public List<WebSearchResult> SearchQuery(string query) { try { if (!string.IsNullOrEmpty(query)) { var svc = new Google.Apis.Customsearch.v1.CustomsearchService(new BaseClientService.Initializer { ApiKey = apiKey }); var listRequest = svc.Cse.List(query); listRequest.Cx = cx; // Google accept only 1 query per 1 second var timeDiff = DateTime.Now - LAST_QUERY_TIME; if (timeDiff != null && timeDiff.TotalSeconds < 1) { Thread.Sleep(Convert.ToInt32(timeDiff.TotalMilliseconds)); } Search search = listRequest.Execute(); LAST_QUERY_TIME = DateTime.Now; string searchCount = (search != null && search.Items != null) ? search.Items.Count.ToString() : "0"; log.InfoFormat("[SearchQuery] query={0}, search.Items.Count={1}.", query, searchCount); return GoogleResultToWebSearchResult(search); } else { log.ErrorFormat("[SearchQuery] query is null or empty."); return null; } } catch (Exception e) { log.ErrorFormat("[SearchQuery] Exception={0}.", e.Message); return null; } }
public CseResource(CustomsearchService service) { this.service = service; }