Ceron

RS Python Downloader

May 22nd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.49 KB | None | 0 0
  1. from urllib.request import *
  2. import math
  3. import sys
  4. import time
  5. import os,traceback
  6. dl_starttime = time.clock()
  7. timeout = 60
  8. def getDLink(link = '') :
  9.     link = link.split("|")
  10.     #u = urlopen(url)
  11.     #s = u.readall()
  12.     url = "http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=" + link[2] + "&filename=" + link[3]
  13.     #print(url)
  14.     try:
  15.         u = urlopen(url,None,timeout)
  16.         s = u.readall()
  17.         print("Rapidshare-Responsed:\n\n",s,"\n\n")
  18.         if 'ERROR' in str(s):
  19.             raise NameError("Rapidshare responsed an Error :(")
  20.         s = str(s).split(":")
  21.         s = s[1]
  22.         s = s.split(",")
  23.         url = "http://" + s[0] + "/cgi-bin/rsapi.cgi?sub=download&fileid=" + link[2] + "&filename=" + link[3] + "&dlauth=" + s[1]
  24.         retval = [url,link[3]]
  25.         return retval
  26.     except URLError:
  27.         print("URLError exception caught:\n\n",sys.exc_info(),"\n\n")
  28.         pass
  29.        
  30. def download(url='',filename=''):
  31.     u = urlopen(url,None,timeout)
  32.     meta = u.info()
  33.     file_size = int(meta.get('Content-Length'))
  34.     if os.path.exists(filename):
  35.         localfile_size = os.path.getsize(filename)
  36.     else:
  37.         localfile_size = 0
  38.     print("file_size:",file_size," localfile_size:",localfile_size)
  39.     if localfile_size < file_size:
  40.         f = open(filename,'wb')
  41.         print("Downloading: " + filename + " Bytes:" + str(file_size))
  42.         file_size_dl = 0
  43.         block_sz = 32768
  44.         while True:
  45.             buffer = u.read(block_sz)
  46.             if not buffer:
  47.                 break
  48.            
  49.             file_size_dl += len(buffer)
  50.             f.write(buffer)
  51.             p = file_size_dl * 100.0 /file_size
  52.             d = file_size_dl/1024/1024
  53.             progressbar(p,d)
  54.             #status = r"%2.2f MB [%3.3f%%]" % (file_size_dl/1024/1024, file_size_dl * 100. / file_size)
  55.             #status = status + chr(8)*(len(status)+1)
  56.             #print(status)
  57.         f.close()
  58.     else:
  59.         print("Skipped file: ",filename)
  60.  
  61.    
  62. def progressbar(percent,downloaded):
  63.     width = 60
  64.     mark = '#'
  65.     marks = math.floor(width * (percent /100.0))
  66.     spaces = math.floor(width - marks)
  67.     loader = '[' + (mark * int(marks)) + (' ' * int(spaces)) + ']'
  68.     eltime = getelapsedtime(dl_starttime)
  69.     sys.stdout.write("%s %2.2f%% (%2.2f MB) %2d h %2d min %2d sec\r" % (loader, percent,downloaded,eltime[0],eltime[1],eltime[2]))
  70.     if percent >= 100:
  71.  
  72.         sys.stdout.write("\n")
  73.  
  74.     sys.stdout.flush()
  75.    
  76.    
  77. def getelapsedtime(starttime):
  78.     now = time.clock()
  79.     delta = int(now-starttime)
  80.     hr = math.floor(delta / 3600)
  81.     min = math.floor((delta % 3600) / 60)
  82.     sec = ((delta % 3600) % 60)
  83.     eltime = [hr,min,sec]
  84.     return eltime
  85.  
  86. def validatefilesizes():
  87.     filenames = []
  88.     fin = True
  89.     for file in links:
  90.         filename = file.split("|")[3]
  91.         filenames.append([filename,file])
  92.     for file in filenames:
  93.         print(file)
  94.         dlink = getDLink(file[1])[0]
  95.         u = urlopen(dlink,None,timeout)
  96.         meta = u.info()
  97.         localsize = os.path.getsize(file[0])
  98.         if localsize < int(meta.get('Content-Length')):
  99.             #dlink = getDLink(file[1])
  100.             #download(dlink,file[0])
  101.             fin = False
  102.     return fin
  103. #ein Array aus Links =)    
  104. links = []
  105.  
  106.  
  107. def iterate():
  108.     for file in links:
  109.         try:
  110.             dl_starttime = time.clock()
  111.             arguments = getDLink(file)
  112.             download(arguments[0],arguments[1])
  113.         except:
  114.             print("Ein Fehler trat auf:\n\n",file," ",sys.exc_info(),"\n\n")
  115.             traceback.print_exc(file=sys.stdout)
  116.             print("\n\n")
  117.  
  118.  
  119. def main():
  120.  
  121.     dl_starttime = time.clock()
  122.     try:
  123.         iterate()
  124.         while not validatefilesizes():
  125.             try:
  126.                 iterate()
  127.                 validatefilesizes()
  128.             except:
  129.                 print("Ein Fehler trat auf:\n\n",sys.exc_info(),"\n\n")
  130.     except:
  131.         print("Ein Fehler trat auf:\n\n",sys.exc_info(),"\n\n")
  132.  
  133. if __name__ == "__main__":
  134.     main()
Add Comment
Please, Sign In to add comment