Advertisement
Guest User

Untitled

a guest
Aug 28th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. from tqdm import tqdm
  2. import requests
  3. import math
  4. import re
  5.  
  6. #hardcoded credentials
  7. user = 'username'
  8. password = 'password'
  9.  
  10. #build a list of file IDs from a separately-constructed pandas dataframe
  11. filelist = [0,1,2,3]
  12. uuids = []
  13. for i in filelist:
  14.     uuids.append(filedf.iloc[i]['uuid'])
  15.  
  16. #Serially retrieve and store the file corresponding to each id
  17. for u in uuids:
  18.     url = "url/%s" % u
  19.     # Streaming, so we can iterate over the response.
  20.     r = requests.get(url, stream=True,auth=(user,password))
  21.     # Total size in bytes.
  22.     total_size = int(r.headers.get('content-length', 0));
  23.     block_size = 1024
  24.     wrote = 0
  25.    
  26.     #retrieve the filename from from HTTP headers:
  27.     d = r.headers['content-disposition']
  28.     fname = re.findall("filename=\"(.+)\"", d)[0]
  29.     with open(fname, 'wb') as f:
  30.         for data in tqdm_notebook(r.iter_content(block_size), total=math.ceil(total_size//block_size) , unit='KB', unit_scale=True):
  31.             wrote = wrote  + len(data)
  32.             f.write(data)
  33.     if total_size != 0 and wrote != total_size:
  34.         print("ERROR, something went wrong")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement