Class: WebsiteScreenshot
- Inherits:
-
Object
- Object
- WebsiteScreenshot
- Defined in:
- lib/website_screenshot.rb
Constant Summary collapse
- VERSION =
Gem::Specification.load(File.("../website_screenshot.gemspec", File.dirname(__FILE__))).version.to_s
- @@state =
"waiting"
- @@progress =
0
- @@started_at =
nil
Instance Attribute Summary collapse
-
#check_loading_status_interval ⇒ Object
:nodoc:.
-
#file_name ⇒ Object
Filename to save the file to.
-
#render_timeout ⇒ Object
Timeout before killing the page.
-
#size ⇒ Object
Window size.
-
#url ⇒ Object
Page url.
-
#verbose ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#get ⇒ Object
Renders the website and saves a screenshot.
-
#initialize(args) ⇒ WebsiteScreenshot
constructor
Instantiates a new object.
Constructor Details
#initialize(args) ⇒ WebsiteScreenshot
Instantiates a new object.
Options:
- url
-
Website path. URL redirects are automatically resolved using
curl
. - file_name
-
Name of the saved image. Defaults to output.png.
- render_timeout
-
Timeout before killing the page. Defaults at two minutes.
- check_loading_status_interval
-
Interval between page status checks.
- size
-
Window size the page is being rendered into. Defaults at 1360x768.
- verbose
-
Outputs page load progress.
43 44 45 46 47 48 49 50 |
# File 'lib/website_screenshot.rb', line 43 def initialize(args) self.render_timeout = args[:render_timeout] || 120 self.check_loading_status_interval = args[:check_loading_status_interval] || 0.1 self.file_name = args[:file_name] || "output.png" self.size = args[:size] || "1360x768" self.url = args[:url] self.verbose = args[:verbose] # default FALSE end |
Instance Attribute Details
#check_loading_status_interval ⇒ Object
:nodoc:
22 23 24 |
# File 'lib/website_screenshot.rb', line 22 def check_loading_status_interval @check_loading_status_interval end |
#file_name ⇒ Object
Filename to save the file to.
25 26 27 |
# File 'lib/website_screenshot.rb', line 25 def file_name @file_name end |
#render_timeout ⇒ Object
Timeout before killing the page.
20 21 22 |
# File 'lib/website_screenshot.rb', line 20 def render_timeout @render_timeout end |
#size ⇒ Object
Window size.
28 29 30 |
# File 'lib/website_screenshot.rb', line 28 def size @size end |
#url ⇒ Object
Page url.
31 32 33 |
# File 'lib/website_screenshot.rb', line 31 def url @url end |
#verbose ⇒ Object
:nodoc:
33 34 35 |
# File 'lib/website_screenshot.rb', line 33 def verbose @verbose end |
Instance Method Details
#get ⇒ Object
Renders the website and saves a screenshot.
Returns:
-
If the webpage began rendering, the load percentage. A screenshot is saved if the page has been
50%
loaded or more. -
false
if for some reason theurl
could not be opened, or the browser initialized.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/website_screenshot.rb', line 56 def get app = Qt::Application.new(ARGV) webview = Qt::WebView.new() webview.connect(SIGNAL("loadStarted()")) do @@started_at = Time.now.to_i end webview.connect(SIGNAL("loadFinished(bool)")) do |result| if result @@state = "finished-success" else @@state = "finished-fail" @@progress = false end suspend_thread # Give it enough time to switch to the sentinel thread and avoid an empty exec loop. end webview.connect(SIGNAL("loadProgress(int)")) do |progress| puts "#{progress}%" if verbose @@progress = progress suspend_thread if has_reached_time_out? end # Enable flash, javascript and some other sensible browsing options. webview::settings()::setAttribute(Qt::WebSettings::PluginsEnabled, true) webview::settings()::setAttribute(Qt::WebSettings::JavascriptCanOpenWindows, false) webview::settings()::setAttribute(Qt::WebSettings::PrivateBrowsingEnabled, true) webview::settings()::setAttribute(Qt::WebSettings::JavascriptEnabled, true) # Hide the scrollbars. webview.page.mainFrame.setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff) webview.page.mainFrame.setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff) webview.load(Qt::Url.new(url)) webview.resize(size) webview.show render_page_thread = Thread.new do app.exec end check_status_thread = Thread.new do while true do sleep check_loading_status_interval if @@state =~ /^finished/ || has_reached_time_out? # Save a screenshot if page finished loaded or it has timed out with 50%+ completion. save(webview) if @@state == "finished-success" || @@progress >= 50 render_page_thread.kill break end end end check_status_thread.join render_page_thread.join return @@progress end |