google_search_results 1.1.0 → 2.0.1

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: c9cec2b3a527d7088744a0189c2d57bc324b3bec
4
- data.tar.gz: d9920122de8d07df6663ca0794eee0a8a4f33572
2
+ SHA256:
3
+ metadata.gz: ad56c82937a90531cdebbddfa443885bb58848e8b165387263a72968e656a8aa
4
+ data.tar.gz: be506a5558ad74878c37faac39eb2477b377547e63cba06e631e7c4f83145806
5
5
  SHA512:
6
- metadata.gz: 4096606c2a209617653af427213de99d88a419aac4e7aa6c3c5cd28ce0f43af6d716f552aa9b31b710f7a819090d74d53dfa2fc2a001160a976220ffeaaf1582
7
- data.tar.gz: 35c00cc9a3c8b2a2f9387afe73f54cf2b4859a9e7565409fbfdf81448485bdd8df2fbe8e4c05a3de31428ad02f976bbc1e411cb13a684fe0f059a4d62c104b22
6
+ metadata.gz: b39e137c299bb240a5194af347917170b85754394ef793f57c2843d9d0dced7558aef2a37ad907e431eeebbd558fd0f93ab40212d5a214a75b66ff5b50479e47
7
+ data.tar.gz: cab293e5a8883b81403fe802d59a4dfcf5078e822dddd62503e5b9ea46335b186e0bb50ea1cdc4838a80bb1db434a6feb6acae6831b27c44e0cfffd83885c495
@@ -1,102 +1,7 @@
1
- # Google Search Result for Ruby
2
- # powered by SerpApi.com
3
- #
4
- # Usage
5
- # ```
6
- #query_params = {
7
- # q: "query",
8
- # google_domain: "Google Domain",
9
- # location: "Location Requested",
10
- # device: device,
11
- # hl: "Google UI Language",
12
- # gl: "Google Country",
13
- # safe: "Safe Search Flag",
14
- # num: "Number of Results",
15
- # start: "Pagination Offset",
16
- # serp_api_key: "Your SERP API Key"
17
- # }
18
- #
19
- # query = GoogleSearchResults.new(query_params)
20
- # query.params[:location] = "Portland"
21
- #
22
- # html_results = query.get_html
23
- # hash_results = query.get_hash
24
- # json_results = query.get_json
25
- # ```
26
- # encoding: UTF-8
27
-
28
- require 'open-uri'
29
- require 'json'
30
- require_relative 'google_search_results/hash'
31
-
32
- class GoogleSearchResults
33
-
34
- VERSION = "1.1.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
44
-
45
- # constructor
46
- #
47
- # Usage
48
- # ---
49
- # ```require 'google_search_results'
50
- # query = GoogleSearchResults.new({q: "coffee", serp_api_key: "Your SERP API Key")
51
- # result = query.get_json```
52
- #
53
- 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
63
- end
64
-
65
- # get_html
66
- # @return [String] raw html
67
- def get_html
68
- @params[:output] = "html"
69
- get_results
70
- end
71
-
72
- # get_html
73
- # @return [Hash] search result Ruby hash
74
- # where keys are Symbol
75
- def get_hash
76
- JSON.load(get_json).symbolize_all_keys
77
- end
78
-
79
- def construct_url
80
- @params[:source] = "ruby"
81
- if @params[:serp_api_key].nil? and GoogleSearchResults.serp_api_key
82
- @params[:serp_api_key] = GoogleSearchResults.serp_api_key
83
- end
84
- URI::HTTPS.build(host: BACKEND, path: '/search', query: URI.encode_www_form(@params))
85
- end
86
-
87
- private
88
-
89
- def get_results
90
- begin
91
- open(construct_url, read_timeout: 600).read
92
- rescue OpenURI::HTTPError => e
93
- if error = JSON.load(e.io.read)["error"]
94
- raise error
95
- else
96
- raise e
97
- end
98
- end
99
- end
100
-
101
- end
102
-
1
+ require_relative 'search/serp_api_search.rb'
2
+ require_relative 'search/baidu_search.rb'
3
+ require_relative 'search/bing_search.rb'
4
+ require_relative 'search/ebay_search.rb'
5
+ require_relative 'search/google_search.rb'
6
+ require_relative 'search/yahoo_search.rb'
7
+ require_relative 'search/yandex_search.rb'
@@ -0,0 +1,33 @@
1
+ require_relative 'serp_api_search'
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 = BaiduSearch.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 BaiduSearch < SerpApiSearch
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 'serp_api_search'
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 = BingSearch.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 BingSearch < SerpApiSearch
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_search'
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 = EbaySearch.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 EbaySearch < SerpApiSearch
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
@@ -0,0 +1,39 @@
1
+ require_relative 'serp_api_search'
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 = GoogleSearch.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 GoogleSearch < SerpApiSearch
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 SerpApiSearch
15
+
16
+ VERSION = "2.0.1"
17
+ BACKEND = "serpapi.com"
18
+
19
+ attr_accessor :params
20
+
21
+ # constructor
22
+ #
23
+ # Usage
24
+ # ---
25
+ #
26
+ # ```ruby
27
+ # require 'google_search'
28
+ # search = SerpApiSearch.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(url).open(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_search'
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 = YahooSearch.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 YahooSearch < SerpApiSearch
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_search'
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 = YandexSearch.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 YandexSearch < SerpApiSearch
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.1.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - hartator
8
- autorequire:
8
+ - jvmvik
9
+ autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2019-02-21 00:00:00.000000000 Z
12
+ date: 2021-02-22 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
@@ -16,43 +17,77 @@ 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:
48
77
  - lib/google_search_results.rb
49
- - lib/google_search_results/hash.rb
78
+ - lib/search/baidu_search.rb
79
+ - lib/search/bing_search.rb
80
+ - lib/search/ebay_search.rb
81
+ - lib/search/google_search.rb
82
+ - lib/search/serp_api_search.rb
83
+ - lib/search/yahoo_search.rb
84
+ - lib/search/yandex_search.rb
50
85
  homepage: https://github.com/serpapi/google-search-results-ruby
51
86
  licenses:
52
87
  - MIT
53
88
  metadata:
54
89
  yard.run: yri
55
- post_install_message:
90
+ post_install_message:
56
91
  rdoc_options: []
57
92
  require_paths:
58
93
  - lib
@@ -67,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
102
  - !ruby/object:Gem::Version
68
103
  version: '0'
69
104
  requirements: []
70
- rubyforge_project:
71
- rubygems_version: 2.6.14
72
- signing_key:
105
+ rubygems_version: 3.1.2
106
+ signing_key:
73
107
  specification_version: 4
74
- summary: Google Search Results via SERP API.
108
+ summary: Get Google, Bing, Baidu, Ebay, Yahoo, Yandex Search Results via SerpApi.com
75
109
  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