class ScreenshotConsumer(object): """A consumer for screenshot queue messages.""" def __init__(self, screenshot_root=None, username=None, password=None, queue=None, host=None): # Save attributes self.screenshot_root = screenshot_root self.username = username self.password = password self.queue = queue if queue else "screenshots" self.host = host if host else "localhost" # Set up credentials if applicable if self.username and self.password: self.credentials = PlainCredentials(self.username, self.password) else: self.credentials = None # Initialize a QApplication self.application = init_qtgui() # Set up the renderer self.renderer = WebkitRenderer() self.renderer.width = 1024 self.renderer.height = 768 self.renderer.timeout = 10 self.renderer.format = "png" def _capture_screenshot(self, ch, method, properties, body): """Captures a website screenshot.""" parameters = body.split() filename = parameters[0] pattern = re.compile(r"\w+\.\w+") path = os.path.join(self.screenshot_root, filename) if pattern.match(filename) and not os.path.exists(path): image = open(path, "w") self.renderer.render_to_file(url=parameters[1], file=image) image.close() def run(self): # Open RabbitMQ connection connection = AsyncoreConnection(ConnectionParameters(host=self.host, credentials=self.credentials)) channel = connection.channel() # Declare the queue channel.queue_declare(queue=self.queue, durable=True) # Subscribe to the queue channel.basic_consume(self._capture_screenshot, queue=self.queue, no_ack=True) # Wait for data asyncore_loop()
def renderer_func(): renderer = WebkitRenderer() renderer.width = 800 renderer.height = 600 renderer.timeout = 10 renderer.wait = 1 renderer.format = "png" renderer.grabWholeWindow = False outfile = open("stackoverflow.png", "w") renderer.render_to_file(url="http://stackoverflow.com", file=outfile) outfile.close()
def _get_renderer(self, width, height): try: return self.renderers[(width, height)] except: if self.verbose: print "about to make WebkitRenderer" renderer = WebkitRenderer(scaleRatio='crop', scaleTransform='smooth', scaleToWidth=width, scaleToHeight=height, width=1440, height=900, wait=5, timeout=60, ignoreAlerts=True, ignoreConfirms=True, ignoreConsoleMessages=True) if self.verbose: print "done making WebkitRenderer" renderer.qWebSettings[QWebSettings.JavascriptEnabled] = True self.renderers[(width, height)] = renderer return renderer
def __init__(self, screenshot_root=None, username=None, password=None, queue=None, host=None): # Save attributes self.screenshot_root = screenshot_root self.username = username self.password = password self.queue = queue if queue else "screenshots" self.host = host if host else "localhost" # Set up credentials if applicable if self.username and self.password: self.credentials = PlainCredentials(self.username, self.password) else: self.credentials = None # Initialize a QApplication self.application = init_qtgui() # Set up the renderer self.renderer = WebkitRenderer() self.renderer.width = 1024 self.renderer.height = 768 self.renderer.timeout = 10 self.renderer.format = "png"
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.logger = logger 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 renderer.renderTransparentBackground = options.transparent renderer.encodedUrl = options.encoded_url if options.cookies: renderer.cookies = options.cookies 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_object=options.output) options.output.close() QApplication.exit(0) except RuntimeError, e: logger.error("main: %s" % e) print >> sys.stderr, e QApplication.exit(1)
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.logger = logger 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 renderer.renderTransparentBackground = options.transparent renderer.encodedUrl = options.encoded_url if options.cookies: renderer.cookies = options.cookies 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(res=options.url, file_object=options.output) options.output.close() QApplication.exit(0) except RuntimeError, e: logger.error("main: %s" % e) print >> sys.stderr, e QApplication.exit(1)
def __init__(self): #self.app_gui = self.init_qtgui() self.renderer = WebkitRenderer() self.renderer.format = 'jpg' self.renderer.width = 1024 self.renderer.height = 900
class Html2Png: def __init__(self): #self.app_gui = self.init_qtgui() self.renderer = WebkitRenderer() self.renderer.format = 'jpg' self.renderer.width = 1024 self.renderer.height = 900 #pass def init_qtgui(display=None, style=None, qtargs=None): """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 or []) return QApplication(qtargs2) def init_qtgui_deprecated(display=None, style=None, qtargs=None): """Initiates the QApplication environment using the given args.""" if QApplication.instance(): 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 or []) return QApplication(qtargs2) def goHtml2Png(self, content, fname): f = open("/tmp/%s.html" % (fname), "w") f.write(content) f.close() #self.renderer = WebkitRenderer() #self.renderer.format = 'jpg' #self.renderer.width = 1024 #self.renderer.height = 768 original_filepath = "/tmp/%s.jpg" % (fname) original_file = open(original_filepath, "w") self.renderer.render_to_file("file:///tmp/%s.html" % (fname), original_file) #self.renderer.render_to_file("file:///tmp/62811987905_order_22335269.html", original_file) original_file.close()
def generate_thumbnail(url, width, height): renderer = WebkitRenderer() renderer.width = 1280 renderer.height = 768 renderer.timeout = 60 renderer.wait = 3 renderer.format = "png" renderer.scaleRatio = "crop" renderer.scaleToWidth = width renderer.scaleToHeight = height renderer.grabWholeWindow = False #outfile = open("stackoverflow.png", "w") #renderer.render_to_file(url=url, file=outfile) #outfile.close() return renderer.render_to_bytes(url)
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.logger = logger 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 renderer.renderTransparentBackground = options.transparent renderer.quality = options.quality 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 try: res = 0 renderer.render_to_file(url=options.url, file_object=options.output) except BaseWebkitException, e: res = e.status_code finally: options.output.close() QApplication.exit(res)
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.logger = logger 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 renderer.renderTransparentBackground = options.transparent renderer.encodedUrl = options.encoded_url if options.cookies: renderer.cookies = options.cookies 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 for f in options.urls: read = 'file://' + os.path.join(options.input_dir, f) if options.input_dir else f save = os.path.join(options.output_dir, f) if options.output_dir else f save = os.path.splitext(save)[0] if not options.keep_ext else save save = save + '.' + options.format if not os.path.isdir(os.path.dirname(save)): os.makedirs(os.path.dirname(save)) with open(save, "w") as save_file: renderer.render_to_file(res=read, file_object=save_file) QApplication.exit(0) except RuntimeError, e: logger.error("main: %s" % e) print >> sys.stderr, e QApplication.exit(1)
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.logger = logger 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 renderer.renderTransparentBackground = options.transparent renderer.quality = options.quality 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 try: res = 0 renderer.render_to_file( url=options.url, file_object=options.output ) except BaseWebkitException, e: res = e.status_code finally: options.output.close() QApplication.exit(res)
#!/usr/bin/env python import sys import signal from webkit2png import WebkitRenderer from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import QWebPage app = QApplication(sys.argv) signal.signal(signal.SIGINT, signal.SIG_DFL) wkr = WebkitRenderer() hosts = ['http://www.google.com', 'http://wikipedia.com', 'http://github.com'] for host in hosts: try: f = open(("%s.png" % host).replace('/', '_'), 'w') wkr.render_to_file(host, f) f.close() print "finished %s" % host except Exception, err: print "Error on %s: %s" % (host, str(err))