ganadist

Untitled

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