Class: GoogleBuilder
- Inherits:
-
Object
- Object
- GoogleBuilder
- Defined in:
- lib/scrapedo/google_builder.rb
Constant Summary collapse
- CONFIG_PATH =
Pathname.new(__dir__ || ".").join("config/google_builder").
- FILE_MAP =
{ time_period: "time_period.yml", hl: "host_language.yml", lr: "language_restrict.yml", gl: "geo_location.yml", cr: "country_restrict.yml", google_domain: "domain.yml" }.freeze
Instance Method Summary collapse
-
#cr(country) ⇒ Object
(also: #country_restrict)
Country Restrict applies strict country filtering.
-
#device(device) ⇒ Object
Device type for SERP layout.
-
#disable_filter ⇒ Object
Disables "Similar Results" and "Omitted Results" filters.
-
#domain(domain) ⇒ Object
(also: #google_domain)
Google domain to query.
-
#enable_nfpr ⇒ Object
Enables Google's automatic spelling correction.
-
#gl(location) ⇒ Object
(also: #geo_location)
Geo Location (datacenter country).
-
#hl(language) ⇒ Object
(also: #host_language)
Host language of the Google UI.
-
#include_html ⇒ Object
Result includes the raw Google HTML.
-
#initialize(token, all_params: false) ⇒ GoogleBuilder
constructor
Initializes a new instance of GoogleBuilder.
- #inspect ⇒ Object
-
#location(location) ⇒ Object
Location name in Google's canonical format.
-
#lr(language) ⇒ Object
(also: #language_restrict)
Language Restrict applies strict language filtering.
- #method_missing(name, *args, &block) ⇒ Object
-
#next ⇒ JSON
Gets next page if result has next page.
-
#next? ⇒ Boolean
Returns
trueif result have more pages; otherwisefalse. -
#params(*params) ⇒ Object
Set any parameter from Google Search API.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#safe_search ⇒ Object
Send active to filter adult content from results.
-
#scrapedo_url ⇒ Object
Returns the URL to be used for the request.
-
#search(query) ⇒ Object
Search query.
-
#start(start = 0) ⇒ JSON
Start scraping from a specific offset.
-
#time(time_period) ⇒ Object
Time Period limits results to a specific recency window.
-
#uule(uule) ⇒ Object
Google UULE-encoded location string.
Constructor Details
#initialize(token, all_params: false) ⇒ GoogleBuilder
Initializes a new instance of GoogleBuilder
22 23 24 25 26 27 |
# File 'lib/scrapedo/google_builder.rb', line 22 def initialize(token, all_params: false) @has_next = false @data = { token: token } @params_cache = {} @all_params = all_params end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/scrapedo/google_builder.rb', line 254 def method_missing(name, *args, &block) source_type, config_data = find_config_source(name) if source_type define_singleton_method(name) do @data[source_type] = config_data[name] self end return send(name) end super end |
Instance Method Details
#cr(country) ⇒ Object Also known as: country_restrict
Country Restrict applies strict country filtering. Only results originating from the specified country are returned. Supports 240+ countries. See Country Restrict Parameter.
This can also use GoogleBuilder#cr_country. For example:
- GoogleBuilder#cr("countryUS") is same as GoogleBuilder#cr_us and GoogleBuilder#cr_united_states.
- GoogleBuilder#cr("countryAL") is same as GoogleBuilder#cr_al and GoogleBuilder#cr_albania. (For this one, all_params must be true)
GoogleBuilder#country_restrict is an alias for GoogleBuilder#cr.
182 183 184 185 |
# File 'lib/scrapedo/google_builder.rb', line 182 def cr(country) @data[:cr] = country self end |
#device(device) ⇒ Object
Device type for SERP layout. See General Parameters. Accepted values: desktop, mobile, default is desktop.
This can also use GoogleBuilder##device. For example:
- GoogleBuilder#device("desktop") is same as GoogleBuilder#desktop.
- GoogleBuilder#device("mobile") is same as GoogleBuilder#mobile.
56 57 58 59 |
# File 'lib/scrapedo/google_builder.rb', line 56 def device(device) @data[:device] = device if %w[desktop mobile].include?(device) self end |
#disable_filter ⇒ Object
Disables "Similar Results" and "Omitted Results" filters.
201 202 203 204 |
# File 'lib/scrapedo/google_builder.rb', line 201 def disable_filter @data[:filter] = false self end |
#domain(domain) ⇒ Object Also known as: google_domain
Google domain to query. Prefixes [https://], [http://], and [www.] are automatically stripped. Default is google.com. Supports 84 regional domains. See Supported Google Domains.
This can also use GoogleBuilder#domain_#location. For example:
- GoogleBuilder#domain("google.com") is same as GoogleBuilder#domain_united_states.
GoogleBuilder#google_domain is an alias for GoogleBuilder#domain.
125 126 127 128 |
# File 'lib/scrapedo/google_builder.rb', line 125 def domain(domain) @data[:google_domain] = domain self end |
#enable_nfpr ⇒ Object
Enables Google's automatic spelling correction.
195 196 197 198 |
# File 'lib/scrapedo/google_builder.rb', line 195 def enable_nfpr @data[:nfpr] = true self end |
#gl(location) ⇒ Object Also known as: geo_location
Geo Location (datacenter country). Determines from which country's perspective results are ranked and returned. Default is us. Supports 240+ countries. ISO 3166-1 alpha-2 codes. See Country Parameter.
This can also use GoogleBuilder#gl_#location. For example:
- GoogleBuilder#gl("us") is same as GoogleBuilder#gl_us and GoogleBuilder#gl_united_states.
- GoogleBuilder#gl("cl") is same as GoogleBuilder#gl_cl and GoogleBuilder#gl_chile. (For this one, all_params must be true)
GoogleBuilder#geo_location is an alias for GoogleBuilder#gl.
110 111 112 113 |
# File 'lib/scrapedo/google_builder.rb', line 110 def gl(location) @data[:gl] = location self end |
#hl(language) ⇒ Object Also known as: host_language
Host language of the Google UI. Default is en. Supports 150+ languages. ISO 639-1 codes. See Language Parameter.
This can also use GoogleBuilder#hl_language. For example:
- GoogleBuilder#hl("en") is same as GoogleBuilder#hl_en and GoogleBuilder#hl_english.
- GoogleBuilder#hl("ach") is same as GoogleBuilder#hl_ach and GoogleBuilder#hl_luo. (For this one, all_params must be true)
GoogleBuilder#host_language is an alias for GoogleBuilder#hl.
94 95 96 97 |
# File 'lib/scrapedo/google_builder.rb', line 94 def hl(language) @data[:hl] = language self end |
#include_html ⇒ Object
Result includes the raw Google HTML. Useful for debugging and custom parsing. See General Parameters.
79 80 81 82 |
# File 'lib/scrapedo/google_builder.rb', line 79 def include_html @data[:include_html] = true self end |
#inspect ⇒ Object
270 271 272 273 274 275 276 |
# File 'lib/scrapedo/google_builder.rb', line 270 def inspect vars = instance_variables.map do |var| value = instance_variable_get(var) "#{var}=#{value.inspect}" unless var == :@params_cache end.join(", ") "#<#{self.class}:0x#{object_id.to_s(16)} #{vars}>" end |
#location(location) ⇒ Object
Location name in Google's canonical format. Automatically encoded to UULE internally. See Location.
Format: City,State/Region,Country
Examples:
[Istanbul,Istanbul,Turkey]
[New York,New York,United States]
141 142 143 144 |
# File 'lib/scrapedo/google_builder.rb', line 141 def location(location) @data[:location] = location self end |
#lr(language) ⇒ Object Also known as: language_restrict
Language Restrict applies strict language filtering. Only results written in the specified language are returned. Supports 35 languages. See Language Restrict Parameter.
This can also use GoogleBuilder#lr_language. For example:
- GoogleBuilder#lr("en") is same as GoogleBuilder#lr_en and GoogleBuilder#lr_english.
GoogleBuilder#language_restrict is an alias for GoogleBuilder#lr.
166 167 168 169 |
# File 'lib/scrapedo/google_builder.rb', line 166 def lr(language) @data[:lr] = language self end |
#next ⇒ JSON
Gets next page if result has next page.
225 226 227 |
# File 'lib/scrapedo/google_builder.rb', line 225 def next start(@data[:start] + 10) if next? end |
#next? ⇒ Boolean
Returns true if result have more pages; otherwise false.
30 31 32 |
# File 'lib/scrapedo/google_builder.rb', line 30 def next? @has_next end |
#params(*params) ⇒ Object
Set any parameter from Google Search API.
239 240 241 242 243 244 245 |
# File 'lib/scrapedo/google_builder.rb', line 239 def params(*params) params.first.each do |key, value| key_sym = key.to_sym @data[key_sym] = value if key_sym != :token && key_sym != :start && !value.nil? end self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
266 267 268 |
# File 'lib/scrapedo/google_builder.rb', line 266 def respond_to_missing?(name, include_private = false) find_config_source(name)&.present? || super end |
#safe_search ⇒ Object
Send active to filter adult content from results.
189 190 191 192 |
# File 'lib/scrapedo/google_builder.rb', line 189 def safe_search @data[:safe] = "active" self end |
#scrapedo_url ⇒ Object
Returns the URL to be used for the request.
248 249 250 251 252 |
# File 'lib/scrapedo/google_builder.rb', line 248 def scrapedo_url url = URI("https://api.scrape.do/plugin/google/search") url.query = URI.encode_www_form(@data) url end |
#search(query) ⇒ Object
This is required!
Search query. See Required Parameters.
39 40 41 42 43 44 45 46 |
# File 'lib/scrapedo/google_builder.rb', line 39 def search(query) raise ArgumentError, "Query is required" if blank? query @data.select! { |key| key == :token } @has_next = false @data[:q] = query self end |
#start(start = 0) ⇒ JSON
Start scraping from a specific offset. See General Parameters.
212 213 214 215 216 217 218 219 220 |
# File 'lib/scrapedo/google_builder.rb', line 212 def start(start = 0) raise ArgumentError, "Query is required" if blank? @data[:q] @data[:start] = start.positive? ? start / 10 * 10 : 0 response = Net::HTTP.get(scrapedo_url) result = JSON.parse(response) @has_next = !result["pagination"]["next"].nil? result end |
#time(time_period) ⇒ Object
Time Period limits results to a specific recency window. See Time-Based Filtering. Accepted values: last_hour, last_day, last_week, last_month, last_year
This can also use GoogleBuilder#time_period. For example:
- GoogleBuilder#time("last_hour") is same as GoogleBuilder#hour and GoogleBuilder#last_hour.
- GoogleBuilder#time("last_day") is same as GoogleBuilder#day and GoogleBuilder#last_day and GoogleBuilder#today.
- GoogleBuilder#time("last_week") is same as GoogleBuilder#week and GoogleBuilder#last_week.
- GoogleBuilder#time("last_month") is same as GoogleBuilder#month and GoogleBuilder#last_month.
- GoogleBuilder#time("last_year") is same as GoogleBuilder#year and GoogleBuilder#last_year.
72 73 74 75 |
# File 'lib/scrapedo/google_builder.rb', line 72 def time(time_period) @data[:time_period] = time_period self end |
#uule(uule) ⇒ Object
Google UULE-encoded location string. Auto-generated from location when not provided. If both location and uule are sent, uule takes priority. location is sufficient for most use cases. See uule.
152 153 154 155 |
# File 'lib/scrapedo/google_builder.rb', line 152 def uule(uule) @data[:uule] = uule self end |