Class: Docapurl::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/docapurl/browser.rb

Constant Summary collapse

SYSTEM_MAX_PAGE_DOWN_TO_BOTTOM =
50

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) {|_self| ... } ⇒ Browser

Returns a new instance of Browser.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/docapurl/browser.rb', line 9

def initialize(options)
  @save_path = options[:save_path]
  options[:timeout] ||= 30

  options[:slowmo] = 0.5
  @logger = options[:logger] || Logger.new(STDOUT)
  @browser = Ferrum::Browser.new options
  @context = browser.contexts.create
  @page = @context.create_page
  yield(self) if block_given?
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



7
8
9
# File 'lib/docapurl/browser.rb', line 7

def browser
  @browser
end

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/docapurl/browser.rb', line 7

def context
  @context
end

#loggerObject

Returns the value of attribute logger.



7
8
9
# File 'lib/docapurl/browser.rb', line 7

def logger
  @logger
end

#pageObject

Returns the value of attribute page.



7
8
9
# File 'lib/docapurl/browser.rb', line 7

def page
  @page
end

#save_pathObject

Returns the value of attribute save_path.



7
8
9
# File 'lib/docapurl/browser.rb', line 7

def save_path
  @save_path
end

Class Method Details

.cap(url, path = nil, browser_options = {}, cap_options = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/docapurl/browser.rb', line 93

def cap(url, path = nil, browser_options = {}, cap_options = {})
  browser = new(browser_options)
  cap_options[:path] = path
  browser.cap(url, cap_options)
rescue StandardError => e
  browser.logger.error e
ensure
  browser&.close
end

Instance Method Details

#cap(url, options) ⇒ Object



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/docapurl/browser.rb', line 21

def cap(url, options)
  options[:quality] ||= 90
  options[:full] = true if options[:full].nil?
  options[:path] ||= @save_path
  host = fetch_domain(url)
  options[:path] ||= "screenshot-#{host.to_s == '' ? '' : "#{host}-"}#{Time.now.strftime('%F-%T')}.jpg"
  logger.info "browser begin to visit url #{url}"

  set_callback("before_visit_func", options)
  page.go_to(url)
  set_callback("after_visit_func", options)


  logger.info 'visited'
  max_pagedown = options[:max_pagedown] || 5
  pagedown_to_bottom = options.delete :pagedown_to_bottom
  visit_whole_page(page: page, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom)

  sleep_before_screen = options.delete :sleep_before_screen
  logger.info "sleep #{sleep_before_screen.to_i} second before screenshot"
  sleep(sleep_before_screen.to_i)


  set_callback("before_screenshot_func", options)
  page.screenshot(**options)
  set_callback("after_screenshot_func", options)

  logger.info "screenshot ended, path = #{options[:path]}"
end

#closeObject



51
52
53
54
55
56
57
# File 'lib/docapurl/browser.rb', line 51

def close
  return if browser.nil?
  context.dispose unless context.nil?

  logger.info 'close browser'
  browser.quit
end

#visit_whole_page(page: nil, max_pagedown: nil, pagedown_to_bottom: false) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/docapurl/browser.rb', line 59

def visit_whole_page( page: nil, max_pagedown: nil, pagedown_to_bottom: false)
  viewport_height = page.viewport_size.last.to_i
  document_height = page.document_size.last.to_i
  return if document_height<= viewport_height

  page_down_count = document_height / viewport_height
  if pagedown_to_bottom
    page_down_count = SYSTEM_MAX_PAGE_DOWN_TO_BOTTOM if page_down_count >SYSTEM_MAX_PAGE_DOWN_TO_BOTTOM
  else
    page_down_count = max_pagedown if page_down_count > max_pagedown

  end

  page_down_count.times do
    logger.info "press PageDown .."
    page.keyboard.type(:PageDown)
  end
  logger.info "press HOME .."
  page.keyboard.type(:Home)
end