~luisbg/flickbook/trunk

« back to all changes in this revision

Viewing changes to src/flickroll/download.py

  • Committer: Luis de Bethencourt
  • Date: 2008-03-12 11:24:36 UTC
  • Revision ID: bethencourt@gmail.com-20080312112436-7u6qf1ew9hrcjfsc
if network falls flicbook continues and keeps trying

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
from shutil import move
19
19
import urllib
20
20
import threading
 
21
import socket
21
22
 
22
 
class Download(threading.Thread):
 
23
class Download():
23
24
    def __init__(self, source, filename):
24
 
       self.source = source
25
 
       self.filename = filename
26
 
       threading.Thread.__init__(self)
 
25
        self.source = source
 
26
        self.filename = filename
 
27
        self.connection = True
 
28
        socket.setdefaulttimeout(15)
27
29
 
28
30
    def run(self):
29
 
       tmp_file = urllib.urlretrieve(self.source)
30
 
       move(tmp_file[0], self.filename)
 
31
        try:
 
32
            urllib.urlopen(self.source)
 
33
        except:
 
34
            #print 'network down'
 
35
            self.connection = False
 
36
        if self.connection == True:
 
37
            tmp_file = urllib.urlretrieve(self.source)
 
38
            move(tmp_file[0], self.filename)
 
39
        self.connection = True
31
40
 
32
41
if __name__ == "__main__":
33
42
    source = 'https://launchpadlibrarian.net/12174626/flickbook192.jpg'
34
43
    filename = 'test.jpg'
35
 
    Download(source, filename).start()
 
44
    Download(source, filename).run()