Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. !pip install -U -q PyDrive
  2. import os
  3. from pydrive.auth import GoogleAuth
  4. from pydrive.drive import GoogleDrive
  5. from google.colab import auth
  6. from oauth2client.client import GoogleCredentials
  7.  
  8. # 1. Authenticate and create the PyDrive client.
  9. auth.authenticate_user()
  10. gauth = GoogleAuth()
  11. gauth.credentials = GoogleCredentials.get_application_default()
  12. drive = GoogleDrive(gauth)
  13.  
  14. # choose a local (colab) directory to store the data.
  15. local_download_path = os.path.expanduser('~/data')
  16. try:
  17.   os.makedirs(local_download_path)
  18. except: pass
  19.  
  20. # 2. Auto-iterate using the query syntax
  21. #    https://developers.google.com/drive/v2/web/search-parameters
  22. file_list = drive.ListFile(
  23.     {'q': "'1txNxCjMtbfsxUrvlR5kNCr0JWm-hm7mB' in parents"}).GetList()
  24. mimetypes = {
  25.         # Drive Document files as PDF
  26.         'application/vnd.google-apps.document': 'application/pdf',
  27.  
  28.         # Drive Sheets files as MS Excel files.
  29.         'application/vnd.google-apps.spreadsheet': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  30.  
  31.     # see https://developers.google.com/drive/v3/web/mime-types
  32.     }
  33. for f in file_list:
  34.   # 3. Create & download by id.
  35.   print('title: %s, id: %s' % (f['title'], f['id']))
  36.   fname = os.path.join(local_download_path, f['title'])
  37.   print('downloading to {}'.format(fname))
  38.   f_ = drive.CreateFile({'id': f['id']})
  39.   if f['mimeType'] in mimetypes:
  40.     download_mimetype = mimetypes[f['mimeType']]
  41.     f.GetContentFile(fname, mimetype=download_mimetype)
  42.     f.GetContentFile(fname, mimetype=download_mimetype)
  43.   else:
  44.     f_.GetContentFile(fname)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement