Created
January 17, 2023 13:18
-
-
Save emaballarin/30a2b2bf0cb143854bbd6a4c9d4b39d5 to your computer and use it in GitHub Desktop.
Python function(s) and standalone script, and utility Bash and PowerShell scripts to download shared Google Drive files by ID... and no additional dependencies!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env pwsh | |
| python -O gdrivedown.py $args |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import requests | |
| def download_gdrive(gdrive_id, fname_save): | |
| # From: https://github.com/RobustBench/robustbench/blob/1a9c24fa69363d8130f8cdf67ca3ce8a7c481aa8/robustbench/utils.py#L34 | |
| def get_confirm_token(response): | |
| for key, value in response.cookies.items(): | |
| if key.startswith("download_warning"): | |
| return value | |
| return None | |
| def save_response_content(response, fname_save): | |
| CHUNK_SIZE = 32768 | |
| with open(fname_save, "wb") as f: | |
| for chunk in response.iter_content(CHUNK_SIZE): | |
| if chunk: # filter out keep-alive new chunks | |
| f.write(chunk) | |
| print(f"Download started: path={fname_save} (gdrive_id={gdrive_id})") | |
| url_base = "https://docs.google.com/uc?export=download&confirm=t" | |
| session = requests.Session() | |
| response = session.get(url_base, params={"id": gdrive_id}, stream=True) | |
| token = get_confirm_token(response) | |
| if token: | |
| params = {"id": gdrive_id, "confirm": token} | |
| response = session.get(url_base, params=params, stream=True) | |
| save_response_content(response, fname_save) | |
| session.close() | |
| print(f"Download finished: path={fname_save} (gdrive_id={gdrive_id})") | |
| if __name__ == "__main__": | |
| import sys | |
| if len(sys.argv) not in (2, 3): | |
| print("Usage: python gdrivedown.py <gdrive_id> [<fname_save>]") | |
| sys.exit(1) | |
| if len(sys.argv) == 2: | |
| args_fname_save = sys.argv[1] | |
| else: | |
| args_fname_save = sys.argv[2] | |
| args_gdrive_id = sys.argv[1] | |
| download_gdrive(args_gdrive_id, args_fname_save) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| python -O gdrivedown.py $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment