Example #1
0
        protected List <Result> Get_Results(SearchObject.Rootobject searchObj)
        {
            //Extract all of the necessary result specific data from the search result object
            List <Result> value = new List <Result>();

            try
            {
                if (searchObj != null)
                {
                    if (searchObj.items != null)
                    {
                        foreach (SearchObject.Item item in searchObj.items)
                        {
                            string src = string.Empty;
                            if (item.pagemap != null)
                            {
                                if (item.pagemap.cse_image != null)
                                {
                                    if (item.pagemap.cse_image.Length > 0)
                                    {
                                        src = item.pagemap.cse_image[0].src;
                                    }
                                }
                            }
                            value.Add(new Result(item.title, item.link, item.displayLink, item.snippet, src));
                        }
                    }
                }
            }
            catch
            {
                return(new List <Result>());
            }

            return(value);
        }
Example #2
0
        protected SearchViewModel PerformSearch(SearchViewModel Search)
        {
            try
            {
                if (TryValidateModel(Search))
                {
                    if (!string.IsNullOrWhiteSpace(Search.SearchQuery))
                    {
                        List <string> searchTerms   = new List <string>(Search.SearchQuery.Split(" "));
                        string        excludedTerms = string.Empty;
                        string        includedTerms = string.Empty;
                        string        searchQuery   = string.Empty;
                        foreach (string term in searchTerms)
                        {
                            if (term.StartsWith('-'))
                            {
                                excludedTerms += $"+{term.Substring(1)}";
                            }
                            else if (term.StartsWith('+'))
                            {
                                includedTerms += $"+{term.Substring(1)}";
                            }
                            else
                            {
                                searchQuery += $"+{term}";
                            }
                        }
                        if (!string.IsNullOrWhiteSpace(excludedTerms))
                        {
                            excludedTerms = excludedTerms.Substring(1);
                        }
                        if (!string.IsNullOrWhiteSpace(includedTerms))
                        {
                            includedTerms = includedTerms.Substring(1);
                        }
                        if (!string.IsNullOrWhiteSpace(searchQuery))
                        {
                            searchQuery = searchQuery.Substring(1);
                        }
                        string query = $"{GetApiString(Search.api)}{searchQuery}&start={Search.Index*10 +1}";
                        if (!string.IsNullOrWhiteSpace(includedTerms))
                        {
                            query += $"&exactTerms={includedTerms}";
                        }
                        if (!string.IsNullOrWhiteSpace(excludedTerms))
                        {
                            query += $"&excludeTerms={excludedTerms}";
                        }

                        string json = new WebClient().DownloadString(query);
                        SearchObject.Rootobject searchObj = JsonConvert.DeserializeObject <SearchObject.Rootobject>(json);
                        Search.Results      = Get_Results(searchObj);
                        Search.TotalResults = Convert.ToInt32(searchObj.searchInformation.totalResults);
                        Search.SetMaxPages();
                    }
                }
            }
            catch
            {
                Search = new SearchViewModel();
            }
            return(Search);
        }