google_search_results 1.0.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 57f71c4cad805b4e1bf820d27dee4da8ead20b99
4
- data.tar.gz: 19b72d8a91fa20666f0fba71b2394f7d49691749
2
+ SHA256:
3
+ metadata.gz: d6431b13d508a7d2758723df6ff4f0f702e00005daf957789b01f993516e4a37
4
+ data.tar.gz: 8ade7cedec4cfeb413ee4f5ded86ba37a578f5424f88f5ab28bcec9006623983
5
5
  SHA512:
6
- metadata.gz: eb2406e30532a5322371f69ce720a775cf13676cead003d59f85b45507cec9f1cce7b6eb67ad19f4d3db0131a8b87a093c025d2d67e195f8cd0e1421a80f1b7c
7
- data.tar.gz: c19c6675aaba2dbd23c49ddc042ea3e065a65ca7f1bc8ecdb5b9846a25516c15627412d21122a76d298a93d8e0b578cca44e2b05723fc2d1a923ac9b286660b5
6
+ metadata.gz: 112a0060aa508a73274f6b96233ec46a7d4256d2d3e5952605a1951da9b8af0225b6f1be61e5fc8d7c149feff52218538a3cb39221fc305d09ed8f387fbf2385
7
+ data.tar.gz: e9d03804d47bd7e84778635e4cf35b14790cad13124572bf23a769a7160b68f26381d42532b15a4a28033ee69f8be1d8fa11c18b252937631a4342d3c5e514ce
@@ -1,67 +1,7 @@
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.2"
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_timeout: 600).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 '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.0"
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::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_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.0.2
4
+ version: 2.0.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: 2018-05-10 00:00:00.000000000 Z
12
+ date: 2020-08-09 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
@@ -16,41 +17,76 @@ 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
- metadata: {}
88
+ metadata:
89
+ yard.run: yri
54
90
  post_install_message:
55
91
  rdoc_options: []
56
92
  require_paths:
@@ -66,9 +102,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
102
  - !ruby/object:Gem::Version
67
103
  version: '0'
68
104
  requirements: []
69
- rubyforge_project:
70
- rubygems_version: 2.6.14
105
+ rubygems_version: 3.1.2
71
106
  signing_key:
72
107
  specification_version: 4
73
- summary: Google Search Results via SERP API.
108
+ summary: Get Google, Bing, Baidu, Ebay, Yahoo, Yandex Search Results via SerpApi.com
74
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