]> code.delx.au - youtube-cgi/blobdiff - youtube.cgi
better command line downloader
[youtube-cgi] / youtube.cgi
index 293db8ab74d013e7306048bdd48c6eb56aca50af..283e69233b5efc4860482bdaa9f39a4720a6fed4 100755 (executable)
@@ -1,5 +1,7 @@
 #!/usr/bin/env python
 
+from __future__ import division
+
 import cookielib
 import cgi
 import itertools
@@ -11,6 +13,7 @@ import resource
 import shutil
 import subprocess
 import sys
+import time
 import urllib
 import urllib2
 import urlparse
@@ -26,6 +29,8 @@ MIMETYPES = {
 }
 
 QUALITIES = {
+       "hd1080": 5,
+       "hd720": 4,
        "large": 3,
        "medium": 2,
        "small": 1,
@@ -210,6 +215,43 @@ def cgimain():
                )
                return
 
+def copy_with_progress(total_size, infile, outfile):
+       def pp_size(size):
+               suffixes = ["", "KiB", "MiB", "GiB"]
+               for i, suffix in enumerate(suffixes):
+                       if size < 1024:
+                               break
+                       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
+       while True:
+               now = time.time()
+               if now - last_ts > 0.5:
+                       last_ts = now
+                       print_status()
+
+               buf = infile.read(32768)
+               if not buf:
+                       break
+               outfile.write(buf)
+               bytes_read += len(buf)
+
+       # Newline at the end
+       print_status()
+       print
+
 def main():
        try:
                url = sys.argv[1]
@@ -218,10 +260,15 @@ def main():
                sys.exit(1)
        doc = parse_url(url)
        video_url, filename = get_video_url(doc)
-       data = urlopen(video_url)
+       video_data = urlopen(video_url)
+       if os.path.isfile(filename):
+               print >>sys.stderr, "Error! File exists:", filename
+               sys.exit(1)
        outfile = open(filename, "w")
-       shutil.copyfileobj(data, outfile)
-       data.close()
+       total_size = int(video_data.info().getheader("Content-Length"))
+       print "Downloading", filename.encode("utf-8")
+       copy_with_progress(total_size, video_data, outfile)
+       video_data.close()
        outfile.close()
 
 
@@ -230,5 +277,9 @@ if __name__ == "__main__":
        if os.environ.has_key("SCRIPT_NAME"):
                cgimain()
        else:
-               main()
+               try:
+                       main()
+               except KeyboardInterrupt:
+                       print "\nExiting..."
+                       sys.exit(1)