Class: Docapurl::Browser
- Inherits:
-
Object
- Object
- Docapurl::Browser
- Defined in:
- lib/docapurl/browser.rb
Constant Summary collapse
- SYSTEM_MAX_PAGE_DOWN_TO_BOTTOM =
50
Instance Attribute Summary collapse
-
#browser ⇒ Object
Returns the value of attribute browser.
-
#context ⇒ Object
Returns the value of attribute context.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#page ⇒ Object
Returns the value of attribute page.
-
#save_path ⇒ Object
Returns the value of attribute save_path.
Class Method Summary collapse
Instance Method Summary collapse
- #cap(url, options) ⇒ Object
- #close ⇒ Object
-
#initialize(options) {|_self| ... } ⇒ Browser
constructor
A new instance of Browser.
- #visit_whole_page(page: nil, max_pagedown: nil, pagedown_to_bottom: false) ⇒ Object
Constructor Details
#initialize(options) {|_self| ... } ⇒ Browser
Returns a new instance of Browser.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/docapurl/browser.rb', line 9 def initialize() @save_path = [:save_path] [:timeout] ||= 30 [:slowmo] = 0.5 @logger = [:logger] || Logger.new(STDOUT) @browser = Ferrum::Browser.new @context = browser.contexts.create @page = @context.create_page yield(self) if block_given? end |
Instance Attribute Details
#browser ⇒ Object
Returns the value of attribute browser.
7 8 9 |
# File 'lib/docapurl/browser.rb', line 7 def browser @browser end |
#context ⇒ Object
Returns the value of attribute context.
7 8 9 |
# File 'lib/docapurl/browser.rb', line 7 def context @context end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/docapurl/browser.rb', line 7 def logger @logger end |
#page ⇒ Object
Returns the value of attribute page.
7 8 9 |
# File 'lib/docapurl/browser.rb', line 7 def page @page end |
#save_path ⇒ Object
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 = new() [:path] = path browser.cap(url, ) 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, ) [:quality] ||= 90 [:full] = true if [:full].nil? [:path] ||= @save_path host = fetch_domain(url) [: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", ) page.go_to(url) set_callback("after_visit_func", ) logger.info 'visited' max_pagedown = [:max_pagedown] || 5 pagedown_to_bottom = .delete :pagedown_to_bottom visit_whole_page(page: page, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom) sleep_before_screen = .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", ) page.screenshot(**) set_callback("after_screenshot_func", ) logger.info "screenshot ended, path = #{[:path]}" end |
#close ⇒ Object
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) = page..last.to_i document_height = page.document_size.last.to_i return if document_height<= page_down_count = document_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 |