Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- from pydrive.auth import GoogleAuth
- from pydrive.drive import GoogleDrive
- # TODO: folders
- # TODO: is:unorganized
- sourceCredentialsFile = "source_credentials.json"
- destinationCredentialsFile = "destination_credentials.json"
- rootId = "<dirHash>"
- owner = "me"
- folderMimeType = "application/vnd.google-apps.folder"
- ignoredMimeTypes = [
- "application/vnd.google-apps.presentation",
- "application/vnd.google-apps.spreadsheet",
- "application/vnd.google-apps.document",
- folderMimeType
- ]
- foldersQuery = "'%s' in parents and mimeType = '" + folderMimeType + "' and trashed = false"
- myFilesQuery = "'%s' in parents and " + " and ".join("mimeType != '%s'" % (x) for x in ignoredMimeTypes) \
- + " and '" + owner + "' in owners and trashed = false"
- forceDelete = True
- def authenticate(credentialsFile):
- auth = GoogleAuth()
- auth.LoadCredentialsFile(credentialsFile)
- if auth.credentials is None:
- auth.LocalWebserverAuth()
- elif auth.access_token_expired:
- auth.Refresh()
- else:
- auth.Authorize()
- auth.SaveCredentialsFile(credentialsFile)
- return GoogleDrive(auth)
- def downloadFile(drive, file):
- file = drive.CreateFile({'id': file["id"]})
- file.GetContentFile(file["title"])
- def copyFile(drive, file):
- print(
- "copyFile(): id: '%s', title: '%s', mimeType: '%s'" %
- (file["id"], file["title"], file["mimeType"])
- )
- newFile = drive.auth.service \
- .files() \
- .copy(
- fileId = file["id"],
- body = { "title": file["title"] }
- ) \
- .execute()
- return newFile
- def trashFile(drive, file):
- print(
- "trashFile(): id: '%s', title: '%s', mimeType: '%s'" %
- (file["id"], file["title"], file["mimeType"])
- )
- file.Trash()
- def deleteFile(drive, file):
- print(
- "deleteFile(): id: '%s', title: '%s', mimeType: '%s'" %
- (file["id"], file["title"], file["mimeType"])
- )
- file.Delete()
- def recurse(parentId):
- files = sourceDrive.ListFile({"q": myFilesQuery % (parentId)}) \
- .GetList()
- for file in files:
- copyFile(destinationDrive, file)
- if (forceDelete):
- deleteFile(sourceDrive, file)
- else:
- trashFile(sourceDrive, file)
- folders = sourceDrive.ListFile({"q": foldersQuery % (parentId)}) \
- .GetList()
- for folder in folders:
- recurse(folder["id"])
- sourceDrive = authenticate(sourceCredentialsFile)
- destinationDrive = authenticate(destinationCredentialsFile)
- recurse(rootId)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement