Module: Crabfarm::Modes::Recorder

Defined in:
lib/crabfarm/modes/recorder.rb

Class Method Summary collapse

Class Method Details

.memento_path(_name) ⇒ Object



9
10
11
# File 'lib/crabfarm/modes/recorder.rb', line 9

def self.memento_path(_name)
  File.join(GlobalState.mementos_path, _name + '.json.gz')
end

.start(_target, _replay = false) ⇒ 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
# File 'lib/crabfarm/modes/recorder.rb', line 13

def self.start(_target, _replay=false)
  return puts "Must provide a recording target" unless _target.is_a? String

  target_path = memento_path _target
  return puts "Memento file does not exist: #{target_path}" if _replay and not File.exist? target_path

  crabtrap_config = Crabfarm.config.crabtrap_config
  crabtrap_config[:mode] = _replay ? :replay : :capture
  crabtrap_config[:port] = Utils::PortDiscovery.find_available_port
  crabtrap_config[:bucket_path] = target_path

  crabtrap = CrabtrapRunner.new crabtrap_config
  crabtrap.start

  begin
    driver_config = Crabfarm.config.driver_config
    driver_config[:name] = Crabfarm.config.recorder_driver
    driver_config[:proxy] = "127.0.0.1:#{crabtrap.port}"

    driver = DefaultDriverFactory.new(driver_config).build_driver nil

    begin
      puts "Press Ctrl-C or close browser to stop #{_replay ? 'playback' : 'capturing'}."
      loop do
        driver.current_url
        sleep 1.0
      end
    rescue Selenium::WebDriver::Error::WebDriverError, SystemExit, Interrupt
      # noop
    end

    puts "Releasing crawling context".color(:green)
    driver.quit rescue nil
  ensure
    crabtrap.stop
  end
end