class MeatSauce::CLI
Public Instance Methods
capture()
click to toggle source
# File lib/meat_sauce/cli.rb, line 68 def capture links = show_links(true) open_driver do |driver| if options[:depth_limit] == 0 # スクリーンショットを取る driver.save_screenshot(options[:file_path]) else links.each_with_index do |link, idx| driver.navigate.to(link.to_s) driver.save_screenshot(path_add_suffix(options[:file_path], idx)) end end end end
open_driver() { |driver| ... }
click to toggle source
# File lib/meat_sauce/cli.rb, line 100 def open_driver # ブラウザ起動 # :chrome, :firefox, :safari, :ie, :operaなどに変更可能 driver = Selenium::WebDriver.for(options[:browser].to_sym) # urlにアクセス driver.navigate.to(url_complement(options[:url])) yield(driver) if block_given? rescue => ex say ex.message, :red ensure # ブラウザ終了 driver.quit if defined?(driver) end
path_add_suffix(path, idx)
click to toggle source
# File lib/meat_sauce/cli.rb, line 117 def path_add_suffix(path, idx) dir_path ||= File.dirname(path) ext_name ||= File.extname(path) file_name = File.basename(path, ext_name) [dir_path, "/" ,file_name,"_#{idx.to_s}",ext_name].join end
scraping()
click to toggle source
# File lib/meat_sauce/cli.rb, line 19 def scraping open_driver do |driver| html = driver.page_source doc = Nokogiri::HTML(html) doc.css(options[:tag]).each do |tag| if options[:only_text] puts tag.inner_text else puts tag.inner_html end end end end
show_links(call_as_function = false)
click to toggle source
# File lib/meat_sauce/cli.rb, line 45 def show_links(call_as_function = false) opts = { skip_query_strings: options[:skip_query_string] } opts.merge!(depth_limit: options[:depth_limit]) unless options[:depth_limit].nil? url = [] Anemone.crawl(url_complement(options[:url]), opts) do |anemone| anemone.on_every_page do |page| url << page.url end end if call_as_function return url else puts url.join("\n") end end
show_source()
click to toggle source
# File lib/meat_sauce/cli.rb, line 36 def show_source open_driver do |driver| puts driver.page_source end end
Private Instance Methods
url_complement(url_path)
click to toggle source
# File lib/meat_sauce/cli.rb, line 86 def url_complement(url_path) if url_path.nil? say("'--url' または '-u'オプションでurlを指定してください (No value provided for required options '--url')", :red) end url = url_path unless url_path.start_with?("http://") url = "http://" + url end raise UrlFormatError, "invalid url" if URI.regexp.match(url).nil? return url end