コード例 #1
0
        /// <summary>
        /// Performs a search using the query parameters specified.
        /// </summary>
        /// <param name="queryParameters">A Google.CustomSearch.QueryParameters that specifies the search to be performed.</param>
        /// <returns>Returns an Google.CustomSearch.SearchResult containing the results of the search.</returns>
        public SearchResult Search(QueryParameters queryParameters)
        {
            Uri requestUri = this.FormatRequest(queryParameters);

            Debug.WriteLine(string.Format("Request Google : {0}", requestUri.ToString()));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            return this.ProcessResponse(queryParameters, response);
        }
コード例 #2
0
        /// <summary>
        /// Processes the HTTP Response from Google CSE and produces a 
        /// Google.CustomSearch.SearchResult containing the supplied data.
        /// </summary>
        /// <param name="queryParameters">The parameters which were used to perform the query.</param>
        /// <param name="response">The HttpWebResponse recieved from Google CSE.</param>
        /// <returns>Returns a SearchResult containing the search results.</returns>
        private SearchResult ProcessResponse(QueryParameters queryParameters, HttpWebResponse response)
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new WebException("The request was borked");
            }

            SearchResult retval = new SearchResult(queryParameters);
            new ResponseParser().Parse(retval, response.GetResponseStream());

            return retval;
        }
コード例 #3
0
        /// <summary>
        /// Performs an asynchronous search using the query parameters specified.
        /// On completion the AsyncCallback specified as callback is notified.
        /// </summary>
        /// <param name="queryParameters">A Google.CustomSearch.QueryParameters that specifies the search to be performed.</param>
        /// <param name="callback">A System.AsyncCallback object that will be notified when the call completes.</param>
        /// <returns>Returns a System.IAsyncResult that can be used to monitor the status of the asynchronous call.</returns>
        public IAsyncResult SearchAsync(QueryParameters queryParameters, AsyncCallback callback)
        {
            AsyncSearch caller = new AsyncSearch(this.Search);

            // TODO: What should be passed as the state object? is the Filter.Label appropriate?
            return caller.BeginInvoke(queryParameters, callback, queryParameters.Filter.Label);
        }
コード例 #4
0
        /// <summary>
        /// Generates the Uri to be used for the request to Google CSE.
        /// </summary>
        /// <param name="input">The query parameters to format in the URL.</param>
        /// <returns>Returns a System.Uri to request the search results from Google CSE.</returns>
        private Uri FormatRequest(QueryParameters input)
        {
            StringBuilder url = new StringBuilder(GOOGLE_URL);
            Dictionary<string, string> queryParameters = new Dictionary<string, string>();

            queryParameters.Add("cx", this.CseID);
            queryParameters.Add("client", "google-csbe");
            queryParameters.Add("q", input.SearchTerm);

            if (null != input.SpecialQueryTerms.BackLinks)
            {
                // Note: You cannot specify any other query terms when using link:
                queryParameters["p"] = string.Format("link:{0}", input.SpecialQueryTerms.BackLinks.ToString());
            }
            else
            {
                if (input.Filter != null)
                {
                    queryParameters["q"] += "+more:" + input.Filter.Label;
                }

                foreach (string word in input.SpecialQueryTerms.InUrl)
                {
                    queryParameters["q"] += "+inurl:" + word;
                }

                queryParameters.Add("num", input.Count.ToString());
                queryParameters.Add("start", input.Start.ToString());

                if (OutputFormat.Xml == input.Format)
                {
                    queryParameters.Add("output", "xml");
                }
                else
                {
                    queryParameters.Add("output", "xml_no_dtd");
                }

                // Safe search defaults to off.
                if (SafeSearch.Off != input.Safe)
                {
                    string value = "medium";
                    if (SafeSearch.High == input.Safe)
                    {
                        value = "high";
                    }
                    queryParameters.Add("safe", value);
                }

                // Auto filter defaults to true
                if (!input.AutoFilter)
                {
                    queryParameters.Add("filter", "0");
                }

                // Special search options.

                // Exclude Query Terms
                foreach (string term in input.SpecialQueryTerms.ExcludeQueryTerms)
                {
                    queryParameters["q"] += string.Format(" -{0}", term);
                }

                // File type filtering
                bool firstType = true;
                foreach (var pair in input.FileTypes)
                {
                    string combinator = " OR";
                    string type = string.Format(" filetype:{0}", pair.Key.TrimStart('.'));
                    if (!pair.Value)
                    {
                        type = " -" + type.TrimStart();
                        combinator = " AND";
                        // TODO: This logic is not perfect, do the terms need encapsulating in parens?
                        // What happens when some are ANDs and some need to be OR'd ?
                    }
                    if (!firstType) type = combinator + type;
                    queryParameters["q"] += type;
                    firstType = false;
                }

                // Advanced search options.
                if (null != input.AdvancedSearchSite)
                {
                    queryParameters.Add("as_sitesearch", input.AdvancedSearchSite.ToString());
                }

                if (null != input.AdvancedSearchSite)
                {
                    string value = "i";
                    if (DomainInclusion.Exclude == input.AdvancedSearchDomainInclusion)
                    {
                        value = "e";
                    }

                    queryParameters.Add("as_dt", value);
                }

                if (!string.IsNullOrEmpty(input.AdvancedSearchPhraseQuery))
                {
                    queryParameters.Add("as_epq", input.AdvancedSearchPhraseQuery);
                }

                if (!string.IsNullOrEmpty(input.AdvancedSearchExcludePhraseQuery))
                {
                    queryParameters.Add("as_eq", input.AdvancedSearchExcludePhraseQuery);
                }

                if (null != input.AdvancedSearchRequiredLink)
                {
                    queryParameters.Add("as_lq", input.AdvancedSearchRequiredLink.ToString());
                }

                // Sort options
                if (input.Sort.Type != Sort.SortType.Relevance) // Sort by relevance is the Google default sort, so no need to explicitly specify the sort
                {
                    string qs = null;
                    if (input.Sort.Type == Sort.SortType.Date)
                    {
                        qs = "date";
                        qs += (input.Sort.Direction == Sort.SortDirection.Ascending ? ":a" : ":d");
                    }
                    if (input.Sort.Type == Sort.SortType.Attribute)
                    {
                        qs = input.Sort.Attribute;
                        qs += (input.Sort.Direction == Sort.SortDirection.Ascending ? ":a" : ":d");
                        switch (input.Sort.Strength)
                        {
                            case Sort.SortStrength.Strong: qs += ":s"; break;
                            case Sort.SortStrength.Weak:   qs += ":w"; break;
                            default:                       qs += ":h"; break;
                        }
                    }
                    if (!string.IsNullOrEmpty(qs))
                        queryParameters.Add("sort", qs);
                }
            }

            foreach (var pair in queryParameters)
            {
                url.Append(pair.Key + "=" + HttpUtility.UrlEncode(pair.Value) + "&");
            }

            return new Uri(url.ToString().TrimEnd('&'));
        }
コード例 #5
0
ファイル: Facet.cs プロジェクト: musakkhir/cse-net
 /// <summary>
 /// Initializes a new instance of the Google.CustomSearch.Facet class specifying the 
 /// QueryParameters that were used to generate the search to which this Facet is related.
 /// </summary>
 /// <param name="parameters">The QueryParameters to which this Facet is related.</param>
 public Facet(QueryParameters parameters)
 {
     this.Parameters = parameters;
 }