Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import base64, httplib2
- import os
- import io
- from apiclient import discovery
- from oauth2client.file import Storage
- from apiclient.http import MediaIoBaseDownload, MediaFileUpload
- from googleapiclient.discovery import build
- from httplib2 import Http
- from oauth2client import file, client, tools
- from pydrive.auth import GoogleAuth
- from pydrive.drive import GoogleDrive
- import os
- from google.colab import auth
- from oauth2client.client import GoogleCredentials
- from apiclient import errors
- try:
- import argparse
- flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
- except ImportError:
- flags = None
- SCOPES = ['https://www.googleapis.com/auth/drive']
- CLIENT_SECRET_FILE = 'client_secret_84370201868-963093g2ktmndk4arl7igd1pmrc0ij94.apps.googleusercontent.com.json'
- APPLICATION_NAME = 'Drive API Python Quickstart'
- def get_credentials():
- home_dir = os.path.expanduser('~')
- credential_dir = os.path.join(home_dir, '.credentials')
- if not os.path.exists(credential_dir):
- os.makedirs(credential_dir)
- credential_path = os.path.join(credential_dir,
- 'credentials.json')
- store = Storage(credential_path)
- credentials = store.get()
- if not credentials or credentials.invalid:
- flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
- flow.user_agent = APPLICATION_NAME
- if flags:
- credentials = tools.run_flow(flow, store, flags)
- else: # Needed only for compatibility with Python 2.6
- credentials = tools.run(flow, store)
- print('Storing credentials to ' + credential_path)
- return credentials
- credentials = get_credentials()
- http = credentials.authorize(httplib2.Http())
- service = discovery.build('drive', 'v3', http=http)
- def delete_file(service, file_id):
- try:
- service.files().delete(fileId=file_id).execute()
- except errors.HttpError as error:
- print('An error occurred: %s' % error)
- gauth = GoogleAuth()
- if os.path.isfile("mycreds.txt") is False:
- choice = input ("Do you want to: U) Upload authentication file (mycreds.txt). B) Browser authentication (only possible for owner of the connected Google drive folder). [U/B]? : ")
- if choice == "U":
- print("Upload the mycreds.txt file")
- from google.colab import files
- files.upload()
- elif choice == "B":
- auth.authenticate_user()
- gauth.credentials = GoogleCredentials.get_application_default()
- gauth.SaveCredentialsFile("mycreds.txt")
- gauth.LoadCredentialsFile("mycreds.txt")
- if gauth.access_token_expired:
- gauth.Refresh()
- else: gauth.Authorize()
- drive = GoogleDrive(gauth)
- folder_id = '1AJeR5NL27T3l9l0578XJ71x6KnKt_4Ai'
- lister = drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
- print(len(lister))
- for item in lister:
- print(item['title'])
- print('title: %s, mimeType: %s' % (item['title'], item['mimeType']))
- mimetypes = {
- 'application/vnd.google-apps.document': 'application/pdf',
- 'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
- }
- download_mimetype = None
- for item in lister:
- print(item['title'])
- print(item['id'])
- print(item)
- item.GetContentFile(item['title'])
- delete_file(service, item['id'])
Advertisement
Add Comment
Please, Sign In to add comment