google_search_results 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 981326cdb315dc996ca78711369407131f295719296498613d6c8003c11afeb7
4
- data.tar.gz: 0f9d30de883d5db7aff06274a098716103544696283ed1bc85e533a756b551c0
3
+ metadata.gz: 37245404ba2b79b279c3719ed9b0142aa8914d80564cd6cfd70af9a2dcd4f56d
4
+ data.tar.gz: ae3bec079837ac373d4ffa4ff862b32212d914bde417184e5cb3627de38762ea
5
5
  SHA512:
6
- metadata.gz: 7519258d49f11585846f80fc7f29909faf619e86b75d1680c60e6e63e15afec7f3ff7071d960c9954b97a67e1746da90ea1ea6cdd3ba9066524123605fd774c5
7
- data.tar.gz: 8a156b74c5a054b07bd8f41a8ca757ce11a8246f837987da6dd1a6545739a214f22a1b6e6cdc76e28c2cf53cf720f92064a84bea60af34e2f0d51f3879380191
6
+ metadata.gz: b4e94b1c4902bc506e03c0b1461564ace6052925cb286a7e135634f76fdc81e9ed62833a440d77d8a32e0699745314f4cd1e4e72e22f14499dffa4fbee9dc935
7
+ data.tar.gz: d3199c0440f4f68ae947617c867e5a68ec516b2d60870c763fef538762f93f88d695660f5dad98fe18100b096396941fd5d2e49e59d0c8bec709f22e2ab08078
@@ -0,0 +1,33 @@
1
+ require_relative 'serp_api_client'
2
+
3
+ # Baidu Search Result for Ruby powered by SerpApi
4
+ #
5
+ # Search API Usage
6
+ #
7
+ # ```ruby
8
+ # parameter = {
9
+ # q: "query",
10
+ # api_key: "Serp API Key"
11
+ # }
12
+ #
13
+ # client = BaiduSearchResults.new(parameter)
14
+ #
15
+ # html_results = client.get_html
16
+ # hash_results = client.get_hash
17
+ # json_results = client.get_json
18
+ #
19
+ # ```
20
+ # doc: https://serpapi.com/baidu-search-api
21
+
22
+ class BaiduSearchResults < SerpApiClient
23
+
24
+ def initialize(params = {})
25
+ super(params, BING_ENGINE)
26
+ check_params([:q, :engine])
27
+ end
28
+
29
+ def get_location
30
+ raise 'location is not supported by Baidu'
31
+ end
32
+
33
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../lib/bing_search_results'
2
+
3
+ # Bing Search Result for Ruby powered by SerpApi
4
+ #
5
+ # Search API Usage
6
+ #
7
+ # ```ruby
8
+ # parameter = {
9
+ # q: "query",
10
+ # location: "city,state,country",
11
+ # api_key: "Your SERP API Key"
12
+ # }
13
+ #
14
+ # client = BingSearchResults.new(parameter)
15
+ # client.params[:location] = "Portland"
16
+ #
17
+ # html_results = client.get_html
18
+ # hash_results = client.get_hash
19
+ # json_results = client.get_json
20
+ #
21
+ # ```
22
+ #
23
+ # doc: https://serpapi.com/bing-search-api
24
+
25
+ class BingSearchResults < SerpApiClient
26
+
27
+ def initialize(params = {})
28
+ super(params, BING_ENGINE)
29
+ check_params([:q, :engine])
30
+ end
31
+
32
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'serp_api_client'
2
+
3
+ # Ebay Search Result for Ruby powered by SerpApi
4
+ #
5
+ # Search API Usage
6
+ #
7
+ # ```ruby
8
+ # parameter = {
9
+ # _nkw: "query",
10
+ # api_key: "Your SERP API Key"
11
+ # }
12
+ #
13
+ # client = EbaySearchResults.new(parameter)
14
+ # client.params[:ebay_domain] = "ebay.com"
15
+ #
16
+ # html_results = client.get_html
17
+ # hash_results = client.get_hash
18
+ # json_results = client.get_json
19
+ #
20
+ # ```
21
+ #
22
+ # doc: https://serpapi.com/ebay-search-api
23
+
24
+ class EbaySearchResults < SerpApiClient
25
+
26
+ def initialize(params = {})
27
+ super(params, EBAY_ENGINE)
28
+ check_params([:_nkw, :engine])
29
+ end
30
+
31
+ def get_location
32
+ raise 'location is not supported by ' + EBAY_ENGINE
33
+ end
34
+
35
+ end
@@ -1,7 +1,10 @@
1
- # Google Search Result for Ruby powered by SerpAPI
1
+ require_relative 'serp_api_client'
2
+
3
+ # Google Search Result for Ruby powered by SerpApi
2
4
  #
3
5
  # Search API Usage
