Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def googleupload(self,filename):
- packageName = filename
- ga = GoogleAuth(self.googleConfig)
- ga.LoadClientConfig()
- ga.LoadCredentials()
- ga.Authorize()
- drive = GoogleDrive(ga)
- #flist = drive.ListFile({'q': "title contains '.crypt' and trashed = false"})
- folderlistquery = drive.ListFile({'q': "title = 'SeccuDB' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"})
- cloudfolder = folderlistquery.GetList()
- if (len(cloudfolder) == 0):
- #create folder
- folder = drive.CreateFile()
- folder['title'] = "SeccuDB"
- folder['mimeType'] = "application/vnd.google-apps.folder"
- folder['parents'] = "root"
- folder.Upload()
- cloudfolder = folderlistquery.GetList()
- if (len(cloudfolder) == 0):
- print "error"
- raise error('GooglePermissionsError')
- cloudfolderid = cloudfolder[0]['id']
- print cloudfolderid
- databaseListquery = drive.ListFile({'q': "'%s' in parents and trashed = false" % (cloudfolderid)})
- databaseList = databaseListquery.GetList()
- database_file = drive.CreateFile()
- database_file['title'] = packageName
- database_file['parents']=[{"kind": "drive#fileLink" ,'id': str(cloudfolderid) }]
- #check if already exists, if so, get id and update
- databasenamelist = []
- for databaseAvaliable in databaseList:
- databasenamelist.append(databaseAvaliable['title'])
- if packageName in databasenamelist:
- cloudPackageQuery = drive.ListFile({'q': "title = '%s' and trashed = false" % (packageName)})
- cloudPackage = cloudPackageQuery.GetList()
- if(len(cloudPackage) > 1): # if theres more than one, go for the most recent
- packdates = []
- for everypack in cloudPackage:
- packdates.append((everypack['modifiedByMeDate'],everypack['id']))
- database_file['id'] = sorted(packdates,reverse=True)[0][1]
- else:
- database_file['id'] = cloudPackage[0]['id']
- database_file.Upload()
- return True
- def listgoogledrive(self):
- ga = GoogleAuth(self.googleConfig)
- ga.LoadClientConfig()
- ga.LoadCredentials()
- ga.Authorize()
- drive = GoogleDrive(ga)
- #flist = drive.ListFile({'q': "title contains '.crypt' and trashed = false"})
- folderlistquery = drive.ListFile({'q': "title = 'SeccuDB' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"})
- cloudfolder = folderlistquery.GetList()
- if (len(cloudfolder) == 0):
- print "no databases available"
- return False
- cloudfolderid = cloudfolder[0]['id'] # id of app foler
- FileListquery = drive.ListFile({'q': "'%s' in parents and trashed = false" % (cloudfolderid)})
- FileList = FileListquery.GetList() #get list of files in that folder
- #check if already exists, if so, get id and update
- databasenamelist = []
- databaseidlist = []
- mappedIds = {}
- if (len(FileList) == 0):
- databasenamelist.append("None")
- else:
- for cloudFile in FileList:
- if(cloudFile['title'].endswith('.crypt')): #if legit db
- databasenamelist.append(cloudFile['title'])
- databaseidlist.append(cloudFile['id'])
- mappedIds[cloudFile['title']] = cloudFile['id']
- self.google_filelist = databasenamelist
- self.google_fileids = databaseidlist
- self.google_files_mapped = mappedIds
- return self
- def googledownload(self,fileID):
- import urllib, urllib2, os, sys
- ga = GoogleAuth(self.googleConfig)
- ga.LoadClientConfig()
- ga.LoadCredentials()
- ga.Authorize()
- drive = GoogleDrive(ga)
- file1 = drive.CreateFile()
- file1['id'] = fileID
- file1.FetchContent() # Force download metadata and double check content
- print "fetch: " + str(file1["webContentLink"])
- #The pydrive package download
- page=file1["webContentLink"]
- request=urllib.urlretrieve(page, file1["title"])
- if os.path.exists(os.path.join(os.path.dirname(__file__),file1["title"])):
- print "YUP THE FILE EXISTS"
- return True
- else:
- print "nope"
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement