| 1 |
#! /usr/bin/env python |
| 2 |
# vi:ts=4:et |
| 3 |
|
| 4 |
import sys, threading |
| 5 |
import pycurl |
| 6 |
import pygtk |
| 7 |
pygtk.require('2.0') |
| 8 |
import gtk |
| 9 |
|
| 10 |
# We should ignore SIGPIPE when using pycurl.NOSIGNAL - see |
| 11 |
# the libcurl tutorial for more info. |
| 12 |
try: |
| 13 |
import signal |
| 14 |
from signal import SIGPIPE, SIG_IGN |
| 15 |
except ImportError: |
| 16 |
pass |
| 17 |
else: |
| 18 |
signal.signal(SIGPIPE, SIG_IGN) |
| 19 |
|
| 20 |
|
| 21 |
class ProgressBar: |
| 22 |
def __init__(self, uri): |
| 23 |
self.round = 0.0 |
| 24 |
win = gtk.Window(gtk.WINDOW_TOPLEVEL) |
| 25 |
win.set_title("PycURL progress") |
| 26 |
win.show() |
| 27 |
vbox = gtk.VBox(spacing=5) |
| 28 |
vbox.set_border_width(10) |
| 29 |
win.add(vbox) |
| 30 |
win.set_default_size(200, 20) |
| 31 |
vbox.show() |
| 32 |
label = gtk.Label("Downloading %s" % uri) |
| 33 |
label.set_alignment(0, 0.5) |
| 34 |
vbox.pack_start(label) |
| 35 |
label.show() |
| 36 |
pbar = gtk.ProgressBar() |
| 37 |
pbar.show() |
| 38 |
self.pbar = pbar |
| 39 |
vbox.pack_start(pbar) |
| 40 |
win.connect("destroy", self.close_app) |
| 41 |
|
| 42 |
def progress(self, download_t, download_d, upload_t, upload_d): |
| 43 |
if download_t == 0: |
| 44 |
self.round = self.round + 0.1 |
| 45 |
if self.round >= 1.0: self.round = 0.0 |
| 46 |
else: |
| 47 |
self.round = float(download_d) / float(download_t) |
| 48 |
gtk.threads_enter() |
| 49 |
self.pbar.set_fraction(self.round) |
| 50 |
gtk.threads_leave() |
| 51 |
|
| 52 |
def mainloop(self): |
| 53 |
gtk.threads_enter() |
| 54 |
gtk.main() |
| 55 |
gtk.threads_leave() |
| 56 |
|
| 57 |
def close_app(self, *args): |
| 58 |
args[0].destroy() |
| 59 |
gtk.main_quit() |
| 60 |
|
| 61 |
|
| 62 |
class Test(threading.Thread): |
| 63 |
def __init__(self, url, target_file, progress): |
| 64 |
threading.Thread.__init__(self) |
| 65 |
self.target_file = target_file |
| 66 |
self.progress = progress |
| 67 |
self.curl = pycurl.Curl() |
| 68 |
self.curl.setopt(pycurl.URL, url) |
| 69 |
self.curl.setopt(pycurl.WRITEDATA, self.target_file) |
| 70 |
self.curl.setopt(pycurl.FOLLOWLOCATION, 1) |
| 71 |
self.curl.setopt(pycurl.NOPROGRESS, 0) |
| 72 |
self.curl.setopt(pycurl.PROGRESSFUNCTION, self.progress) |
| 73 |
self.curl.setopt(pycurl.MAXREDIRS, 5) |
| 74 |
self.curl.setopt(pycurl.NOSIGNAL, 1) |
| 75 |
|
| 76 |
def run(self): |
| 77 |
self.curl.perform() |
| 78 |
self.curl.close() |
| 79 |
self.target_file.close() |
| 80 |
self.progress(1.0, 1.0, 0, 0) |
| 81 |
|
| 82 |
|
| 83 |
# Check command line args |
| 84 |
if len(sys.argv) < 3: |
| 85 |
print("Usage: %s <URL> <filename>" % sys.argv[0]) |
| 86 |
raise SystemExit |
| 87 |
|
| 88 |
# Make a progress bar window |
| 89 |
p = ProgressBar(sys.argv[1]) |
| 90 |
# Start thread for fetching url |
| 91 |
Test(sys.argv[1], open(sys.argv[2], 'wb'), p.progress).start() |
| 92 |
# Enter the GTK mainloop |
| 93 |
gtk.threads_init() |
| 94 |
try: |
| 95 |
p.mainloop() |
| 96 |
except KeyboardInterrupt: |
| 97 |
pass |