Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #taken from this StackOverflow answer: https://stackoverflow.com/a/39225039
  2. import requests
  3.  
  4. def download_file_from_google_drive(id, destination):
  5. URL = "https://docs.google.com/uc?export=download"
  6.  
  7. session = requests.Session()
  8.  
  9. response = session.get(URL, params = { 'id' : id }, stream = True)
  10. token = get_confirm_token(response)
  11.  
  12. if token:
  13. params = { 'id' : id, 'confirm' : token }
  14. response = session.get(URL, params = params, stream = True)
  15.  
  16. save_response_content(response, destination)
  17.  
  18. def get_confirm_token(response):
  19. for key, value in response.cookies.items():
  20. if key.startswith('download_warning'):
  21. return value
  22.  
  23. return None
  24.  
  25. def save_response_content(response, destination):
  26. CHUNK_SIZE = 32768
  27.  
  28. with open(destination, "wb") as f:
  29. for chunk in response.iter_content(CHUNK_SIZE):
  30. if chunk: # filter out keep-alive new chunks
  31. f.write(chunk)
  32.  
  33. file_id = '1hF8vS6YeHkx3j2pfCeQqqZGwA_PJq_Da'
  34. destination = '/home/moamen/FlowNet2_checkpoint.pth.tar'
  35. download_file_from_google_drive(file_id, destination)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement