Created
August 11, 2017 02:37
-
-
Save eseiver/8a3f7157157bab07456f921afdf28a03 to your computer and use it in GitHub Desktop.
Functions for downloading files from Google Drive
This file contains 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
# With thanks to https://stackoverflow.com/questions/42190691/python-how-to-find-google-drive-filetype & @egh | |
import requests | |
from tqdm import tqdm | |
def download_file_from_google_drive(id, destination): | |
""" General method for downloading from Google Drive. | |
Doesn't require using API or having credentials | |
:param id: Google Drive id for file (constant even if filename changes) | |
:param destination: directory where to download the file | |
""" | |
URL = "https://docs.google.com/uc?export=download" | |
session = requests.Session() | |
response = session.get(URL, params={'id': id}, stream=True) | |
token = get_confirm_token(response) | |
if token: | |
params = {'id': id, 'confirm': token} | |
response = session.get(URL, params=params, stream=True) | |
r = requests.get(URL, params=params, stream=True) | |
save_response_content(response, destination) | |
def get_confirm_token(response): | |
""" Part of keep-alive method for downloading large files from Google Drive | |
Discards packets of data that aren't the actual file | |
:param response: session-based google query | |
:return: either datapacket or discard unneeded data | |
""" | |
for key, value in response.cookies.items(): | |
if key.startswith('download_warning'): | |
return value | |
return None | |
def save_response_content(response, destination, file_size): | |
""" Saves the downloaded file parts from Google Drive to local file | |
Includes progress bar for download % | |
:param response: session-based google query | |
""" | |
CHUNK_SIZE = 32768 | |
# for downloading large file with progress bar | |
if file_size > CHUNK_SIZE: | |
with open(destination, "wb") as file: | |
size = zip_size | |
pieces = size / CHUNK_SIZE | |
with tqdm(total=pieces) as pbar: | |
for chunk in response.iter_content(CHUNK_SIZE): | |
pbar.update(1) | |
if chunk: # filter out keep-alive new chunks | |
file.write(chunk) | |
# for downloading small files in one chunk | |
else: | |
with open(destination, "wb") as file: | |
for chunk in response.iter_content(CHUNK_SIZE): | |
if chunk: # filter out keep-alive new chunks | |
file.write(chunk) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is great. I've been using it for a little while to download GDrive files from Colab notebooks. Somehow, it recently stopped working (seems like
response.cookies.items()
stopped returning any cookies) so the token is not fetched and the script ends up downloading the big-file virus checkup warning HTML page instead of the actual file. Same behavior on Colab and local machine. My suspicion is that Google changed something in their API. Could not find anything relevant.