google_search_results 1.0.1 → 1.3.2

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
- SHA1:
3
- metadata.gz: eb52d55050108ccd4c76ef94af06e6b8ec9d7c2d
4
- data.tar.gz: 63926f816a039a88bce029cc2df72bf74ef0d1e0
2
+ SHA256:
3
+ metadata.gz: 9cf8b604485ff927dae81c9fd1a5017d92563b339bc77994e0365926a14f6192
4
+ data.tar.gz: 46cca48662a2bfe6d7b7c79e34b92afa931d3be9e8cf38eb139d6d086068d2c7
5
5
  SHA512:
6
- metadata.gz: '091907b1665ca25e0952db05267e5946580ea3599604f052e90db773af019c4f182f8f67ec97142f2b0b7586bf40ab65fd3826bf07017bc589dd30fe2be342be'
7
- data.tar.gz: 8904267d9994e0948d2ef84168d7bd47d79ce5b7de608c60ffd16c9c6eb3c23e8bc6af2f0cb8d85d4bfcb02cdf66dc041f072f5a7f1be5ee09153fd4a6e9ab38
6
+ metadata.gz: 6d26a044da90201b3c7e77841e7acc5bce8f0534ad7840df78b42120a7549ca79a3b787af90ad25e1062713b47a0fe7ab32b000a9ada008f27a64421ae97370c
7
+ data.tar.gz: 5257a57981176ee85df134f6bc89e1eebf2b16e44d0d3c90ee72a63541359b83e9ac8a69639c41c87d9f8a2eeb6571360bd223bd87b8474f90c3aa0a53b4796d
@@ -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
+ # search = BaiduSearchResults.new(parameter)
14
+ #
15
+ # html_results = search.get_html
16
+ # hash_results = search.get_hash
17
+ # json_results = search.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
+ # search = BingSearchResults.new(parameter)
15
+ # search.params[:location] = "Portland"
16
+ #
17
+ # html_results = search.get_html
18
+ # hash_results = search.get_hash
19
+ # json_results = search.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
+ # search = EbaySearchResults.new(parameter)
14
+ # search.params[:ebay_domain] = "ebay.com"
15
+ #
16
+ # html_results = search.get_html
17
+ # hash_results = search.get_hash
18
+ # json_results = search.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,67 +1,39 @@
1
- # encoding: UTF-8
2
-
3
- require 'open-uri'
4
- require 'json'
5
- require_relative 'google_search_results/hash'
6
-
7
- class GoogleSearchResults
8
-
9
- VERSION = "1.0.1"
10
- BACKEND = "serpapi.com"
11
-
12
- @@serp_api_key = nil
13
-
14
- class << self
15
- attr_accessor :serp_api_key
16
- end
17
-
18
- attr_accessor :params
19
-
20
- def initialize params = {}
21
- @params = params
22
- end
23
-
24
- def construct_url
25
- @params[:source] = "ruby"
26
- if @params[:serp_api_key].nil? and GoogleSearchResults.serp_api_key
27
- @params[:serp_api_key] = GoogleSearchResults.serp_api_key
28
- end
29
- URI::HTTPS.build(host: BACKEND, path: '/search', query: URI.encode_www_form(@params))
30
- end
31
-
32
- def get_results
33
- begin
34
- open(construct_url).read
35
- rescue OpenURI::HTTPError => e
36
- if error = JSON.load(e.io.read)["error"]
37
- raise error
38
- else
39
- raise e
40
- end
41
- end
42
- end
43
-
44
- def get_html
45
- @params[:output] = "html"
46
- get_results
47
- end
48
-
49
- def get_json
50
- @params[:output] = "json"
51
- get_results
52
- end
53
-
54
- def get_json_with_images
55
- @params[:output] = "json_with_images"
56
- get_results
57
- end
58
-
59
- def get_hash
60
- JSON.load(get_json).symbolize_all_keys
61
- end
62
-
63
- def get_hash_with_images
64
- JSON.load(get_json_with_images).symbolize_all_keys
65
- end
66
-
67
- end
1
+ require_relative 'serp_api_client'
2
+
3
+ # Google Search Result for Ruby powered by SerpApi
4
+ #
5
+ # Search API Usage
6
+ #
7
+ # ```ruby
8
+ # parameter = {
9
+ # q: "query",
10
+ # google_domain: "Google Domain",
11
+ # location: "Location Requested",
12
+ # device: device,
13
+ # hl: "Google UI Language",
14
+ # gl: "Google Country",
15
+ # safe: "Safe Search Flag",
16
+ # num: "Number of Results",
17
+ # start: "Pagination Offset",
18
+ # tbm: "to be matched field",
19
+ # tbs: "to be searched field",
20
+ # api_key: "Your SERP API Key"
21
+ # }
22
+ #
23
+ # search = GoogleSearchResults.new(parameter)
24
+ # search.params[:location] = "Portland"
25
+ #
26
+ # html_results = search.get_html
27
+ # hash_results = search.get_hash
28
+ # json_results = search.get_json
29
+ # ```
30
+ #
31
+ # doc: https://serpapi.com/search-api
32
+ class GoogleSearchResults < SerpApiClient
33
+
34
+ def initialize(params = {})
35
+ super(params, GOOGLE_ENGINE)
36
+ check_params([:q, :engine])
37
+ end
38
+
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 HTTP client for serpapi.com
13
+ #
14
+ class SerpApiClient
15
+
16
+ VERSION = "1.3.2"
17
+ BACKEND = "serpapi.com"
18
+
19
+ attr_accessor :params
20
+
21
+ # constructor
22
+ #
23
+ # Usage
24
+ # ---
25
+ #
26
+ # ```ruby
27
+ # require 'google_search_results'
28
+ # search = SerpApiClient.new({q: "coffee", api_key: "secure API key", engine: "google"})
29
+ # result = search.get_json
30
+ # ```
31
+ #
32
+ # @param [Hash] params contains requested parameter
33
+ # @param [String] engine google|baidu|google|bing|ebay|yandex (optional or can be set in params)
34
+ def initialize(params, engine = nil)
35
+ @params = params
36
+ @params[:engine] ||= engine
37
+ raise 'engine must be defined in params or a second argument' if @params[:engine].nil?
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 search
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
+ # search = YahooSearchResults.new(parameter)
14
+ # search.params[:yahoo_domain] = "fr"
15
+ #
16
+ # html_results = search.get_html
17
+ # hash_results = search.get_hash
18
+ # json_results = search.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
+ # search = YandexSearchResults.new(parameter)
14
+ # search.params[:yandex_domain] = "yandex.com"
15
+ #
16
+ # html_results = search.get_html
17
+ # hash_results = search.get_hash
18
+ # json_results = search.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.0.1
4
+ version: 1.3.2
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: 2018-02-07 00:00:00.000000000 Z
12
+ date: 2020-07-05 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
@@ -16,41 +17,75 @@ 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
- name: minitest
29
+ name: rspec
29
30
  requirement: !ruby/object:Gem::Requirement