4
- # ```
6
+ #
7
+ # ```ruby
5
8
  # parameter = {
6
9
  # q: "query",
7
10
  # google_domain: "Google Domain",
@@ -14,7 +17,7 @@
14
17
  # start: "Pagination Offset",
15
18
  # tbm: "to be matched field",
16
19
  # tbs: "to be searched field",
17
- # serp_api_key: "Your SERP API Key"
20
+ # api_key: "Your SERP API Key"
18
21
  # }
19
22
  #
20
23
  # client = GoogleSearchResults.new(parameter)
@@ -24,111 +27,13 @@
24
27
  # hash_results = client.get_hash
25
28
  # json_results = client.get_json
26
29
  # ```
27
- # encoding: UTF-8
28
-
29
- require 'open-uri'
30
- require 'json'
31
-
32
- class GoogleSearchResults
33
-
34
- VERSION = "1.2.0"
35
- BACKEND = "serpapi.com"
36
-
37
- @@serp_api_key = nil
38
-
39
- class << self
40
- attr_accessor :serp_api_key
41
- end
42
-
43
- attr_accessor :params
30
+ #
31
+ # doc: https://serpapi.com/search-api
32
+ class GoogleSearchResults < SerpApiClient
44
33
 
45
- # constructor
46
- #
47
- # Usage
48
- # ---
49
- # ```require 'google_search_results'
50
- # client = GoogleSearchResults.new({q: "coffee", serp_api_key: "Your SERP API Key")
51
- # result = client.get_json```
52
- #
53
34
  def initialize(params = {})
54
- @params = params
55
- end
56
-
57
- # get_json
58
- # @return [Hash] search result "json like"
59
- # where keys are String
60
- def get_json
61
- @params[:output] = "json"
62
- get_results('/search')
63
- end
64
-
65
- # get_html
66
- # @return [String] raw html
67
- def get_html
68
- @params[:output] = "html"
69
- get_results('/search')
70
- end
71
-
72
- # get_html
73
- # @return [Hash] search result Ruby hash
74
- # where keys are Symbol
75
- def get_hash
76
- JSON.parse(get_json, {symbolize_names: true})
35
+ super(params, GOOGLE_ENGINE)
36
+ check_params([:q, :engine])
77
37
  end
78
38
 
