Last active
April 24, 2019 07:08
-
-
Save BrianHung/08eb6f20a2cfc58f910498f7ed9ee078 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| from __future__ import print_function | |
| import pickle | |
| import os.path | |
| from googleapiclient.discovery import build | |
| from google_auth_oauthlib.flow import InstalledAppFlow | |
| from google.auth.transport.requests import Request | |
| from apiclient import errors | |
| from apiclient.http import MediaIoBaseDownload | |
| from googleapiclient.discovery import build | |
| from httplib2 import Http | |
| from oauth2client import file, client, tools | |
| import io | |
| import os | |
| import sys | |
| # If modifying these scopes, delete the file token.json. | |
| SCOPES = 'https://www.googleapis.com/auth/drive' | |
| def main(): | |
| """ | |
| Download folder content from google dirve without zipping. | |
| """ | |
| store = file.Storage('storage.json') | |
| creds = None | |
| # If there are no (valid) credentials available, let the user log in. | |
| if not creds or not creds.valid: | |
| if creds and creds.expired and creds.refresh_token: | |
| creds.refresh(Request()) | |
| else: | |
| flow = InstalledAppFlow.from_client_secrets_file( | |
| 'credentials.json', SCOPES) | |
| creds = flow.run_local_server() | |
| # Save the credentials for the next run | |
| with open('token.pickle', 'wb') as token: | |
| pickle.dump(creds, token) | |
| service = build('drive', 'v3', credentials=creds) | |
| print("Credentials are a-go! \n") | |
| folder_name = sys.argv[1] | |
| folder_id = '' | |
| location = '' | |
| if len(sys.argv) > 2: | |
| location = str(sys.argv[2], 'utf-8') | |
| if location[-1] != '/': | |
| location += '/' | |
| try: | |
| folder = service.files().list( | |
| q="name='{}' and mimeType='application/vnd.google-apps.folder'".format(folder_name), | |
| fields='files(id)').execute() | |
| folder_id = folder['files'][0][u'id'] | |
| print(folder_id, folder_name) | |
| folder_name = str(folder_name) | |
| download_folder(service, folder_id, location, folder_name) | |
| except Exception as error: | |
| print('An error occurred: {}'.format(error)) | |
| def download_folder(service, folder_id, location, folder_name): | |
| if not os.path.exists(location + folder_name): | |
| os.makedirs(location + folder_name) | |
| location += folder_name + '/' | |
| result = [] | |
| files = service.files().list( | |
| q="'{}' in parents".format(folder_id), | |
| fields='files(id, name, mimeType)').execute() | |
| result.extend(files['files']) | |
| result = sorted(result, key=lambda k: k[u'name']) | |
| total = len(result) | |
| current = 1 | |
| for item in result: | |
| file_id = item[u'id'] | |
| filename = item[u'name'] | |
| mime_type = item[u'mimeType'] | |
| print(file_id, filename, mime_type, '({}/{})'.format(current, total)) | |
| if mime_type == 'application/vnd.google-apps.folder': | |
| download_folder(service, file_id, location, filename) | |
| elif not os.path.isfile(location + filename): | |
| download_file(service, file_id, location, filename) | |
| current += 1 | |
| def download_file(service, file_id, location, filename): | |
| request = service.files().get_media(fileId=file_id) | |
| fh = io.FileIO(location + filename, 'wb') | |
| downloader = MediaIoBaseDownload(fh, request, 1024 * 1024 * 1024) | |
| done = False | |
| while done is False: | |
| status, done = downloader.next_chunk() | |
| print('\rDownload {}%.'.format(int(status.progress() * 100))) | |
| sys.stdout.flush() | |
| print() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment