Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from pydrive.auth import GoogleAuth
  4. from pydrive.drive import GoogleDrive
  5.  
  6. # TODO: folders
  7. # TODO: is:unorganized
  8.  
  9. sourceCredentialsFile = "source_credentials.json"
  10. destinationCredentialsFile = "destination_credentials.json"
  11. rootId = "<dirHash>"
  12. owner = "me"
  13. folderMimeType = "application/vnd.google-apps.folder"
  14. ignoredMimeTypes = [
  15.   "application/vnd.google-apps.presentation",
  16.   "application/vnd.google-apps.spreadsheet",
  17.   "application/vnd.google-apps.document",
  18.   folderMimeType
  19. ]
  20. foldersQuery = "'%s' in parents and mimeType = '" + folderMimeType + "' and trashed = false"
  21. myFilesQuery = "'%s' in parents and " + " and ".join("mimeType != '%s'" % (x) for x in ignoredMimeTypes) \
  22.   + " and '" + owner + "' in owners and trashed = false"
  23. forceDelete = True
  24.  
  25. def authenticate(credentialsFile):
  26.   auth = GoogleAuth()
  27.   auth.LoadCredentialsFile(credentialsFile)
  28.   if auth.credentials is None:
  29.     auth.LocalWebserverAuth()
  30.   elif auth.access_token_expired:
  31.     auth.Refresh()
  32.   else:
  33.     auth.Authorize()
  34.   auth.SaveCredentialsFile(credentialsFile)
  35.   return GoogleDrive(auth)
  36.  
  37. def downloadFile(drive, file):
  38.   file = drive.CreateFile({'id': file["id"]})
  39.   file.GetContentFile(file["title"])
  40.  
  41. def copyFile(drive, file):
  42.   print(
  43.     "copyFile(): id: '%s', title: '%s', mimeType: '%s'" %
  44.     (file["id"], file["title"], file["mimeType"])
  45.   )
  46.  
  47.   newFile = drive.auth.service \
  48.     .files() \
  49.     .copy(
  50.       fileId = file["id"],
  51.       body = { "title": file["title"] }
  52.     ) \
  53.     .execute()
  54.   return newFile
  55.  
  56. def trashFile(drive, file):
  57.   print(
  58.     "trashFile(): id: '%s', title: '%s', mimeType: '%s'" %
  59.     (file["id"], file["title"], file["mimeType"])
  60.   )
  61.  
  62.   file.Trash()
  63.  
  64. def deleteFile(drive, file):
  65.   print(
  66.     "deleteFile(): id: '%s', title: '%s', mimeType: '%s'" %
  67.     (file["id"], file["title"], file["mimeType"])
  68.   )
  69.  
  70.   file.Delete()
  71.  
  72. def recurse(parentId):
  73.   files = sourceDrive.ListFile({"q": myFilesQuery % (parentId)}) \
  74.     .GetList()
  75.   for file in files:
  76.     copyFile(destinationDrive, file)
  77.     if (forceDelete):
  78.       deleteFile(sourceDrive, file)
  79.     else:
  80.       trashFile(sourceDrive, file)
  81.  
  82.   folders = sourceDrive.ListFile({"q": foldersQuery % (parentId)}) \
  83.     .GetList()
  84.   for folder in folders:
  85.     recurse(folder["id"])
  86.  
  87. sourceDrive = authenticate(sourceCredentialsFile)
  88. destinationDrive = authenticate(destinationCredentialsFile)
  89.  
  90. recurse(rootId)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement