-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_files_from_drive.py
31 lines (31 loc) · 1.3 KB
/
get_files_from_drive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import argparse
import requests
def download_file_from_google_drive(id, destination):
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)
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
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)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('file_id', help='the ID of the file to download')
parser.add_argument('dest_name', help='the name of the file to download into')
args = parser.parse_args()
file_id = args.file_id
destination = args.dest_name
print("Downloading ID '%s' to file '%s'" % (file_id, destination))
download_file_from_google_drive(file_id, destination)