calfred2808

Python: Download File using Google Link

Aug 23rd, 2022
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import requests
  2.  
  3. def download_file_from_google_drive(id, destination):
  4.     URL = "https://docs.google.com/uc?export=download"
  5.  
  6.     session = requests.Session()
  7.  
  8.     response = session.get(URL, params = { 'id' : id }, stream = True)
  9.     token = get_confirm_token(response)
  10.  
  11.     if token:
  12.         params = { 'id' : id, 'confirm' : token }
  13.         response = session.get(URL, params = params, stream = True)
  14.  
  15.     save_response_content(response, destination)    
  16.  
  17. def get_confirm_token(response):
  18.     for key, value in response.cookies.items():
  19.         if key.startswith('download_warning'):
  20.             return value
  21.  
  22.     return None
  23.  
  24. def save_response_content(response, destination):
  25.     CHUNK_SIZE = 32768
  26.  
  27.     with open(destination, "wb") as f:
  28.         for chunk in response.iter_content(CHUNK_SIZE):
  29.             if chunk: # filter out keep-alive new chunks
  30.                 f.write(chunk)
  31.  
  32. if __name__ == "__main__":
  33.     file_id = 'TAKE ID FROM SHAREABLE LINK'
  34.     destination = 'DESTINATION FILE ON YOUR DISK'
  35.     download_file_from_google_drive(file_id, destination)
Tags: python
Add Comment
Please, Sign In to add comment