Class: Ronin::Web::CLI::Commands::Screenshot Private

Inherits:
Ronin::Web::CLI::Command show all
Includes:
Core::CLI::Logging, BrowserOptions
Defined in:
lib/ronin/web/cli/commands/screenshot.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Screenshots one or more URLs.

Usage

ronin-web screenshot [options] {URL [...] | --file FILE}

Options

-B, --browser NAME|PATH          The browser name or path to execute
-W, --width WIDTH                Sets the width of the browser viewport (Default: 1024)
-H, --height HEIGHT              Sets the height of the browser viewport (Default: 768)
-f, --file FILE                  Input file to read URLs from
-F, --format png|jpg             Screenshot image format (Default: png)
-d, --directory DIR              Directory to save images to (Default: /data/home/postmodern/code/ronin-rb/ronin-web)
    --full                       Screenshots the full page
-C, --css-path CSSPath           The CSSpath selector to screenshot
-h, --help                       Print help information

Arguments

URL ...                          The URL visit and screenshot

Since:

  • 1.0.0

Instance Method Summary collapse

Methods included from BrowserOptions

#browser, #browser_kwargs, included

Instance Method Details

#image_path_for(url) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Generates the image path for a given URL.

Parameters:

  • url (String)

    The given URL.

Returns:

  • (String)

    The relative image path that represents the URL.

Since:

  • 1.0.0



172
173
174
175
176
177
178
179
180
# File 'lib/ronin/web/cli/commands/screenshot.rb', line 172

def image_path_for(url)
  uri = parse_url(url)

  path = File.join(options[:directory],uri.host,uri.request_uri)
  path << 'index' if path.end_with?('/')
  path << ".#{options[:format]}"

  return path
end

#parse_url(url) ⇒ URI::HTTP, URI::HTTPS

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses a URL.

Parameters:

  • url (String)

    The URL string to parse.

Returns:

  • (URI::HTTP, URI::HTTPS)

    The parsed URL.

Since:

  • 1.0.0



156
157
158
159
160
161
# File 'lib/ronin/web/cli/commands/screenshot.rb', line 156

def parse_url(url)
  URI.parse(url)
rescue URI::InvalidURI
  print_error "invalid URI: #{url}"
  exit(1)
end

#process_url(url) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Visits and screenshots a URL.

Parameters:

  • url (String)

    The URL to screenshot.

Since:

  • 1.0.0



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ronin/web/cli/commands/screenshot.rb', line 128

def process_url(url)
  begin
    browser.goto(url)
  rescue Ferrum::StatusError
    print_error "failed to request URL: #{url}"
  end

  image_path = image_path_for(url)
  FileUtils.mkdir_p(File.dirname(image_path))

  log_info "Screenshotting #{url} to #{image_path} ..."
  browser.screenshot(
    path:     image_path,
    format:   options[:format],
    full:     options[:full],
    selector: options[:css_path]
  )
end

#run(*urls) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the ronin-web screenshot command.

Parameters:

  • urls (Array<String>)

    The URLs to screenshot.

Since:

  • 1.0.0



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ronin/web/cli/commands/screenshot.rb', line 105

def run(*urls)
  if options[:file]
    File.open(options[:file]) do |file|
      file.each_line(chomp: true) do |url|
        process_url(url)
      end
    end
  elsif !urls.empty?
    urls.each do |url|
      process_url(url)
    end
  else
    print_error "must specify --file or URL arguments"
    exit(-1)
  end
end