79
- # alias for get_hash
80
- # @deprecated
81
- def get_hash_with_images
82
- get_hash
83
- end
84
-
85
- # alias for get_json
86
- # @deprecated
87
- def get_json_with_images
88
- get_json
89
- end
90
-
91
- # Get location using Location API
92
- def get_location
93
- JSON.parse(get_results('/locations.json'), {symbolize_names: true})
94
- end
95
-
96
- # Retrieve search result from the Search Archive API
97
- def get_search_archive(search_id, format = 'json')
98
- raise 'format must be json or html' if format !~ /^(html|json)$/
99
- JSON.parse(get_results("/searches/#{search_id}.#{format}"), {symbolize_names: true})
100
- end
101
-
102
- # Get account information using Account API
103
- def get_account
104
- JSON.parse(get_results('/account'), {symbolize_names: true})
105
- end
106
-
107
- def construct_url(path)
108
- @params[:source] = "ruby"
109
- if GoogleSearchResults.serp_api_key
110
- @params[:serp_api_key] ||= GoogleSearchResults.serp_api_key
111
- end
112
- if @params[:serp_api_key].nil?
113
- @params.delete(:serp_api_key)
114
- end
115
-
116
- URI::HTTPS.build(host: BACKEND, path: path, query: URI.encode_www_form(@params))
117
- end
118
-
119
- private
120
-
121
- def get_results(path)
122
- begin
123
- open(construct_url(path), read_timeout: 600).read
124
- rescue OpenURI::HTTPError => e
125
- if error = JSON.load(e.io.read)["error"]
126
- raise error
127
- else
128
- raise e
129
- end
130
- end
131
- end
132
-
133
- end
134
-
39
+ end
@@ -0,0 +1,178 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+
4
+ GOOGLE_ENGINE = 'google'
5
+ BAIDU_ENGINE = 'baidu'
6
+ BING_ENGINE = 'bing'
7
+ YAHOO_ENGINE = 'yahoo'
8
+ YANDEX_ENGINE = 'yandex'
9
+ EBAY_ENGINE = 'ebay'
10
+
11
+
12
+ # Generic serpapi.com client
13
+ # which allow to custom cutting edge search service
14
+ # by setting the engine paremeter.
15
+ class SerpApiClient
16
+
17
+ VERSION = "1.3.0"
18
+ BACKEND = "serpapi.com"
19
+
20
+ attr_accessor :params
21
+
22
+ # constructor
23
+ #
24
+ # Usage
25
+ # ---
26
+ #
27
+ # ```ruby
28
+ # require 'google_search_results'
29
+ # client = SerpApiClient.new({q: "coffee", api_key: "secure API key"}, "google")
30
+ # result = client.get_json
31
+ # ```
32
+ #
33
+ # @param [Hash] params contains requested parameter
34
+ # @param [String] engine google|baidu|google|bing|ebay|yandex
35
+ def initialize(params, engine)
36
+ @params = params
37
+ @params[:engine] ||= engine
38
+ end
39
+
40
+ # get_json
41
+ # @return [Hash] search result "json like"
42
+ # where keys are String
43
+ def get_json
44
+ @params[:output] = "json"
45
+ get_results('/search')
46
+ end
47
+
48
+ # get_html
49
+ # @return [String] raw html
50
+ def get_html
51
+ @params[:output] = "html"
52
+ get_results('/search')
53
+ end
54
+
55
+ # get_html
56
+ # @return [Hash] search result Ruby hash
57
+ # where keys are Symbol
58
+ def get_hash
59
+ JSON.parse(get_json, {symbolize_names: true})
60
+ end
61
+
62
+ # alias for get_hash
63
+ # @deprecated
64
+ def get_hash_with_images
65
+ get_hash
66
+ end
67
+
68
+ # alias for get_json
69
+ # @deprecated
70
+ def get_json_with_images
71
+ get_json
72
+ end
73
+
74
+ # Get location using Location API
75
+ def get_location
76
+ as_json(get_results('/locations.json'))
77
+ end
78
+
79
+ # Retrieve search result from the Search Archive API
80
+ def get_search_archive(search_id, format = 'json')
81
+ raise 'format must be json or html' if format !~ /^(html|json)$/
82
+ as_json(get_results("/searches/#{search_id}.#{format}"))
83
+ end
84
+
85
+ # Get account information using Account API
86
+ def get_account
87
+ as_json(get_results('/account'))
88
+ end
89
+
90
+ # @return [String] current search engine
91
+ def engine
92
+ @params[:engine]
93
+ end
94
+
95
+ def construct_url(path)
96
+ @params[:source] = "ruby"
97
+ if !$serp_api_key.nil?
98
+ @params[:api_key] = $serp_api_key
99
+ end
100
+
101
+ @params.delete_if { |_, value| value.nil? }
102
+
103
+ URI::HTTPS.build(host: BACKEND, path: path, query: URI.encode_www_form(@params))
104
+ end
105
+
106
+ # serp_api_key
107
+ # legacy implementation.
108
+ #
109
+ # @param [String] api_key set user secret API key (copy/paste from https://serpapi.com/dashboard)
110
+ def self.serp_api_key=(api_key)
111
+ self.api_key = api_key
112
+ end
113
+
114
+ # api_key
115
+ # @param [String] api_key set user secret API key (copy/paste from https://serpapi.com/dashboard)
116
+ def self.api_key=(api_key)
117
+ $serp_api_key = api_key
118
+ end
119
+
120
+ # @return [String] api_key for this client
121
+ def api_key
122
+ @params[:api_key] || @params[:serp_api_key] || $serp_api_key
123
+ end
124
+
125
+ private
126
+
127
+ def as_json(data)
128
+ JSON.parse(data, symbolize_names: true)
129
+ end
130
+
131
+ def get_results(path)
132
+ begin
133
+ url = construct_url(path)
134
+ URI::open(url, read_timeout: 600).read
135
+ rescue OpenURI::HTTPError => e
136
+ if error = JSON.load(e.io.read)["error"]
137
+ puts "server returns error for url: #{url}"
138
+ raise error
139
+ else
140
+ puts "fail: fetch url: #{url}"
141
+ raise e
142
+ end
143
+ rescue => e
144
+ puts "fail: fetch url: #{url}"
145
+ raise e
146
+ end
147
+ end
148
+
149
+ def check_params(keys = [])
150
+ return if @params.keys == [:engine]
151
+
152
+ raise 'keys must be a list of String or Symbol' unless keys.class == Array
153
+ missing = []
154
+ keys.each do |key|
155
+ case key.class.to_s
156
+ when 'String'
157
+ if @params[key].nil?
158
+ if @params[key.to_sym].nil?
159
+ missing << key.to_s
160
+ end
161
+ end
162
+ when 'Symbol'
163
+ if @params[key].nil?
164
+ if @params[key.to_s].nil?
165
+ missing << key.to_s
166
+ end
167
+ end
168
+ else
169
+ raise 'keys must contains Symbol or String'
170
+ end
171
+ end
172
+ if !missing.empty?
173
+ raise "missing required keys in params.\n #{missing.join(',')}"
174
+ end
175
+ end
176
+
177
+ end
178
+
@@ -0,0 +1,35 @@
1
+ require_relative 'serp_api_client'
2
+
3
+ # Yahoo Search Result for Ruby powered by SerpApi
4
+ #
5
+ # Search API Usage
6
+ #
7
+ # ```ruby
8
+ # parameter = {
9
+ # p: "query",
10
+ # api_key: "Your SERP API Key"
11
+ # }
12
+ #
13
+ # client = YahooSearchResults.new(parameter)
14
+ # client.params[:yahoo_domain] = "fr"
15
+ #
16
+ # html_results = client.get_html
17
+ # hash_results = client.get_hash
18
+ # json_results = client.get_json
19
+ #
20
+ # ```
21
+ #
22
+ # doc: https://serpapi.com/yahoo-search-api
23
+
24
+ class YahooSearchResults < SerpApiClient
25
+
26
+ def initialize(params = {})
27
+ super(params, YAHOO_ENGINE)
28
+ check_params([:p, :engine])
29
+ end
30
+
31
+ def get_location
32
+ raise 'location is not supported by ' + YAHOO_ENGINE
33
+ end
34
+
35
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'serp_api_client'
2
+
3
+ # Yandex Search Result for Ruby powered by SerpApi
4
+ #
5
+ # Search API Usage
6
+ #
7
+ # ```ruby
8
+ # parameter = {
9
+ # text: "query",
10
+ # api_key: "Your SERP API Key"
11
+ # }
12
+ #
13
+ # client = YandexSearchResults.new(parameter)
14
+ # client.params[:yandex_domain] = "yandex.com"
15
+ #
16
+ # html_results = client.get_html
17
+ # hash_results = client.get_hash
18
+ # json_results = client.get_json
19
+ #
20
+ # ```
21
+ #
22
+ # doc: https://serpapi.com/yandex-search-api
23
+
24
+ class YandexSearchResults < SerpApiClient
25
+
26
+ def initialize(params = {})
27
+ super(params, YANDEX_ENGINE)
28
+ check_params([:text, :engine])
29
+ end
30
+
31
+ def get_location
32
+ raise 'location is not supported by ' + YAHOO_ENGINE
33
+ end
34
+
35
+ end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_search_results
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hartator
8
+ - jvmvik
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2019-04-16 00:00:00.000000000 Z
12
+ date: 2020-03-31 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
@@ -16,35 +17,70 @@ dependencies:
16
17
  requirements:
