|
a |
|
b/code/google_drive.py |
|
|
1 |
""" |
|
|
2 |
Reference: |
|
|
3 |
https://stackoverflow.com/questions/25010369/wget-curl-large-file-from-google-drive |
|
|
4 |
turdus-merula's answer |
|
|
5 |
|
|
|
6 |
A utility script for helping download large file from google drive |
|
|
7 |
|
|
|
8 |
Usage: python google_drive.py drive_file_id destination_file_path |
|
|
9 |
""" |
|
|
10 |
|
|
|
11 |
from __future__ import print_function |
|
|
12 |
import requests |
|
|
13 |
|
|
|
14 |
def download_file_from_google_drive(id, destination): |
|
|
15 |
def get_confirm_token(response): |
|
|
16 |
for key, value in response.cookies.items(): |
|
|
17 |
if key.startswith('download_warning'): |
|
|
18 |
return value |
|
|
19 |
|
|
|
20 |
return None |
|
|
21 |
|
|
|
22 |
def save_response_content(response, destination): |
|
|
23 |
CHUNK_SIZE = 32768 |
|
|
24 |
|
|
|
25 |
with open(destination, "wb") as f: |
|
|
26 |
for chunk in response.iter_content(CHUNK_SIZE): |
|
|
27 |
if chunk: # filter out keep-alive new chunks |
|
|
28 |
f.write(chunk) |
|
|
29 |
|
|
|
30 |
URL = "https://docs.google.com/uc?export=download" |
|
|
31 |
|
|
|
32 |
session = requests.Session() |
|
|
33 |
|
|
|
34 |
response = session.get(URL, params = { 'id' : id }, stream = True) |
|
|
35 |
token = get_confirm_token(response) |
|
|
36 |
|
|
|
37 |
if token: |
|
|
38 |
params = { 'id' : id, 'confirm' : token } |
|
|
39 |
response = session.get(URL, params = params, stream = True) |
|
|
40 |
|
|
|
41 |
save_response_content(response, destination) |
|
|
42 |
|
|
|
43 |
|
|
|
44 |
if __name__ == "__main__": |
|
|
45 |
import sys |
|
|
46 |
if len(sys.argv) is not 3: |
|
|
47 |
print "Usage: python google_drive.py drive_file_id destination_file_path" |
|
|
48 |
else: |
|
|
49 |
# TAKE ID FROM SHAREABLE LINK |
|
|
50 |
file_id = sys.argv[1] |
|
|
51 |
# DESTINATION FILE ON YOUR DISK |
|
|
52 |
destination = sys.argv[2] |
|
|
53 |
download_file_from_google_drive(file_id, destination) |