ganadist

Untitled

Jan 26th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import sys
  2. import cairo, gi
  3. gi.require_version('Gtk', '2.0')
  4. gi.require_version('Gdk', '2.0')
  5. gi.require_version('WebKit', '1.0')
  6. from gi.repository import Gtk, Gdk, WebKit
  7.  
  8. if len(sys.argv) != 3:
  9.     print 'usage: %s url filename'%sys.argv[0]
  10.     sys.exit(-1)
  11.  
  12. uri, filename = sys.argv[1:]
  13.  
  14. for scheme in ('http://', 'https://', 'ftp://', 'file://'):
  15.     if uri.startswith(scheme):
  16.         break
  17. else:
  18.     uri = 'http://' + uri
  19.  
  20. w = Gtk.OffscreenWindow()
  21. w.set_default_size(800, 640)
  22. v = WebKit.WebView()
  23. w.add(v)
  24.  
  25. def capture_window(window):
  26.     w, h = window.get_size()
  27.     dst = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
  28.     ctx = cairo.Context(dst)
  29.     surface = Gdk.cairo_create(window.get_window()).get_target()
  30.     ctx.set_source_surface(surface)
  31.     ctx.paint()
  32.     dst.write_to_png(filename)
  33.     Gtk.main_quit()
  34.  
  35. def progress_cb(webview, progress, window):
  36.     if webview.get_progress() == 1.0:
  37.         capture_window(window)
  38.  
  39. v.connect('notify::progress', progress_cb, w)
  40. v.load_uri(uri)
  41. w.show_all()
  42.  
  43. Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment