Class: SeoPositionTracker::CLI
- Inherits:
-
Object
- Object
- SeoPositionTracker::CLI
- Defined in:
- lib/seo-position-tracker-ruby.rb
Instance Method Summary collapse
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #run ⇒ Object
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
9 10 11 |
# File 'lib/seo-position-tracker-ruby.rb', line 9 def initialize(argv) @argv = argv end |
Instance Method Details
#run ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/seo-position-tracker-ruby.rb', line 13 def run = { query: 'coffee', target_keywords: ['coffee'], target_websites: ['starbucks.com'], search_engine: ['google', 'bing', 'duckduckgo', 'yahoo', 'yandex', 'naver'], api_key: '5868ece26d41221f5e19ae8b3e355d22db23df1712da675d144760fc30d57988', language: nil, country: nil, location: nil, domain: nil, save_to: 'CSV' } OptionParser.new do |opts| opts. = "Usage: seo [options]" opts.on('-q', '--query QUERY', String, 'Search query. Default "coffee".') { |q| [:query] = q } opts.on('-k', '--target-keywords KEYWORDS', Array, 'Target keywords to track. Default "coffee".') { |k| [:target_keywords] = k } opts.on('-w', '--target-websites WEBSITES', Array, 'Target websites to track. Default "starbucks.com".') { |w| [:target_websites] = w } opts.on('-e', '--search-engine ENGINES', Array, 'Choosing a search engine to track: "google", "bing", "duckduckgo", "yahoo", "yandex", "naver". You can select multiple search engines by separating them with a comma: google,bing. All search engines are selected by default.') { |e| [:search_engine] = e } opts.on('-a', '--api-key API_KEY', String, 'Your SerpApi API key: https://serpapi.com/manage-api-key. Default is a test API key to test CLI.') { |a| [:api_key] = a } opts.on('-l', '--language LANGUAGE', String, 'Language of the search. Supported only for "google", "yahoo" and "yandex" engines. Default is nil.') { |l| [:language] = l } opts.on('-c', '--country COUNTRY', String, 'Country of the search. Supported only for "google", "bing" and "yahoo" engines. Default is nil.') { |c| [:country] = c } opts.on('-p', '--location LOCATION', String, 'Location of the search. Supported only for "google", "bing", "duckduckgo" and "yandex" engines. Default is nil.') { |p| [:location] = p } opts.on('-d', '--domain DOMAIN', String, 'Search engine domain to use. Supported only for "google", "yahoo" and "yandex" engines. Default is nil.') { |d| [:domain] = d } opts.on('-s', '--save-to SAVE', String, 'Saves the results in the current directory in the selected format (CSV, JSON, TXT). Default CSV.') { |s| [:save_to] = s } end.parse!(@argv) tracker = SeoPositionTracker::Scraper.new( query=[:query], api_key=[:api_key], keywords=[:target_keywords], websites=[:target_websites], language=[:language], country=[:country], location=[:location], domain=[:domain] ) position_data = [] [:search_engine]&.each do |engine| case engine when 'google' data = tracker.scrape_google position_data.concat(data) when 'bing' data = tracker.scrape_bing position_data.concat(data) when 'duckduckgo' data = tracker.scrape_duckduckgo position_data.concat(data) when 'yahoo' data = tracker.scrape_yahoo position_data.concat(data) when 'yandex' data = tracker.scrape_yandex position_data.concat(data) when 'naver' data = tracker.scrape_naver position_data.concat(data) else puts "\"#{engine}\" is an unknown search engine." end end if position_data.any? tracker.print(position_data) puts "Saving data in #{[:save_to].upcase} format..." case [:save_to].upcase when 'CSV' tracker.save_to_csv(position_data) when 'JSON' tracker.save_to_json(position_data) when 'TXT' tracker.save_to_txt(position_data) end puts "Data successfully saved to #{[:query].gsub(" ", "_")}.#{[:save_to].downcase} file." else puts "Unfortunately, no matches were found." end end |