#!/usr/bin/env python
+from __future__ import division
+
import cookielib
import cgi
import itertools
}
QUALITIES = {
+ "hd1080": 5,
+ "hd720": 4,
"large": 3,
"medium": 2,
"small": 1,
if size < 1024:
break
size /= 1024
- return "%d %s" % (size, suffix)
+ return "%.2f %s" % (size, suffix)
+
+ def print_status():
+ sys.stdout.write("\33[2K\r")
+ sys.stdout.write("%s / %s (%s/sec)" % (
+ pp_size(bytes_read),
+ pp_size(total_size),
+ pp_size(bytes_read / (now - start_ts)),
+ ))
+ sys.stdout.flush()
start_ts = time.time()
last_ts = 0
now = time.time()
if now - last_ts > 0.5:
last_ts = now
- sys.stdout.write("\33[2K\r")
- sys.stdout.write("%s / %s (%s/sec)" % (
- pp_size(bytes_read),
- pp_size(total_size),
- pp_size(bytes_read / (now - start_ts)),
- ))
- sys.stdout.flush()
+ print_status()
buf = infile.read(32768)
if not buf:
outfile.write(buf)
bytes_read += len(buf)
+ # Newline at the end
+ print_status()
+ print
+
def main():
try:
url = sys.argv[1]
doc = parse_url(url)
video_url, filename = get_video_url(doc)
video_data = urlopen(video_url)
+ if os.path.isfile(filename):
+ print >>sys.stderr, "Error! File exists:", filename
+ sys.exit(1)
outfile = open(filename, "w")
total_size = int(video_data.info().getheader("Content-Length"))
print "Downloading", filename.encode("utf-8")