Last active
February 9, 2021 02:44
-
-
Save AfroThundr3007730/d0441ecfcd1567a3a8945d37f8cf8175 to your computer and use it in GitHub Desktop.
Generates a bash script to recursively download a Google Drive folder
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
#!/usr/bin/python3 | |
# Generates a bash script to recursively download a Google Drive folder | |
# Original: https://gist.github.com/immuntasir/73b8e8eef7e6c9066aaf2432bebf7db0 | |
import sys | |
from pydrive.auth import GoogleAuth | |
from pydrive.drive import GoogleDrive | |
# A browser window will open. login using the appropriate account. | |
def authenticate_gdrive(): | |
gauth = GoogleAuth() | |
gauth.DEFAULT_SETTINGS.update( | |
save_credentials=True, | |
save_credentials_backend='file', | |
save_credentials_file='credentials.json', | |
get_refresh_token=True) | |
gauth.LoadCredentials() | |
if gauth.credentials is None: | |
gauth.LocalWebserverAuth() | |
elif gauth.access_token_expired: | |
gauth.Refresh() | |
else: | |
gauth.Authorize() | |
return GoogleDrive(gauth) | |
# Recursively build the list of files and directories | |
def build_filelist(drive): | |
file_dict = {} | |
item_queue = [(parent_id, parent_dir)] | |
while len(item_queue) > 0: | |
current_id, current_dir = item_queue.pop(0) | |
print(current_id, '==>', current_dir) | |
for item in drive.ListFile( | |
{'q': "'{}' in parents and trashed=false".format(current_id)} | |
).GetList(): | |
if item['mimeType'] == 'application/vnd.google-apps.folder': | |
file_dict[item['id']] = { | |
'title': item['title'], 'type': 'folder', | |
'path': current_dir + item['title'] + '/'} | |
item_queue.append((item['id'], file_dict[item['id']]['path'])) | |
else: | |
file_dict[item['id']] = { | |
'title': item['title'], 'type': 'file', | |
'path': current_dir + item['title']} | |
return file_dict | |
# Write the bash script | |
def build_downloadscript(file_dict): | |
with open('download_script.sh', 'w') as f: | |
f.write('#!/bin/bash\n') | |
for id in file_dict.keys(): | |
if file_dict[id]['type'] == 'folder': | |
f.write('mkdir -p ' + file_dict[id]['path'] + '\n') | |
else: | |
f.write(wget_text.replace('FILE_ID', id).replace( | |
'FILE_NAME', file_dict[id]['path']) + '\n') | |
print('Download script saved to:', f.name) | |
if __name__ == '__main__': | |
parent_id = sys.argv[1] if len(sys.argv) >= 2 else input( | |
'Enter Google Drive folder ID to download: ') | |
parent_dir = sys.argv[2] if len(sys.argv) >= 3 else input( | |
'Enter destination directory for download: ') | |
parent_dir += '/' if not parent_dir.endswith('/') else '' | |
wget_text = ( | |
'conf="$(wget --quiet --save-cookies /tmp/cookies.txt ' | |
'--keep-session-cookies --no-check-certificate ' | |
'"https://docs.google.com/uc?export=download&id=FILE_ID" -O- | ' | |
'sed -rn \'s/.*confirm=([0-9A-Za-z_]+).*/\\1\\n/p\')" &&\n' | |
' wget --load-cookies /tmp/cookies.txt ' | |
'"https://docs.google.com/uc?export=download&confirm=$conf&id=FILE_ID"' | |
' -O "FILE_NAME" && rm -f /tmp/cookies.txt') | |
build_downloadscript(build_filelist(authenticate_gdrive())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that you'll need an OAuth API Token as shown here: https://pythonhosted.org/PyDrive/quickstart.html