|
| 1 | +# Download files from Google Drive with terminal |
| 2 | +# Credit: https://stackoverflow.com/questions/25010369/wget-curl-large-file-from-google-drive |
| 3 | +# Usage: python google_drive.py FILE_ID DESTINATION_FILENAME |
| 4 | +# How to get FILE_ID? Click "get sharable link", then you can find it in the end. |
| 5 | +import requests |
| 6 | + |
| 7 | + |
| 8 | +def download_file_from_google_drive(id, destination): |
| 9 | + def get_confirm_token(response): |
| 10 | + for key, value in response.cookies.items(): |
| 11 | + if key.startswith('download_warning'): |
| 12 | + return value |
| 13 | + |
| 14 | + return None |
| 15 | + |
| 16 | + def save_response_content(response, destination): |
| 17 | + CHUNK_SIZE = 32768 |
| 18 | + |
| 19 | + with open(destination, "wb") as f: |
| 20 | + for chunk in response.iter_content(CHUNK_SIZE): |
| 21 | + if chunk: # filter out keep-alive new chunks |
| 22 | + f.write(chunk) |
| 23 | + |
| 24 | + URL = "https://docs.google.com/uc?export=download" |
| 25 | + |
| 26 | + session = requests.Session() |
| 27 | + |
| 28 | + response = session.get(URL, params = { 'id' : id }, stream = True) |
| 29 | + token = get_confirm_token(response) |
| 30 | + |
| 31 | + if token: |
| 32 | + params = { 'id' : id, 'confirm' : token } |
| 33 | + response = session.get(URL, params = params, stream = True) |
| 34 | + |
| 35 | + save_response_content(response, destination) |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + import sys |
| 40 | + if len(sys.argv) is not 3: |
| 41 | + print("Usage: python google_drive.py drive_file_id destination_file_path") |
| 42 | + else: |
| 43 | + # TAKE ID FROM SHAREABLE LINK |
| 44 | + file_id = sys.argv[1] |
| 45 | + # DESTINATION FILE ON YOUR DISK |
| 46 | + destination = sys.argv[2] |
| 47 | + download_file_from_google_drive(file_id, destination) |
0 commit comments