seo-keywords-generator-ruby 0.1.10 → 0.1.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/seo-keywords-generator-ruby.rb +222 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c194e2d1710fd2d9ee64bb4a7fc1943812d430b780ae4d58d0decb161d9be782
|
4
|
+
data.tar.gz: 895cd9589b58d2cefff32c8896042119068cae7517f298e96e60640cec65a651
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ae40ae8282437ce336b7404cea767fdbfdbae78b2fbcdf0cf308a6a02fdad4fce469b9217525afadaf2678dc8334c6980f5b7973bbd7d140b451742d964a016
|
7
|
+
data.tar.gz: 77554adfde96256355bc02ebcbf05c499f43018d146fd2e16f2d16cce71d4cd2658bfc2b4f5a1f28c317cac00a54da1aa9fb54f3d8e69bc6ccf070a1b65b4dc8
|
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'google_search_results'
|
2
|
+
require 'optparse'
|
3
|
+
require 'json'
|
4
|
+
require 'csv'
|
5
|
+
|
6
|
+
module SeoKeywordsGenerator
|
7
|
+
class CLI
|
8
|
+
def initialize(argv)
|
9
|
+
@argv = argv
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
options = {
|
14
|
+
engines: ['ac', 'rs', 'rq'],
|
15
|
+
depth_limit: 0,
|
16
|
+
save_to: 'CSV',
|
17
|
+
api_key: '5868ece26d41221f5e19ae8b3e355d22db23df1712da675d144760fc30d57988',
|
18
|
+
domain: 'google.com',
|
19
|
+
country: 'us',
|
20
|
+
lang: 'en',
|
21
|
+
}
|
22
|
+
|
23
|
+
OptionParser.new do |opts|
|
24
|
+
opts.banner = 'Usage: seo [options]'
|
25
|
+
|
26
|
+
opts.on('-q', '--query QUERY', String, 'Search query (required)') do |q|
|
27
|
+
options[:query] = q
|
28
|
+
end
|
29
|
+
|
30
|
+
opts.on('-e', '--engines ENGINES', Array, 'Choices of engines to extract: Autocomplete (ac), Related Searches (rs), People Also Ask (rq). You can select multiple engines by separating them with a comma: ac,rs. All engines are selected by default.') do |e|
|
31
|
+
options[:engines] = e
|
32
|
+
end
|
33
|
+
|
34
|
+
opts.on('-d', '--depth-limit LIMIT', Integer, 'Depth limit for People Also Ask. Default is 0, first 2-4 results.') do |dl|
|
35
|
+
options[:depth_limit] = dl
|
36
|
+
end
|
37
|
+
|
38
|
+
opts.on('-s', '--save-to SAVE', String, 'Saves the results in the current directory in the selected format (CSV, JSON, TXT). Default CSV.') do |st|
|
39
|
+
options[:save_to] = st
|
40
|
+
end
|
41
|
+
|
42
|
+
opts.on('-k', '--api-key KEY', String, 'Your SerpApi API key: https://serpapi.com/manage-api-key. Default is a test API key to test CLI.') do |ak|
|
43
|
+
options[:api_key] = ak
|
44
|
+
end
|
45
|
+
|
46
|
+
opts.on('-g', '--domain DOMAIN', String, 'Google domain: https://serpapi.com/google-domains. Default google.com.') do |gd|
|
47
|
+
options[:domain] = gd
|
48
|
+
end
|
49
|
+
|
50
|
+
opts.on('-c', '--country COUNTRY', String, 'Country of the search: https://serpapi.com/google-countries. Default us.') do |gl|
|
51
|
+
options[:country] = gl
|
52
|
+
end
|
53
|
+
|
54
|
+
opts.on('-l', '--language LANGUAGE', String, 'Language of the search: https://serpapi.com/google-languages. Default en.') do |hl|
|
55
|
+
options[:lang] = hl
|
56
|
+
end
|
57
|
+
end.parse!(@argv)
|
58
|
+
|
59
|
+
keyword_research = SeoKeywordsGenerator.new(
|
60
|
+
query=options[:query],
|
61
|
+
api_key=options[:api_key],
|
62
|
+
lang=options[:lang],
|
63
|
+
country=options[:country],
|
64
|
+
domain=options[:domain]
|
65
|
+
)
|
66
|
+
|
67
|
+
data = {}
|
68
|
+
|
69
|
+
options[:engines]&.each do |engine|
|
70
|
+
case engine.downcase
|
71
|
+
when 'ac'
|
72
|
+
data['auto_complete'] = keyword_research.get_auto_complete()
|
73
|
+
when 'rs'
|
74
|
+
data['related_searches'] = keyword_research.get_related_searches()
|
75
|
+
when 'rq'
|
76
|
+
data['related_questions'] = keyword_research.get_related_questions(options[:depth_limit])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
if !data.empty?
|
81
|
+
keyword_research.print_data(data)
|
82
|
+
puts "Saving data in #{options[:save_to].upcase} format..."
|
83
|
+
|
84
|
+
case options[:save_to].upcase
|
85
|
+
when 'CSV'
|
86
|
+
keyword_research.save_to_csv(data)
|
87
|
+
when 'JSON'
|
88
|
+
keyword_research.save_to_json(data)
|
89
|
+
when 'TXT'
|
90
|
+
keyword_research.save_to_txt(data)
|
91
|
+
end
|
92
|
+
|
93
|
+
puts "Data successfully saved to #{options[:query].gsub(' ', '_')}.#{options[:save_to].downcase} file"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class Scraper
|
99
|
+
attr_accessor :query, :api_key, :lang, :country, :domain
|
100
|
+
|
101
|
+
def initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com')
|
102
|
+
@query = query
|
103
|
+
@api_key = api_key
|
104
|
+
@lang = lang
|
105
|
+
@country = country
|
106
|
+
@domain = domain
|
107
|
+
@related_questions_results = []
|
108
|
+
end
|
109
|
+
|
110
|
+
def get_auto_complete
|
111
|
+
params = {
|
112
|
+
api_key: @api_key, # https://serpapi.com/manage-api-key
|
113
|
+
engine: 'google_autocomplete', # search engine
|
114
|
+
q: @query, # search query
|
115
|
+
gl: @country, # country of the search
|
116
|
+
hl: @lang # language of the search
|
117
|
+
}
|
118
|
+
|
119
|
+
search = GoogleSearch.new(params) # data extraction on the SerpApi backend
|
120
|
+
results = search.get_hash # JSON -> Ruby hash
|
121
|
+
|
122
|
+
results[:suggestions].map{ |result| result[:value] }.compact
|
123
|
+
end
|
124
|
+
|
125
|
+
def get_related_searches
|
126
|
+
params = {
|
127
|
+
api_key: @api_key, # https://serpapi.com/manage-api-key
|
128
|
+
engine: 'google', # search engine
|
129
|
+
q: @query, # search query
|
130
|
+
google_domain: @domain, # Google domain to use
|
131
|
+
gl: @country, # country of the search
|
132
|
+
hl: @lang # language of the search
|
133
|
+
}
|
134
|
+
|
135
|
+
search = GoogleSearch.new(params) # data extraction on the SerpApi backend
|
136
|
+
results = search.get_hash # JSON -> Ruby hash
|
137
|
+
|
138
|
+
results[:related_searches].map{ |result| result[:query] }.compact
|
139
|
+
end
|
140
|
+
|
141
|
+
def get_depth_results(token, depth)
|
142
|
+
depth_params = {
|
143
|
+
q: @query,
|
144
|
+
api_key: @api_key,
|
145
|
+
engine: 'google_related_questions',
|
146
|
+
next_page_token: token
|
147
|
+
}
|
148
|
+
|
149
|
+
depth_search = GoogleSearch.new(depth_params)
|
150
|
+
depth_results = depth_search.get_hash
|
151
|
+
|
152
|
+
@related_questions_results += depth_results[:related_questions]&.map{ |result| result[:question] }
|
153
|
+
|
154
|
+
if depth > 1
|
155
|
+
depth_results[:related_questions]&.each do |question|
|
156
|
+
if question[:next_page_token]
|
157
|
+
get_depth_results(question[:next_page_token], depth - 1)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def get_related_questions(depth_limit = 0)
|
164
|
+
params = {
|
165
|
+
api_key: @api_key, # https://serpapi.com/manage-api-key
|
166
|
+
engine: 'google', # search engine
|
167
|
+
q: @query, # search query
|
168
|
+
google_domain: @domain, # Google domain to use
|
169
|
+
gl: @country, # country of the search
|
170
|
+
hl: @lang # language of the search
|
171
|
+
}
|
172
|
+
|
173
|
+
search = GoogleSearch.new(params) # data extraction on the SerpApi backend
|
174
|
+
results = search.get_hash # JSON -> Ruby hash
|
175
|
+
|
176
|
+
@related_questions_results = results[:related_questions]&.map{ |result| result[:question] }
|
177
|
+
|
178
|
+
depth_limit = 4 if depth_limit > 4
|
179
|
+
|
180
|
+
if depth_limit > 0
|
181
|
+
results[:related_questions]&.each do |question|
|
182
|
+
if question[:next_page_token]
|
183
|
+
get_depth_results(question[:next_page_token], depth_limit)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
@related_questions_results
|
189
|
+
end
|
190
|
+
|
191
|
+
def save_to_csv(data)
|
192
|
+
CSV.open("#{@query.gsub(' ', '_')}.csv", 'w') do |csv_file|
|
193
|
+
csv_file << data.keys
|
194
|
+
max_length = data.values.map(&:length).max
|
195
|
+
(0...max_length).each do |index|
|
196
|
+
csv_file << data.values.map { |value| value[index] }
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
def save_to_json(data)
|
202
|
+
File.open("#{@query.gsub(' ', '_')}.json", 'w') do |json_file|
|
203
|
+
json_file.write(JSON.pretty_generate(data))
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def save_to_txt(data)
|
208
|
+
File.open("#{@query.gsub(' ', '_')}.txt", "w") do |txt_file|
|
209
|
+
data.each do |key, values|
|
210
|
+
txt_file.puts("#{key}:")
|
211
|
+
values.each do |value|
|
212
|
+
txt_file.puts(" #{value}")
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
def print_data(data)
|
219
|
+
puts JSON.pretty_generate(data)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: seo-keywords-generator-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Chukhrai
|
@@ -36,6 +36,7 @@ extensions: []
|
|
36
36
|
extra_rdoc_files: []
|
37
37
|
files:
|
38
38
|
- bin/seo
|
39
|
+
- lib/seo-keywords-generator-ruby.rb
|
39
40
|
homepage: https://github.com/chukhraiartur/seo-keywords-generator-ruby
|
40
41
|
licenses:
|
41
42
|
- MIT
|