├── README.md ├── create_download_list.py └── download_files.py /README.md: -------------------------------------------------------------------------------- 1 | # google-drive-downloader 2 | Batch download files from Google Drive 3 | 4 | ## How To Use 5 | 1) Setup Python on your system 6 | 2) Create a new [Google Drive project](https://developers.google.com/drive/activity/v1/guides/project) 7 | 3) Create an OAuth 2 Client ID from [here](https://console.developers.google.com/apis/credentials) and download the resulting json file. Rename it to `client_secret.json` 8 | 4) Move both the scripts from this repo and the downloaded json file to a single directory 9 | 5) Open `create_download_list.py` and replace `Enter folder ID here` with the folder ID you want to download 10 | 6) Save and run `create_download_list.py` 11 | 7) If you are running the script for the first time, it will open your browser window and ask you to grant permissions to you app. Click on `Advanced` and proceed to grant the permission. Re-run `create_download_list.py` 12 | 8) You will end up with `filesList.txt` in your current directory. You can modify this file to remove the files you don't want to download 13 | 9) Run `download_files.py` and wait for the download to complete! 14 | -------------------------------------------------------------------------------- /create_download_list.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | from googleapiclient import discovery 4 | from httplib2 import Http 5 | from oauth2client import file, client, tools 6 | 7 | scopes = 'https://www.googleapis.com/auth/drive' 8 | store = file.Storage('storage.json') 9 | creds = store.get() 10 | if not creds or creds.invalid: 11 | flow = client.flow_from_clientsecrets('client_secret.json', scopes) 12 | creds = tools.run_flow(flow, store) 13 | service = discovery.build('drive', 'v3', http=creds.authorize(Http())) 14 | 15 | folder = 'application/vnd.google-apps.folder' 16 | mimeType = 'mimeType' 17 | 18 | 19 | def getFilesInFolder(folderId): 20 | response = service.files().list(q="'%s' in parents" % folderId, 21 | spaces='drive', 22 | fields='nextPageToken, files(id, name, mimeType)', 23 | pageToken=pageToken).execute() 24 | 25 | return response.get('files', []) 26 | 27 | 28 | pageToken = None 29 | 30 | root = 'Enter folder ID here' 31 | 32 | files = [] 33 | while True: 34 | response = service.files().list(q="'%s' in parents" % root, 35 | spaces='drive', 36 | fields='nextPageToken, files(id, name, mimeType)', 37 | pageToken=pageToken).execute() 38 | for file in response.get('files', []): 39 | if file.get(mimeType) == folder: 40 | subFolderList = getFilesInFolder(file.get('id')) 41 | files.extend(subFolderList) 42 | else: 43 | files.append(file) 44 | pageToken = response.get('nextPageToken', None) 45 | if pageToken is None: 46 | break 47 | 48 | with open("filesList.txt", 'w') as output: 49 | for file in files: 50 | output.write('%s (%s)' % (file.get('name'), file.get('id')) + '\n') 51 | -------------------------------------------------------------------------------- /download_files.py: -------------------------------------------------------------------------------- 1 | import io 2 | 3 | from googleapiclient import discovery 4 | from googleapiclient.http import MediaIoBaseDownload 5 | from httplib2 import Http 6 | from oauth2client import file, client, tools 7 | 8 | scopes = 'https://www.googleapis.com/auth/drive' 9 | store = file.Storage('storage.json') 10 | creds = store.get() 11 | if not creds or creds.invalid: 12 | flow = client.flow_from_clientsecrets('client_secret.json', scopes) 13 | creds = tools.run_flow(flow, store) 14 | service = discovery.build('drive', 'v3', http=creds.authorize(Http())) 15 | 16 | with open('filesList.txt') as f: 17 | items = f.read().splitlines() 18 | 19 | fileIds = list(map(lambda item: item[item.find("(") + 1:item.find(")")], items)) 20 | 21 | downloadFolderMeta = { 22 | 'name': 'TBD', 23 | 'mimeType': 'application/vnd.google-apps.folder' 24 | } 25 | 26 | downloadFolder = service.files().create(body=downloadFolderMeta, 27 | fields='id').execute().get('id') 28 | 29 | body = {'parents': [downloadFolder]} 30 | copiedFiles = [] 31 | 32 | for fileId in fileIds: 33 | copiedFile = service.files().copy(fileId=fileId, body=body).execute() 34 | copiedFileId = copiedFile.get('id') 35 | copiedFileName = copiedFile.get('name') 36 | request = service.files().get_media(fileId=copiedFileId) 37 | fh = io.FileIO(copiedFileName, 'wb') 38 | downloader = MediaIoBaseDownload(fh, request) 39 | done = False 40 | while done is False: 41 | status, done = downloader.next_chunk() 42 | print("Downloading %s %d%%" % (copiedFileName, int(status.progress() * 100))) 43 | print("Downloaded %s" % copiedFileName) 44 | service.files().delete(fileId=copiedFileId).execute() 45 | --------------------------------------------------------------------------------