stream13

fs.ua downloader

Aug 9th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. import sys
  5. import urllib
  6. import urllib2
  7. import locale
  8. reload(sys)
  9.  
  10. def help():
  11.     print "\nfsget fsua_[number].txt\n"
  12.     print "Only for fs.ua file lists\n"
  13.     return None
  14.  
  15. # URL-encoded to UTF-8
  16. def fs_decode(localline):
  17.     return urllib.unquote(localline).decode('utf8')
  18.  
  19. # Logics itself
  20. if (len(sys.argv) != 2):
  21.     help()
  22. else:
  23.     filename = sys.argv[1]
  24.     if(os.path.exists(filename) and os.path.isfile(filename)):
  25.         # print "valid file"
  26.         myfile = open(filename, 'r')
  27.         line = myfile.readline()
  28.         block_sz = 8192
  29.         while (line != None) and (line != ""):
  30.             decoded_line = fs_decode(line)
  31.  
  32.             file_name = decoded_line.split('/')[-1]
  33.             file_name = file_name.replace(file_name[-1], "")
  34.             file_name = file_name.replace(file_name[-1], "")
  35.             if(os.path.isfile(file_name)):
  36.                 file_size_dl = int(os.path.getsize(file_name))
  37.             else:
  38.                 file_size_dl = 0
  39.             u = urllib2.urlopen(line)                      
  40.             meta = u.info()
  41.             file_size = int(meta.getheaders("Content-Length")[0])
  42.             print "Downloading: %s\nBytes: %s" % (file_name, file_size)
  43.             # print "Remote file size = "+str(file_size)+";"
  44.             # print "Local file size = "+str(file_size_dl)+";"
  45.             if(file_size != file_size_dl):
  46.                 f = open(file_name, 'wb')
  47.                 while True:
  48.                     buffer = u.read(block_sz)
  49.                     if not buffer:
  50.                         break
  51.  
  52.                     file_size_dl += len(buffer)
  53.                     f.write(buffer)
  54.                     status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
  55.                     status = status + chr(8)*(len(status)+1)
  56.                     print status,
  57.                 f.close()
  58.             else:
  59.                 print "File '"+file_name+"' seems to downloaded already!"
  60.             line = myfile.readline()
  61.        
  62.     else:
  63.         print "invalid file"
Add Comment
Please, Sign In to add comment