carla/Util/download_from_gdrive.py

87 lines
2.4 KiB
Python
Raw Normal View History

2017-10-25 23:26:31 +08:00
#!/usr/bin/env python3
2017-11-02 21:17:52 +08:00
# Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma de
2018-01-22 23:42:10 +08:00
# Barcelona (UAB).
2017-11-02 21:17:52 +08:00
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
2017-10-25 23:26:31 +08:00
"""Download big files from Google Drive."""
import argparse
import shutil
import sys
import requests
2019-03-19 19:20:59 +08:00
2017-10-25 23:26:31 +08:00
def sizeof_fmt(num, suffix='B'):
# https://stackoverflow.com/a/1094933/5308925
2019-03-19 19:20:59 +08:00
for unit in ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z']:
2017-10-25 23:26:31 +08:00
if abs(num) < 1000.0:
return "%3.2f%s%s" % (num, unit, suffix)
num /= 1000.0
return "%.2f%s%s" % (num, 'Yi', suffix)
def print_status(destination, progress):
message = "Downloading %s... %s" % (destination, sizeof_fmt(progress))
empty_space = shutil.get_terminal_size((80, 20)).columns - len(message)
sys.stdout.write('\r' + message + empty_space * ' ')
sys.stdout.flush()
def download_file_from_google_drive(id, destination):
# https://stackoverflow.com/a/39225039/5308925
def save_response_content(response, destination):
chunk_size = 32768
written_size = 0
with open(destination, "wb") as f:
for chunk in response.iter_content(chunk_size):
2019-03-19 19:20:59 +08:00
if chunk: # filter out keep-alive new chunks
2017-10-25 23:26:31 +08:00
f.write(chunk)
written_size += chunk_size
print_status(destination, written_size)
print('Done.')
def get_confirm_token(response):
for key, value in response.cookies.items():
if key.startswith('download_warning'):
return value
return None
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)
if __name__ == "__main__":
try:
argparser = argparse.ArgumentParser(description=__doc__)
argparser.add_argument(
'id',
help='Google Drive\'s file id')
argparser.add_argument(
'destination',
help='destination file path')
args = argparser.parse_args()
download_file_from_google_drive(args.id, args.destination)
except KeyboardInterrupt:
print('\nCancelled by user. Bye!')