Skip to content
All gists
Back to GitHub
Sign in
Sign up
Sign in
Sign up
{{ message }}
Instantly share code, notes, and snippets.
zjiayao
/
download_from_shared_google_drive.py
Created
Mar 11, 2018
Star
1
Fork
0
Star
Code
Revisions
1
Stars
1
Embed
What would you like to do?
Embed
Embed this gist in your website.
Share
Copy sharable link for this gist.
Clone via HTTPS
Clone with Git or checkout with SVN using the repository’s web address.
Learn more about clone URLs
Download ZIP
Download file from shared Google Drive link.
Raw
download_from_shared_google_drive.py
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
Show hidden characters
from __future__ import print_function
import os
import requests
URL = "https://docs.google.com/uc?export=download"
ID = "" # the file ID appeared in the shared link
DATAFILE = "" # the name of the file to be downloaded
if __name__ == '__main__':
file_dir = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(os.path.join(file_dir, DATAFILE)):
print("dowloading file {} ...".format(DATAFILE))
get_file(URL, ID, os.path.join(file_dir, DATAFILE))
print("done, saved to {}".format(file_dir))
else:
print("file {} already existed".format(DATAFILE))
def get_file(url, id, destination):
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)
save_response_content(response, destination)
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, destination):
CHUNK_SIZE = 32768
if not os.path.exists(os.path.dirname(destination)):
os.makedirs(os.path.dirname(destination))
with open(destination, "wb") as f:
for chunk in response.iter_content(CHUNK_SIZE):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
Sign up for free
to join this conversation on GitHub
. Already have an account?
Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.