class Kimurai::CLI

Public Instance Methods

__print_version() click to toggle source
# File lib/kimurai/cli.rb, line 154
def __print_version
  puts VERSION
end
console(spider_name = nil) click to toggle source
# File lib/kimurai/cli.rb, line 103
def console(spider_name = nil)
  require 'pry'
  require './config/boot' if inside_project?

  if spider_name
    raise "Can't find Kimurai project" unless inside_project?

    unless klass = Kimurai.find_by_name(spider_name)
      raise "Can't find spider with name `#{spider_name}` in the project. " \
        "To list all available spiders, run: `$ bundle exec kimurai list`"
    end
  else
    klass = inside_project? ? ApplicationSpider : ::Kimurai::Base
  end

  engine = options["engine"]&.delete(":")&.to_sym
  if url = options["url"]
    klass.new(engine).request_to(:console, url: options["url"])
  else
    klass.new(engine).public_send(:console)
  end
end
crawl(spider_name) click to toggle source
# File lib/kimurai/cli.rb, line 69
def crawl(spider_name)
  raise "Can't find Kimurai project" unless inside_project?
  require './config/boot'

  unless klass = Kimurai.find_by_name(spider_name)
    raise "Can't find spider with name `#{spider_name}` in the project. " \
      "To list all available spiders, run: `$ bundle exec kimurai list`"
  end

  # Set time_zone if exists
  if time_zone = Kimurai.configuration.time_zone
    Kimurai.time_zone = time_zone
  end

  klass.crawl!
end
dashboard() click to toggle source
# File lib/kimurai/cli.rb, line 159
def dashboard
  raise "Can't find Kimurai project" unless inside_project?

  require './config/boot'
  if Object.const_defined?("Kimurai::Dashboard")
    require 'kimurai/dashboard/app'
    Kimurai::Dashboard::App.run!
  else
    raise "Kimurai::Dashboard is not defined"
  end
end
deploy(user_host) click to toggle source
# File lib/kimurai/cli.rb, line 46
def deploy(user_host)
  if !`git status --short`.empty?
    raise "Deploy: Please commit your changes first"
  elsif `git remote`.empty?
    raise "Deploy: Please add remote origin repository to your repo first"
  elsif !`git rev-list master...origin/master`.empty?
    raise "Deploy: Please push your commits to the remote origin repo first"
  end

  repo_url = options["repo-url"] ? options["repo-url"] : `git remote get-url origin`.strip
  repo_name = repo_url[/\/([^\/]*)\.git/i, 1]

  command = AnsibleCommandBuilder.new(user_host, options, playbook: "deploy",
    vars: { repo_url: repo_url, repo_name: repo_name, repo_key_path: options["repo-key-path"] }
  ).get

  pid = spawn *command
  Process.wait pid
end
generate(generator_type, *args) click to toggle source
# File lib/kimurai/cli.rb, line 8
def generate(generator_type, *args)
  case generator_type
  when "project"
    project_name = args.shift
    raise "Provide project name to generate a new project" unless project_name.present?
    Generator.new.generate_project(project_name)
  when "spider"
    spider_name = args.shift
    raise "Provide spider name to generate a spider" unless spider_name.present?
    Generator.new.generate_spider(spider_name, in_project: inside_project?)
  when "schedule"
    Generator.new.generate_schedule
  else
    raise "Don't know this generator type: #{generator_type}"
  end
end
list() click to toggle source
# File lib/kimurai/cli.rb, line 127
def list
  raise "Can't find Kimurai project" unless inside_project?
  require './config/boot'

  Kimurai.list.keys.each { |name| puts name }
end
parse(spider_name, method_name) click to toggle source
# File lib/kimurai/cli.rb, line 88
def parse(spider_name, method_name)
  raise "Can't find Kimurai project" unless inside_project?
  require './config/boot'

  unless klass = Kimurai.find_by_name(spider_name)
    raise "Can't find spider with name `#{spider_name}` in the project. " \
      "To list all available spiders, run: `$ bundle exec kimurai list`"
  end

  klass.parse!(method_name, url: options["url"])
end
runner() click to toggle source
# File lib/kimurai/cli.rb, line 138
def runner
  raise "Can't find Kimurai project" unless inside_project?

  jobs = options["jobs"]
  raise "Jobs count can't be 0" if jobs == 0

  require './config/boot'
  require 'kimurai/runner'

  spiders = options["include"].presence || Kimurai.list.keys
  spiders -= options["exclude"]

  Runner.new(spiders, jobs).run!
end
setup(user_host) click to toggle source
# File lib/kimurai/cli.rb, line 33
def setup(user_host)
  command = AnsibleCommandBuilder.new(user_host, options, playbook: "setup").get

  pid = spawn *command
  Process.wait pid
end

Private Instance Methods

inside_project?() click to toggle source
# File lib/kimurai/cli.rb, line 173
def inside_project?
  Dir.exists?("spiders") && File.exists?("./config/boot.rb")
end