- try:
- url = sys.argv[1]
- except:
- print >>sys.stderr, "Usage: %s http://youtube.com/watch?v=FOOBAR" % sys.argv[0]
- sys.exit(1)
-
- doc = parse_url(url)
- video_url, filename = get_video_url(doc)
- print "Downloading", filename.encode("utf-8")
-
- 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, e:
- 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)
- else:
- break
-
- outfile.close()
+ try:
+ url = sys.argv[1]
+ except:
+ print >>sys.stderr, "Usage: %s http://youtube.com/watch?v=FOOBAR" % sys.argv[0]
+ sys.exit(1)
+
+ doc = parse_url(url)
+ video_url, filename = get_video_url(doc)
+ print "Downloading", filename.encode("utf-8")
+
+ 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, e:
+ 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)
+ else:
+ break
+
+ outfile.close()