From 8fd0fe2caf7348910fcac21738694104aff0befb Mon Sep 17 00:00:00 2001 From: James Bunton Date: Sat, 29 Sep 2012 10:14:58 +1000 Subject: [PATCH] better command line downloader --- youtube.cgi | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/youtube.cgi b/youtube.cgi index 409b025..283e692 100755 --- a/youtube.cgi +++ b/youtube.cgi @@ -224,6 +224,15 @@ def copy_with_progress(total_size, infile, outfile): size /= 1024 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 bytes_read = 0 @@ -231,13 +240,7 @@ def copy_with_progress(total_size, infile, outfile): 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: @@ -246,6 +249,7 @@ def copy_with_progress(total_size, infile, outfile): bytes_read += len(buf) # Newline at the end + print_status() print def main(): @@ -257,6 +261,9 @@ def main(): 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") -- 2.39.2