Example #1
0
 /// <summary>
 /// Create a Google searcher.
 /// </summary>
 /// <param name="cx">Your search engine ID from https://www.google.com/cse/ </param>
 /// <param name="hl">(optional) language</param>
 /// <param name="extraOptions">(optional)If you need to supply more custom options passed to google please include them here. These options will override any defaults</param>
 /// <param name="pageSize">(optional) Number of results per page, Default are 10 but there is a max of 20 with the XMl API</param>
 /// <param name="maxPages">(optional) Number of pages to grab. Larger number will be slower.  Also each page is a new search on your paid search limit.</param>
 /// <param name="start">The result offset, use to page through results. WARNING: CSE starts at 1 but XML starts at zero. If you want the 11th result (page 2) on CSE set this to 11. On XML set this to 10 </param>
 /// <param name="method">(optional) API Used to gather the results. Default isCSE https://developers.google.com/custom-search/json-api/v1/reference/cse/list the other option is  XML https://developers.google.com/custom-search/docs/xml_results
 /// Only the CSA interface gives promotions as a separate object. Not this is a change of default from old versions.
 /// </param>
 /// <param name="key">Api Key, only needed if you are using the CSE interface</param>
 public GoogleSearch(string cx, string hl = "en", Dictionary <string, string> extraOptions = null, int pageSize = 10, int maxPages = 20, int start = 1, GoogleSearchMethod method = GoogleSearchMethod.CSE, string key = "")
 {
     Options["client"] = "google-csbe";
     Options["output"] = "xml_no_dtd";
     Options["cx"]     = cx;
     Options["hl"]     = hl;
     Options["num"]    = pageSize.ToString(CultureInfo.InvariantCulture);
     if (start > 1)
     {
         Options["start"] = start.ToString(CultureInfo.InvariantCulture);
     }
     if (!string.IsNullOrEmpty(key))
     {
         Options["key"] = key;
     }
     _maxPages = maxPages;
     Method    = method;
     if (extraOptions == null)
     {
         return;
     }
     foreach (var option in extraOptions)
     {
         Options[option.Key] = option.Value;
     }
 }
 /// <summary>
 /// Create a Google searcher.
 /// </summary>
 /// <param name="cx">Your search engine ID from https://www.google.com/cse/ </param>
 /// <param name="hl">(optional) language</param>
 /// <param name="extraOptions">(optional)If you need to supply more custom options passed to google please include them here. These options will override any defaults</param>
 /// <param name="pageSize">(optional) Number of results per page, Default are 10, this is also the max</param>
 /// <param name="maxPages">(optional) Number of pages to grab. Larger number will be slower.  Also each page is a new search on your paid search limit. Default is 10 as this will give you the max 100 results.</param>
 /// <param name="start">The result offset, use to page through results. WARNING: CSE starts at 1 but XML starts at zero. If you want the 11th result (page 2) on CSE set this to 11. On XML set this to 10 </param>
 /// <param name="method">(optional) API Used to gather the results. Default isCSE https://developers.google.com/custom-search/json-api/v1/reference/cse/list the other option was XML but is no longer supported by google. https://developers.google.com/custom-search/docs/xml_results
 /// Only the CSE interface gives promotions as a separate object. Note this is a change of default from old versions.
 /// </param>
 /// <param name="key">Api Key , needed if you want more than 100 queries per day https://console.developers.google.com/apis/credentials </param>
 /// <param name="userIp">The Users Ip for imposing per user limits https://support.google.com/cloud/answer/7035610 </param>
 /// <param name="quotaUser">The users unique name for capping by non web request. https://support.google.com/cloud/answer/7035610 </param>
 public GoogleSearch(string cx, string key, string hl = "en", Dictionary <string, string> extraOptions = null, int pageSize = 10, int maxPages = 10, int start = 1, GoogleSearchMethod method = GoogleSearchMethod.CSE, string userIp = null, string quotaUser = null)
 {
     Options["client"] = "google-csbe";
     Options["output"] = "xml_no_dtd";
     Options["cx"]     = cx;
     Options["hl"]     = hl;
     Options["num"]    = pageSize.ToString(CultureInfo.InvariantCulture);
     if (start > 1)
     {
         Options["start"] = start.ToString(CultureInfo.InvariantCulture);
     }
     if (!string.IsNullOrEmpty(key))
     {
         Options["key"] = key;
     }
     if (!string.IsNullOrEmpty(userIp))
     {
         Options["userIP"] = key;
     }
     if (!string.IsNullOrEmpty(quotaUser))
     {
         Options["quotaUser"] = key;
     }
     _maxPages = maxPages;
     if (_maxPages * pageSize > 100)
     {
         //will crash if you ask for more than the first 100 results. This is a Google change.
         _maxPages = 100 / pageSize;
     }
     Method = method;
     if (extraOptions == null)
     {
         return;
     }
     foreach (var option in extraOptions)
     {
         Options[option.Key] = option.Value;
     }
 }
 /// <summary>
 /// Create a Google searcher. 
 /// </summary>
 /// <param name="cx">Your search engine ID from https://www.google.com/cse/ </param>
 /// <param name="hl">(optional) language</param>
 /// <param name="extraOptions">(optional)If you need to supply more custom options passed to google please include them here. These options will override any defaults</param>
 /// <param name="pageSize">(optional) Number of results per page, Default are 10 but there is a max of 20 with the XMl API</param>
 /// <param name="maxPages">(optional) Number of pages to grab. Larger number will be slower.  Also each page is a new search on your paid search limit.</param>
 /// <param name="start">The result offset, use to page through results. WARNING: CSE starts at 1 but XML starts at zero. If you want the 11th result (page 2) on CSE set this to 11. On XML set this to 10 </param>
 /// <param name="method">(optional) API Used to gather the results. Default isCSE https://developers.google.com/custom-search/json-api/v1/reference/cse/list the other option is  XML https://developers.google.com/custom-search/docs/xml_results 
 /// Only the CSA interface gives promotions as a separate object. Not this is a change of default from old versions.
 /// </param>
 /// <param name="key">Api Key, only needed if you are using the CSE interface</param>
 public GoogleSearch(string cx, string hl = "en", Dictionary<string, string> extraOptions = null, int pageSize = 10, int maxPages = 20, int start = 1, GoogleSearchMethod method = GoogleSearchMethod.CSE, string key = "")
 {
     Options["client"] = "google-csbe";
     Options["output"] = "xml_no_dtd";
     Options["cx"] = cx;
     Options["hl"] = hl;
     Options["num"] = pageSize.ToString(CultureInfo.InvariantCulture);
     if (start > 1)
     {
         Options["start"] = start.ToString(CultureInfo.InvariantCulture);
     }
     if (!string.IsNullOrEmpty(key))
     {
         Options["key"] = key;
     }
     _maxPages = maxPages;
     Method = method;
     if (extraOptions == null) return;
     foreach (var option in extraOptions)
     {
         Options[option.Key] = option.Value;
     }
 }