Carson1

Untitled

Jul 28th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. from __future__ import print_function
  2. import base64, httplib2
  3. import os
  4. import io
  5. from apiclient import discovery
  6. from oauth2client.file import Storage
  7. from apiclient.http import MediaIoBaseDownload, MediaFileUpload
  8. from googleapiclient.discovery import build
  9. from httplib2 import Http
  10. from oauth2client import file, client, tools
  11. from pydrive.auth import GoogleAuth
  12. from pydrive.drive import GoogleDrive
  13. import os
  14. from google.colab import auth
  15. from oauth2client.client import GoogleCredentials
  16. from apiclient import errors
  17.  
  18. try:
  19.     import argparse
  20.     flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
  21. except ImportError:
  22.     flags = None
  23.  
  24. SCOPES = ['https://www.googleapis.com/auth/drive']
  25. CLIENT_SECRET_FILE = 'client_secret_84370201868-963093g2ktmndk4arl7igd1pmrc0ij94.apps.googleusercontent.com.json'
  26. APPLICATION_NAME = 'Drive API Python Quickstart'
  27.  
  28. def get_credentials():
  29.     home_dir = os.path.expanduser('~')
  30.     credential_dir = os.path.join(home_dir, '.credentials')
  31.     if not os.path.exists(credential_dir):
  32.         os.makedirs(credential_dir)
  33.     credential_path = os.path.join(credential_dir,
  34.                                    'credentials.json')
  35.  
  36.     store = Storage(credential_path)
  37.     credentials = store.get()
  38.     if not credentials or credentials.invalid:
  39.         flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
  40.         flow.user_agent = APPLICATION_NAME
  41.         if flags:
  42.             credentials = tools.run_flow(flow, store, flags)
  43.         else: # Needed only for compatibility with Python 2.6
  44.             credentials = tools.run(flow, store)
  45.         print('Storing credentials to ' + credential_path)
  46.     return credentials
  47.  
  48. credentials = get_credentials()
  49. http = credentials.authorize(httplib2.Http())
  50. service = discovery.build('drive', 'v3', http=http)
  51. def delete_file(service, file_id):
  52.   try:
  53.       service.files().delete(fileId=file_id).execute()
  54.   except errors.HttpError as error:
  55.       print('An error occurred: %s' % error)
  56.  
  57. gauth = GoogleAuth()
  58. if os.path.isfile("mycreds.txt") is False:
  59.     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]? : ")
  60.     if choice == "U":
  61.           print("Upload the mycreds.txt file")
  62.           from google.colab import files
  63.           files.upload()
  64.     elif choice == "B":
  65.           auth.authenticate_user()
  66.           gauth.credentials = GoogleCredentials.get_application_default()
  67.           gauth.SaveCredentialsFile("mycreds.txt")
  68.  
  69. gauth.LoadCredentialsFile("mycreds.txt")
  70. if gauth.access_token_expired:
  71.     gauth.Refresh()
  72. else: gauth.Authorize()
  73.  
  74. drive = GoogleDrive(gauth)
  75. folder_id = '1AJeR5NL27T3l9l0578XJ71x6KnKt_4Ai'
  76.  
  77. lister = drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
  78.  
  79. print(len(lister))
  80. for item in lister:
  81.     print(item['title'])
  82.     print('title: %s, mimeType: %s' % (item['title'], item['mimeType']))
  83.     mimetypes = {
  84.         'application/vnd.google-apps.document': 'application/pdf',
  85.  
  86.         'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  87.  
  88.     }
  89.     download_mimetype = None
  90.     for item in lister:
  91.         print(item['title'])
  92.         print(item['id'])
  93.         print(item)
  94.         item.GetContentFile(item['title'])
  95.         delete_file(service, item['id'])
Advertisement
Add Comment
Please, Sign In to add comment