+
+ outfile = open(filename, "a")
+ offset = outfile.tell()
+ if offset > 0:
+ print "Resuming download from", pp_size(offset)
+ total_size = None
+
+ while True:
+ try:
+ video_data = urlopen(video_url, offset)
+ except urllib2.HTTPError, e:
+ if e.code == 416:
+ print "File is complete!"
+ break
+ else:
+ raise
+
+ content_length = int(video_data.info().getheader("Content-Length"))
+ if total_size is None:
+ total_size = content_length
+
+ try:
+ copy_with_progress(content_length, video_data, outfile)
+ except IOError:
+ print
+
+ video_data.close()
+ if outfile.tell() != total_size:
+ old_offset = offset
+ offset = outfile.tell()
+ if old_offset == offset:
+ time.sleep(1)
+ print "Restarting download from", pp_size(offset)
+ continue
+