17
18
  - - "~>"
18
19
  - !ruby/object:Gem::Version
19
- version: '10.2'
20
+ version: 13.0.1
20
21
  type: :development
21
22
  prerelease: false
22
23
  version_requirements: !ruby/object:Gem::Requirement
23
24
  requirements:
24
25
  - - "~>"
25
26
  - !ruby/object:Gem::Version
26
- version: '10.2'
27
+ version: 13.0.1
27
28
  - !ruby/object:Gem::Dependency
28
29
  name: rspec
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
32
  - - "~>"
32
33
  - !ruby/object:Gem::Version
33
- version: '3.8'
34
+ version: '3.9'
34
35
  type: :development
35
36
  prerelease: false
36
37
  version_requirements: !ruby/object:Gem::Requirement
37
38
  requirements:
38
39
  - - "~>"
39
40
  - !ruby/object:Gem::Version
40
- version: '3.8'
41
- description: Get Google Search Results via SERP API. Hash, JSON, and HTML format supported.
41
+ version: '3.9'
42
+ - !ruby/object:Gem::Dependency
43
+ name: yard
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 0.9.24
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 0.9.24
56
+ - !ruby/object:Gem::Dependency
57
+ name: rubocop
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 0.39.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.39.0
70
+ description: Scape localized search results from search engine using SerpApi.com and
71
+ returns Hash, JSON, raw HTML
42
72
  email: hartator@gmail.com
43
73
  executables: []
44
74
  extensions: []
45
75
  extra_rdoc_files: []
46
76
  files:
77
+ - lib/baidu_search_results.rb
78
+ - lib/bing_search_results.rb
79
+ - lib/ebay_search_results.rb
47
80
  - lib/google_search_results.rb
81
+ - lib/serp_api_client.rb
82
+ - lib/yahoo_search_results.rb
83
+ - lib/yandex_search_results.rb
48
84
  homepage: https://github.com/serpapi/google-search-results-ruby
49
85
  licenses:
50
86
  - MIT
@@ -65,8 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
101
  - !ruby/object:Gem::Version
66
102
  version: '0'
67
103
  requirements: []
68
- rubygems_version: 3.0.3
104
+ rubygems_version: 3.1.2
69
105
  signing_key:
70
106
  specification_version: 4
71
- summary: Google Search Results via SERP API.
107
+ summary: Get Google, Bing, Baidu, Ebay, Yahoo, Yandex Search Results via SerpApi.com
72
108
  test_files: []