googleDrive.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import requests
  2. def download_file_from_google_drive(id, destination):
  3. URL = "https://docs.google.com/uc?export=download"
  4. session = requests.Session()
  5. response = session.get(URL, params = { 'id' : id }, stream = True)
  6. token = get_confirm_token(response)
  7. if token:
  8. params = { 'id' : id, 'confirm' : token }
  9. response = session.get(URL, params = params, stream = True)
  10. save_response_content(response, destination)
  11. def get_confirm_token(response):
  12. for key, value in response.cookies.items():
  13. if key.startswith('download_warning'):
  14. return value
  15. return None
  16. def save_response_content(response, destination):
  17. CHUNK_SIZE = 32768
  18. with open(destination, "wb") as f:
  19. for chunk in response.iter_content(CHUNK_SIZE):
  20. if chunk: # filter out keep-alive new chunks
  21. f.write(chunk)
  22. if __name__ == "__main__":
  23. file_id = '1DiKPNAjU-gfPx9_L2uA5ldycwwRE8_Wo'
  24. destination = 'hi'
  25. download_file_from_google_drive(file_id, destination)