#!/usr/bin/env python # this file is released into the public domain # Author Michael Buckley support (at) codefisher.org # Based on the code found at # https://help.ubuntu.com/community/PythonRecipes/WebBrowser # http://burtonini.com/blog/computers/mozilla-thumbnail-20040614.xhtml # an X window system neededs to be running for this script to work # if you need help setting one up on your server contact the # author of this script import os import sys import pygtk import gtk import gtkmozembed import gobject class PyWebShot: def __init__(self,url,filename,width=1024,height=800,thumbwidth=256, thumbheight=200,delay=3,timeout=15,profile=None): if profile == None: profile = os.environ["HOME"] print "profile created/being used at",profile+"/.webshot/" gtkmozembed.set_profile_path(profile+"/.webshot/", "webshot") self.width = int(width) self.height = int(height) self.thumbheight = int(thumbheight) self.thumbwidth = int(thumbwidth) self.file = filename self.delay = delay self.window = gtk.Window() self.window.connect("delete_event", self._delete_event) self.window.connect("destroy",self._closedown) gobject.timeout_add(1000*timeout,self.screenshot) #change to self._closedown if you prefer self.mozbrowser = gtkmozembed.MozEmbed() self.window.add(self.mozbrowser) self.mozbrowser.load_url(url) self.mozbrowser.set_size_request(self.width,self.height) self.mozbrowser.connect("progress", self.on_progress) self.mozbrowser.connect("net_stop", self.on_net_stop) self.mozbrowser.show() print "Window created, waiting for page",url,"to load" self.window.show() def on_net_stop(self,caller_widget): print "Loading finished prepearing to take screen shot in",self.delay,'seconds' gobject.timeout_add(1000*self.delay,self.screenshot) def screenshot(self): window = self.mozbrowser.window pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,self.width,self.height) pixbuf.get_from_drawable(window,self.mozbrowser.get_colormap(),0,0,0,0,self.width,self.height) pixbuf = pixbuf.scale_simple(self.thumbwidth, self.thumbheight, gtk.gdk.INTERP_HYPER) pixbuf.save(self.file,"png") print "Image",self.file,'saved' self._closedown() def on_progress(self,caller_widget,progress,total): print 'loaded:',round(float(progress)/total*100),'%' def _closedown(self,data=None): print "Closeing down" self.window.destroy() gtk.main_quit() def _delete_event(self, widget, event, data=None): return False def main(self): gtk.main() if __name__ == "__main__": args = sys.argv[1:] print "Arguments:",args win = PyWebShot(*args) win.main()