forked from adamn/python-webkit2png
-
Notifications
You must be signed in to change notification settings - Fork 3
/
webkit2png.py
executable file
·520 lines (440 loc) · 21.5 KB
/
webkit2png.py
1
2
3
4
5
6
7
8
9
10
11
12
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
50
51
52
53
54
55
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#!/usr/bin/env python
#
# webkit2png.py
#
# Creates screenshots of webpages using by QtWebkit.
#
# Copyright (c) 2008 Roland Tapken <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
# Nice ideas "todo":
# - Add QTcpSocket support to create a "screenshot daemon" that
# can handle multiple requests at the same time.
import sys
import signal
import os
import logging
import time
import urlparse
from optparse import OptionParser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4.QtNetwork import *
VERSION="20091224"
LOG_FILENAME = 'webkit2png.log'
logger = logging.getLogger('webkit2png');
# Class for Website-Rendering. Uses QWebPage, which
# requires a running QtGui to work.
class WebkitRenderer(QObject):
"""A class that helps to create 'screenshots' of webpages using
Qt's QWebkit. Requires PyQt4 library.
Use "render()" to get a 'QImage' object, render_to_bytes() to get the
resulting image as 'str' object or render_to_file() to write the image
directly into a 'file' resource.
These methods have to be called from within Qt's main (GUI) thread.
An example on how to use this is the __qt_main() method at the end
of the libraries source file. More generic examples:
def qt_main():
while go_on():
do_something_meaningful()
while QApplication.hasPendingEvents():
QApplication.processEvents()
QApplication.quit()
app = init_qtgui()
QTimer.singleShot(0, qt_main)
sys.exit(app.exec_())
Or let Qt handle event processing using a QTimer instance:
def qt_main_loop():
if not go_on():
QApplication.quit()
return
do_something_meaningful()
app = init_qtgui()
main_timer = QTimer()
QObject.connect(main_timer, QtCore.SIGNAL("timeout()"), qt_main_loop)
sys.exit(app.exec_())
Avaible properties:
width -- The width of the "browser" window. 0 means autodetect (default).
height -- The height of the window. 0 means autodetect (default).
timeout -- Seconds after that the request is aborted (default: 0)
wait -- Seconds to wait after loading has been finished (default: 0)
scaleToWidth -- The resulting image is scaled to this width.
scaleToHeight -- The resulting image is scaled to this height.
scaleRatio -- The image is scaled using this method. Possible values are:
keep
expand
crop
ignore
grabWhileWindow -- If this is True a screenshot of the whole window is taken. Otherwise only the current frame is rendered. This is required for plugins to be visible, but it is possible that another window overlays the current one while the screenshot is taken. To reduce this possibility, the window is activated just before it is rendered if this property is set to True (default: False).
qWebSettings -- Settings that should be assigned to the created QWebPage instance. See http://doc.trolltech.com/4.6/qwebsettings.html for possible keys. Defaults:
JavascriptEnabled: False
PluginsEnabled: False
PrivateBrowsingEnabled: True
JavascriptCanOpenWindows: False
"""
def __init__(self,**kwargs):
"""Sets default values for the properties."""
if not QApplication.instance():
raise RuntimeError(self.__class__.__name__ + " requires a running QApplication instance")
QObject.__init__(self)
# Initialize default properties
self.width = kwargs.get('width', 0)
self.height = kwargs.get('height', 0)
self.timeout = kwargs.get('timeout', 0)
self.wait = kwargs.get('wait', 0)
self.scaleToWidth = kwargs.get('scaleToWidth', 0)
self.scaleToHeight = kwargs.get('scaleToHeight', 0)
self.scaleRatio = kwargs.get('scaleRatio', 'keep')
self.scaleTransform = kwargs.get('scaleTransform', 'fast')
# Set this to true if you want to capture flash.
# Not that your desktop must be large enough for
# fitting the whole window.
self.grabWholeWindow = kwargs.get('grabWholeWindow', False)
self.ignoreAlerts = kwargs.get('ignoreAlerts', False)
self.ignoreConfirms = kwargs.get('ignoreConfirms', False)
self.ignoreConsoleMessages = kwargs.get('ignoreConsoleMessages', False)
# Set some default options for QWebPage
self.qWebSettings = {
QWebSettings.JavascriptEnabled : False,
QWebSettings.PluginsEnabled : False,
QWebSettings.PrivateBrowsingEnabled : True,
QWebSettings.JavascriptCanOpenWindows : False
}
def render(self, url):
"""Renders the given URL into a QImage object"""
# We have to use this helper object because
# QApplication.processEvents may be called, causing
# this method to get called while it has not returned yet.
helper = _WebkitRendererHelper(self)
image = helper.render(url)
# Bind helper instance to this image to prevent the
# object from being cleaned up (and with it the QWebPage, etc)
# before the data has been used.
image.helper = helper
return image
def render_to_file(self, url, file):
"""Renders the image into a File resource.
Returns the size of the data that has been written.
"""
format = self.format # this may not be constant due to processEvents()
image = self.render(url)
qBuffer = QBuffer()
image.save(qBuffer, format)
file.write(qBuffer.buffer().data())
return qBuffer.size()
def render_to_bytes(self, url):
"""Renders the image into an object of type 'str'"""
format = self.format # this may not be constant due to processEvents()
image = self.render(url)
qBuffer = QBuffer()
image.save(qBuffer, format)
return qBuffer.buffer().data()
class ConfigurableWebPage(QWebPage):
def __init__(self, parent=None, ignoreAlerts=False, ignoreConfirms=False, ignoreConsoleMessages=False):
super(ConfigurableWebPage, self).__init__(parent)
self.ignoreAlerts = ignoreAlerts
self.ignoreConfirms = ignoreConfirms
self.ignoreConsoleMessages = ignoreConsoleMessages
def javaScriptAlert(self, frame, msg):
if not self.ignoreAlerts:
return super(ConfigurableWebPage, self).javaScriptAlert(frame, msg)
def javaScriptConfirm(self, frame, msg):
if not self.ignoreConfirms:
return super(ConfigurableWebPage, self).javaScriptConfirm(frame, msg)
else:
return False
def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
if not self.ignoreConsoleMessages:
return super(ConfigurableWebPage, self).javaScriptConsoleMessage(message, lineNumber, sourceID)
class _WebkitRendererHelper(QObject):
"""This helper class is doing the real work. It is required to
allow WebkitRenderer.render() to be called "asynchronously"
(but always from Qt's GUI thread).
"""
def __init__(self, parent):
"""Copies the properties from the parent (WebkitRenderer) object,
creates the required instances of QWebPage, QWebView and QMainWindow
and registers some Slots.
"""
QObject.__init__(self)
# Copy properties from parent
for key,value in parent.__dict__.items():
setattr(self,key,value)
# Create and connect required PyQt4 objects
self._page = ConfigurableWebPage(None, self.ignoreAlerts, self.ignoreConfirms, self.ignoreConsoleMessages)
self._view = QWebView()
self._view.setPage(self._page)
self._window = QMainWindow()
self._window.setCentralWidget(self._view)
# Import QWebSettings
for key, value in self.qWebSettings.iteritems():
self._page.settings().setAttribute(key, value)
# Connect required event listeners
self.connect(self._page, SIGNAL("loadFinished(bool)"), self._on_load_finished)
self.connect(self._page, SIGNAL("loadStarted()"), self._on_load_started)
self.connect(self._page.networkAccessManager(), SIGNAL("sslErrors(QNetworkReply *,const QList<QSslError>&)"), self._on_ssl_errors)
# The way we will use this, it seems to be unesseccary to have Scrollbars enabled
self._page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self._page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
self._page.settings().setUserStyleSheetUrl(QUrl("data:text/css,html,body{overflow-y:hidden !important;}"))
# Show this widget
self._window.show()
def __del__(self):
"""Clean up Qt4 objects. """
self._window.close()
del self._window
del self._view
del self._page
def render(self, url):
"""The real worker. Loads the page (_load_page) and awaits
the end of the given 'delay'. While it is waiting outstanding
QApplication events are processed.
After the given delay, the Window or Widget (depends
on the value of 'grabWholeWindow' is drawn into a QPixmap
and postprocessed (_post_process_image).
"""
self._load_page(url, self.width, self.height, self.timeout)
# Wait for end of timer. In this time, process
# other outstanding Qt events.
if self.wait > 0:
logger.debug("Waiting %d seconds " % self.wait)
waitToTime = time.time() + self.wait
while time.time() < waitToTime:
while QApplication.hasPendingEvents():
QApplication.processEvents()
# Paint this frame into an image
#self._window.repaint()
while QApplication.hasPendingEvents():
QApplication.processEvents()
if self.grabWholeWindow:
# Note that this does not fully ensure that the
# window still has the focus when the screen is
# grabbed. This might result in a race condition.
self._view.activateWindow()
image = QPixmap.grabWindow(self._window.winId())
else:
image = QPixmap.grabWidget(self._window)
## Another possible drawing solution
#image = QImage(self._page.viewportSize(), QImage.Format_ARGB32)
#painter = QPainter(image)
#self._page.mainFrame().render(painter)
#painter.end()
return self._post_process_image(image)
def _load_page(self, url, width, height, timeout):
"""
This method implements the logic for retrieving and displaying
the requested page.
"""
# This is an event-based application. So we have to wait until
# "loadFinished(bool)" raised.
cancelAt = time.time() + timeout
self.__loading = True
self.__loadingResult = False # Default
# TODO: fromEncoded() needs to be used in some situations. Some
# sort of flag should be passed in to WebkitRenderer maybe?
#self._page.mainFrame().load(QUrl.fromEncoded(url))
self._page.mainFrame().load(QUrl(url))
while self.__loading:
if timeout > 0 and time.time() >= cancelAt:
raise RuntimeError("Request timed out on %s" % url)
while QApplication.hasPendingEvents():
QCoreApplication.processEvents()
logger.debug("Processing result")
if self.__loading_result == False:
logger.warning("Failed to load %s" % url)
# Set initial viewport (the size of the "window")
size = self._page.mainFrame().contentsSize()
logger.debug("contentsSize: %s", size)
if width > 0:
size.setWidth(width)
if height > 0:
size.setHeight(height)
self._window.resize(size)
def _post_process_image(self, qImage):
"""If 'scaleToWidth' or 'scaleToHeight' are set to a value
greater than zero this method will scale the image
using the method defined in 'scaleRatio'.
"""
if self.scaleToWidth > 0 or self.scaleToHeight > 0:
# Scale this image
if self.scaleRatio == 'keep':
ratio = Qt.KeepAspectRatio
elif self.scaleRatio in ['expand', 'crop']:
ratio = Qt.KeepAspectRatioByExpanding
else: # 'ignore'
ratio = Qt.IgnoreAspectRatio
if self.scaleTransform == 'smooth':
transform = Qt.SmoothTransformation
else:
transform = Qt.FastTransformation
qImage = qImage.scaled(self.scaleToWidth, self.scaleToHeight, ratio, transform)
if self.scaleRatio == 'crop':
qImage = qImage.copy(0, 0, self.scaleToWidth, self.scaleToHeight)
return qImage
# Eventhandler for "loadStarted()" signal
def _on_load_started(self):
"""Slot that sets the '__loading' property to true."""
logger.debug("loading started")
self.__loading = True
# Eventhandler for "loadFinished(bool)" signal
def _on_load_finished(self, result):
"""Slot that sets the '__loading' property to false and stores
the result code in '__loading_result'.
"""
logger.debug("loading finished with result %s", result)
self.__loading = False
self.__loading_result = result
# Eventhandler for "sslErrors(QNetworkReply *,const QList<QSslError>&)" signal
def _on_ssl_errors(self, reply, errors):
"""Slot that writes SSL warnings into the log but ignores them."""
for e in errors:
logger.warn("SSL: " + e.errorString())
reply.ignoreSslErrors()
def init_qtgui(display=None, style=None, qtargs=[]):
"""Initiates the QApplication environment using the given args."""
if QApplication.instance():
logger.debug("QApplication has already been instantiated. \
Ignoring given arguments and returning existing QApplication.")
return QApplication.instance()
qtargs2 = [sys.argv[0]]
if display:
qtargs2.append('-display')
qtargs2.append(display)
# Also export DISPLAY var as this may be used
# by flash plugin
os.environ["DISPLAY"] = display
if style:
qtargs2.append('-style')
qtargs2.append(style)
qtargs2.extend(qtargs)
return QApplication(qtargs2)
if __name__ == '__main__':
# This code will be executed if this module is run 'as-is'.
# Enable HTTP proxy
if 'http_proxy' in os.environ:
proxy_url = urlparse.urlparse(os.environ.get('http_proxy'))
proxy = QNetworkProxy(QNetworkProxy.HttpProxy, proxy_url.hostname, proxy_url.port)
QNetworkProxy.setApplicationProxy(proxy)
# Parse command line arguments.
# Syntax:
# $0 [--xvfb|--display=DISPLAY] [--debug] [--output=FILENAME] <URL>
description = "Creates a screenshot of a website using QtWebkit." \
+ "This program comes with ABSOLUTELY NO WARRANTY. " \
+ "This is free software, and you are welcome to redistribute " \
+ "it under the terms of the GNU General Public License v2."
parser = OptionParser(usage="usage: %prog [options] <URL>",
version="%prog " + VERSION + ", Copyright (c) Roland Tapken",
description=description, add_help_option=True)
parser.add_option("-x", "--xvfb", nargs=2, type="int", dest="xvfb",
help="Start an 'xvfb' instance with the given desktop size.", metavar="WIDTH HEIGHT")
parser.add_option("-g", "--geometry", dest="geometry", nargs=2, default=(0, 0), type="int",
help="Geometry of the virtual browser window (0 means 'autodetect') [default: %default].", metavar="WIDTH HEIGHT")
parser.add_option("-o", "--output", dest="output",
help="Write output to FILE instead of STDOUT.", metavar="FILE")
parser.add_option("-f", "--format", dest="format", default="png",
help="Output image format [default: %default]", metavar="FORMAT")
parser.add_option("--scale", dest="scale", nargs=2, type="int",
help="Scale the image to this size", metavar="WIDTH HEIGHT")
parser.add_option("--aspect-ratio", dest="ratio", type="choice", choices=["ignore", "keep", "expand", "crop"],
help="One of 'ignore', 'keep', 'crop' or 'expand' [default: %default]")
parser.add_option("-F", "--feature", dest="features", action="append", type="choice",
choices=["javascript", "plugins"],
help="Enable additional Webkit features ('javascript', 'plugins')", metavar="FEATURE")
parser.add_option("-w", "--wait", dest="wait", default=0, type="int",
help="Time to wait after loading before the screenshot is taken [default: %default]", metavar="SECONDS")
parser.add_option("-t", "--timeout", dest="timeout", default=0, type="int",
help="Time before the request will be canceled [default: %default]", metavar="SECONDS")
parser.add_option("-W", "--window", dest="window", action="store_true",
help="Grab whole window instead of frame (may be required for plugins)", default=False)
parser.add_option("", "--style", dest="style",
help="Change the Qt look and feel to STYLE (e.G. 'windows').", metavar="STYLE")
parser.add_option("-d", "--display", dest="display",
help="Connect to X server at DISPLAY.", metavar="DISPLAY")
parser.add_option("--debug", action="store_true", dest="debug",
help="Show debugging information.", default=False)
parser.add_option("--log", action="store", dest="logfile", default=LOG_FILENAME,
help="Select the log output file",)
# Parse command line arguments and validate them (as far as we can)
(options,args) = parser.parse_args()
if len(args) != 1:
parser.error("incorrect number of arguments")
if options.display and options.xvfb:
parser.error("options -x and -d are mutually exclusive")
options.url = args[0]
logging.basicConfig(filename=options.logfile,level=logging.WARN,)
# Enable output of debugging information
if options.debug:
logger.setLevel(logging.DEBUG)
if options.xvfb:
# Start 'xvfb' instance by replacing the current process
server_num = int(os.getpid() + 1e6)
newArgs = ["xvfb-run", "--auto-servernum", "--server-num", str(server_num), "--server-args=-screen 0, %dx%dx24" % options.xvfb, sys.argv[0]]
skipArgs = 0
for i in range(1, len(sys.argv)):
if skipArgs > 0:
skipArgs -= 1
elif sys.argv[i] in ["-x", "--xvfb"]:
skipArgs = 2 # following: width and height
else:
newArgs.append(sys.argv[i])
logger.debug("Executing %s" % " ".join(newArgs))
os.execvp(newArgs[0],newArgs[1:])
# Prepare outout ("1" means STDOUT)
if options.output == None:
options.output = sys.stdout
else:
options.output = open(options.output, "w")
logger.debug("Version %s, Python %s, Qt %s", VERSION, sys.version, qVersion());
# Technically, this is a QtGui application, because QWebPage requires it
# to be. But because we will have no user interaction, and rendering can
# not start before 'app.exec_()' is called, we have to trigger our "main"
# by a timer event.
def __main_qt():
# Render the page.
# If this method times out or loading failed, a
# RuntimeException is thrown
try:
# Initialize WebkitRenderer object
renderer = WebkitRenderer()
renderer.width = options.geometry[0]
renderer.height = options.geometry[1]
renderer.timeout = options.timeout
renderer.wait = options.wait
renderer.format = options.format
renderer.grabWholeWindow = options.window
if options.scale:
renderer.scaleRatio = options.ratio
renderer.scaleToWidth = options.scale[0]
renderer.scaleToHeight = options.scale[1]
if options.features:
if "javascript" in options.features:
renderer.qWebSettings[QWebSettings.JavascriptEnabled] = True
if "plugins" in options.features:
renderer.qWebSettings[QWebSettings.PluginsEnabled] = True
renderer.render_to_file(url=options.url, file=options.output)
options.output.close()
QApplication.exit(0)
except RuntimeError, e:
logger.error("main: %s" % e)
print >> sys.stderr, e
QApplication.exit(1)
# Initialize Qt-Application, but make this script
# abortable via CTRL-C
app = init_qtgui(display = options.display, style=options.style)
signal.signal(signal.SIGINT, signal.SIG_DFL)
QTimer.singleShot(0, __main_qt)
sys.exit(app.exec_())