seo-keywords-generator-ruby 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ee75c64d14f6d9ba266c24e5e3827e734ee29ae1307fa4b3de68bcd76a74daac
4
+ data.tar.gz: 6e7fb88acc00304a75a9132a5568cbc122287b7ea4aefcc5202e54113d7b4cd3
5
+ SHA512:
6
+ metadata.gz: 62a676454f8197cdca3ba232a4adbb2fe2679d62a2eb04d22d35ef2575c9ae09e0694edff7ca7f1356b2dba614499d6532f98679b013b0a3af019f654608077f
7
+ data.tar.gz: 88076f3fcfd8daffe59cd16e5b645c5366ada0af7c394e4b3e88cd50d5a0f18905ef46c4704dc75c71c7058e57c0aafceb89f4dd86637e67fe2d294fb83e6925
@@ -0,0 +1,129 @@
1
+ require 'google_search_results'
2
+ require 'json'
3
+ require 'csv'
4
+
5
+
6
+ class SeoKeywordsGenerator
7
+ attr_accessor :query, :api_key, :lang, :country, :domain
8
+
9
+ def initialize(query, api_key, lang = 'en', country = 'us', domain = 'google.com')
10
+ @query = query
11
+ @api_key = api_key
12
+ @lang = lang
13
+ @country = country
14
+ @domain = domain
15
+ @related_questions_results = []
16
+ end
17
+
18
+ def get_auto_complete
19
+ params = {
20
+ api_key: @api_key, # https://serpapi.com/manage-api-key
21
+ engine: 'google_autocomplete', # search engine
22
+ q: @query, # search query
23
+ gl: @country, # country of the search
24
+ hl: @lang # language of the search
25
+ }
26
+
27
+ search = GoogleSearch.new(params) # data extraction on the SerpApi backend
28
+ results = search.get_hash # JSON -> Ruby hash
29
+
30
+ results[:suggestions].map{ |result| result[:value] }.compact
31
+ end
32
+
33
+ def get_related_searches
34
+ params = {
35
+ api_key: @api_key, # https://serpapi.com/manage-api-key
36
+ engine: 'google', # search engine
37
+ q: @query, # search query
38
+ google_domain: @domain, # Google domain to use
39
+ gl: @country, # country of the search
40
+ hl: @lang # language of the search
41
+ }
42
+
43
+ search = GoogleSearch.new(params) # data extraction on the SerpApi backend
44
+ results = search.get_hash # JSON -> Ruby hash
45
+
46
+ results[:related_searches].map{ |result| result[:query] }.compact
47
+ end
48
+
49
+ def get_depth_results(token, depth)
50
+ depth_params = {
51
+ q: @query,
52
+ api_key: @api_key,
53
+ engine: 'google_related_questions',
54
+ next_page_token: token
55
+ }
56
+
57
+ depth_search = GoogleSearch.new(depth_params)
58
+ depth_results = depth_search.get_hash
59
+
60
+ @related_questions_results += depth_results[:related_questions]&.map{ |result| result[:question] }
61
+
62
+ if depth > 1
63
+ depth_results[:related_questions]&.each do |question|
64
+ if question[:next_page_token]
65
+ get_depth_results(question[:next_page_token], depth - 1)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def get_related_questions(depth_limit = 0)
72
+ params = {
73
+ api_key: @api_key, # https://serpapi.com/manage-api-key
74
+ engine: 'google', # search engine
75
+ q: @query, # search query
76
+ google_domain: @domain, # Google domain to use
77
+ gl: @country, # country of the search
78
+ hl: @lang # language of the search
79
+ }
80
+
81
+ search = GoogleSearch.new(params) # data extraction on the SerpApi backend
82
+ results = search.get_hash # JSON -> Ruby hash
83
+
84
+ @related_questions_results = results[:related_questions]&.map{ |result| result[:question] }
85
+
86
+ depth_limit = 4 if depth_limit > 4
87
+
88
+ if depth_limit > 0
89
+ results[:related_questions]&.each do |question|
90
+ if question[:next_page_token]
91
+ get_depth_results(question[:next_page_token], depth_limit)
92
+ end
93
+ end
94
+ end
95
+
96
+ @related_questions_results
97
+ end
98
+
99
+ def save_to_csv(data)
100
+ CSV.open("#{@query.gsub(' ', '_')}.csv", 'w') do |csv_file|
101
+ csv_file << data.keys
102
+ max_length = data.values.map(&:length).max
103
+ (0...max_length).each do |index|
104
+ csv_file << data.values.map { |value| value[index] }
105
+ end
106
+ end
107
+ end
108
+
109
+ def save_to_json(data)
110
+ File.open("#{@query.gsub(' ', '_')}.json", 'w') do |json_file|
111
+ json_file.write(JSON.pretty_generate(data))
112
+ end
113
+ end
114
+
115
+ def save_to_txt(data)
116
+ File.open("#{@query.gsub(' ', '_')}.txt", "w") do |txt_file|
117
+ data.each do |key, values|
118
+ txt_file.puts("#{key}:")
119
+ values.each do |value|
120
+ txt_file.puts(" #{value}")
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ def print_data(data)
127
+ puts JSON.pretty_generate(data)
128
+ end
129
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: seo-keywords-generator-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Chukhrai
8
+ - Dmitiry Zub
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2023-05-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Ruby SEO keywords suggestion tool. Google Autocomplete, People Also Ask
15
+ and Related Searches.
16
+ email:
17
+ - chukhraiartur@gmail.com
18
+ - dimitryzub@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/seo-keywords-generator-ruby.rb
24
+ homepage: https://github.com/chukhraiartur/seo-keywords-generator-ruby
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.0.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.2.32
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Ruby SEO Keywords Generator
47
+ test_files: []