Ticket #394: download_file_with_webkit.py

File download_file_with_webkit.py, 1.6 KB (added by humitos, 10 years ago)

Example

Line 
1from gi.repository import Gtk
2from gi.repository import WebKit
3
4
5def __destroy_cb(widget, data=None):
6    Gtk.main_quit()
7
8
9def __progress_change_cb(download, something):
10    progress = download.get_progress()
11    print progress
12
13
14def __state_change_cb(download, gparamspec):
15    state = download.get_status()
16    if state == WebKit.DownloadStatus.STARTED:
17        print 'STARTED'
18    elif state == WebKit.DownloadStatus.FINISHED:
19        print 'FINISHED'
20    elif state == WebKit.DownloadStatus.CANCELLED:
21        print 'CANCELLED'
22    else:
23        print 'Other state: %s' % state
24
25
26def __error_cb(download, err_code, err_detail, reason):
27    print 'Error downloading URI code %s, detail %s: %s' % (err_code, err_detail, reason)
28
29
30# We need to keep the download in memory
31download = None
32def __clicked_button(widget):
33    print 'Button clicked'
34
35    url = 'http://mkaufmann.com.ar/~humitos/olpc/audio/02%20-%20A%20veces%20no.mp3'
36    # url = 'http://mkaufmann.com.ar/~humitos/olpc/audio/Example.ogg'
37    nr = WebKit.NetworkRequest()
38    nr.set_uri(url)
39    global download
40    download = WebKit.Download(network_request=nr)
41
42    download.connect('notify::progress', __progress_change_cb)
43    download.connect('notify::status', __state_change_cb)
44    download.connect('error', __error_cb)
45
46    print download.get_uri()
47    download.set_destination_uri('file:///tmp/new_file.mp3')
48    download.start()
49
50
51window = Gtk.Window()
52window.set_title('Download a file with WebKit example')
53window.set_default_size(300, 180)
54window.connect('destroy', __destroy_cb)
55
56button = Gtk.Button('Download file!')
57button.connect('clicked', __clicked_button)
58
59window.add(button)
60window.show_all()
61Gtk.main()
62