summaryrefslogtreecommitdiff
path: root/examples/gio/downloader.py
blob: 4da4f53f5f02f78775e885e9e8cdb5a787522af3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Example GIO based Asynchronous downloader

import sys

import glib
import glib.option
import gio


class Downloader(object):
    def __init__(self, uri):
        self.total = 0
        self.gfile = gio.File(uri)
        self.loop = glib.MainLoop()

        output = self.get_output_filename()
        self.fd = open(output, 'w')
        print 'Downloading %s -> %s' % (uri, output)
        self.gfile.read_async(self.read_callback)

    def get_output_filename(self):
        basename = self.gfile.get_basename()
        if basename == '/':
            basename = 'index.html'
        return basename

    def stream_read_callback(self, stream, result):
        data = stream.read_finish(result)
        if not data:
            self.data_finished()
            return
        self.data_read(data)
        stream.read_async(4096, self.stream_read_callback)

    def read_callback(self, gfile, result):
        try:
            stream = gfile.read_finish(result)
        except gio.Error, e:
            print 'ERROR: %s' % (e.message,)
            self.loop.quit()
            return
        stream.read_async(4096, self.stream_read_callback)

    def data_read(self, data):
        self.fd.write(data)
        self.total += len(data)

    def data_finished(self):
        print '%d bytes read' % (self.total,)
        self.loop.quit()

    def run(self):
        self.loop.run()

def main(args):
    if len(args) < 2:
        print 'Needs a URI'
        return 1

    d = Downloader(args[1])
    d.run()

if __name__ == '__main__':
    sys.exit(main(sys.argv))