30
31
  requirements:
31
32
  - - "~>"
32
33
  - !ruby/object:Gem::Version
33
- version: '5.2'
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: '5.2'
41
- description: Get Google Search Results via SERP API. Hash, JSON, and HTML outputs
42
- 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.49.1
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 0.49.1
70
+ description: Scape localized search results from search engine using SerpApi.com and
71
+ returns Hash, JSON, raw HTML
43
72
  email: hartator@gmail.com
44
73
  executables: []
45
74
  extensions: []
46
75
  extra_rdoc_files: []
47
76
  files:
77
+ - lib/baidu_search_results.rb
78
+ - lib/bing_search_results.rb
79
+ - lib/ebay_search_results.rb
48
80
  - lib/google_search_results.rb
49
- - lib/google_search_results/hash.rb
81
+ - lib/serp_api_client.rb
82
+ - lib/yahoo_search_results.rb
83
+ - lib/yandex_search_results.rb
50
84
  homepage: https://github.com/serpapi/google-search-results-ruby
51
85
  licenses:
52
86
  - MIT
53
- metadata: {}
87
+ metadata:
88
+ yard.run: yri
54
89
  post_install_message:
55
90
  rdoc_options: []
56
91
  require_paths:
@@ -66,9 +101,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
101
  - !ruby/object:Gem::Version
67
102
  version: '0'
68
103
  requirements: []
69
- rubyforge_project:
70
- rubygems_version: 2.6.14
104
+ rubygems_version: 3.1.2
71
105
  signing_key:
72
106
  specification_version: 4
73
- summary: Google Search Results via SERP API.
107
+ summary: Get Google, Bing, Baidu, Ebay, Yahoo, Yandex Search Results via SerpApi.com
74
108
  test_files: []
@@ -1,18 +0,0 @@
1
- class Hash
2
- def symbolize_all_keys(hash=self)
3
- symbolized_hash = {}
4
- return hash if hash.is_a?(String)
5
- hash.each do |k, v|
6
- if v.is_a?(Array)
7
- symbolized_hash[k.to_sym] = v.map do |e|
8
- symbolize_all_keys(e)
9
- end
10
- elsif v.is_a?(Hash)
11
- symbolized_hash[k.to_sym] = symbolize_all_keys(v)
12
- else
13
- symbolized_hash[k.to_sym] = v
14
- end
15
- end
16
- symbolized_hash
17
- end
18
- end