Advertisement
dvx3

main.py

Mar 7th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. from os.path import expanduser
  2. from random import randint
  3. import requests
  4. import sys
  5. import time
  6. import winsound
  7.  
  8. def downloadFile(url, directory) :
  9.   local_filename = url.split('/')[-1]
  10.   with open(directory + '\\' + local_filename, 'wb') as f:
  11.     start = time.perf_counter()
  12.     r = requests.get(url, stream=True)
  13.     total_length = int(r.headers.get('content-length'))
  14.     dl = 0
  15.     if total_length is None:
  16.       f.write(r.content)
  17.     else:
  18.       for chunk in r.iter_content(8192): # increase chunck size to download faster, and have a higher frequency
  19.         dl += len(chunk)
  20.         f.write(chunk)
  21.         done = int(50 * dl / total_length)
  22.         dl_time = int(dl//(time.perf_counter() - start))
  23.         sys.stdout.write("\r[%s%s] %s B/s" % ('=' * done, ' ' * (50-done), dl_time))
  24.         if int(dl_time/randint(38, 1000)) > 37:
  25.         # here you play and you can manipulate frequencies, (10 kb/s in default), decrease beep ms (2nd argument) to increase download speed
  26.             winsound.Beep(int(dl_time/randint(38, 1000)), 100)
  27.         print('')
  28.   return (time.perf_counter() - start)
  29.  
  30. if __name__ == "__main__" :
  31.     directory = (expanduser("~") + "\\Downloads\\")
  32.     url = input("Type or paste download url, and press enter: ")
  33.     time_elapsed = downloadFile(url, directory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement