Advertisement
Guest User

upload script

a guest
Aug 28th, 2014
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1.  
  2.     def googleupload(self,filename):
  3.         packageName = filename
  4.         ga = GoogleAuth(self.googleConfig)
  5.         ga.LoadClientConfig()
  6.         ga.LoadCredentials()
  7.         ga.Authorize()
  8.         drive = GoogleDrive(ga)
  9.         #flist = drive.ListFile({'q': "title contains '.crypt' and trashed = false"})
  10.         folderlistquery = drive.ListFile({'q': "title = 'SeccuDB' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"})
  11.        
  12.  
  13.         cloudfolder = folderlistquery.GetList()
  14.         if (len(cloudfolder) == 0):
  15.             #create folder
  16.             folder = drive.CreateFile()
  17.             folder['title'] = "SeccuDB"
  18.             folder['mimeType'] = "application/vnd.google-apps.folder"
  19.             folder['parents'] = "root"
  20.             folder.Upload()
  21.             cloudfolder = folderlistquery.GetList()
  22.             if (len(cloudfolder) == 0):
  23.                 print "error"
  24.                 raise error('GooglePermissionsError')
  25.  
  26.  
  27.         cloudfolderid = cloudfolder[0]['id']
  28.         print cloudfolderid
  29.  
  30.         databaseListquery = drive.ListFile({'q': "'%s' in parents and trashed = false" % (cloudfolderid)})
  31.         databaseList = databaseListquery.GetList()
  32.  
  33.  
  34.         database_file = drive.CreateFile()
  35.         database_file['title'] = packageName
  36.         database_file['parents']=[{"kind": "drive#fileLink" ,'id': str(cloudfolderid) }]
  37.         #check if already exists, if so, get id and update
  38.         databasenamelist = []
  39.         for databaseAvaliable in databaseList:
  40.             databasenamelist.append(databaseAvaliable['title'])
  41.         if packageName in databasenamelist:
  42.             cloudPackageQuery = drive.ListFile({'q': "title = '%s' and trashed = false" % (packageName)})
  43.             cloudPackage = cloudPackageQuery.GetList()
  44.  
  45.             if(len(cloudPackage) > 1): # if theres more than one, go for the most recent
  46.                 packdates = []
  47.                 for everypack in cloudPackage:
  48.                     packdates.append((everypack['modifiedByMeDate'],everypack['id']))
  49.                 database_file['id'] = sorted(packdates,reverse=True)[0][1]
  50.  
  51.             else:
  52.                 database_file['id'] = cloudPackage[0]['id']
  53.  
  54.         database_file.Upload()
  55.         return True
  56.  
  57.  
  58.    
  59.     def listgoogledrive(self):
  60.         ga = GoogleAuth(self.googleConfig)
  61.         ga.LoadClientConfig()
  62.         ga.LoadCredentials()
  63.         ga.Authorize()
  64.         drive = GoogleDrive(ga)
  65.         #flist = drive.ListFile({'q': "title contains '.crypt' and trashed = false"})
  66.         folderlistquery = drive.ListFile({'q': "title = 'SeccuDB' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"})
  67.        
  68.  
  69.         cloudfolder = folderlistquery.GetList()
  70.         if (len(cloudfolder) == 0):
  71.             print "no databases available"
  72.             return False
  73.  
  74.  
  75.  
  76.  
  77.         cloudfolderid = cloudfolder[0]['id'] # id of app foler
  78.         FileListquery = drive.ListFile({'q': "'%s' in parents and trashed = false" % (cloudfolderid)})
  79.         FileList = FileListquery.GetList() #get list of files in that folder
  80.  
  81.  
  82.         #check if already exists, if so, get id and update
  83.         databasenamelist = []
  84.         databaseidlist = []
  85.         mappedIds = {}
  86.         if (len(FileList) == 0):
  87.             databasenamelist.append("None")
  88.         else:
  89.             for cloudFile in FileList:
  90.                 if(cloudFile['title'].endswith('.crypt')): #if legit db
  91.                     databasenamelist.append(cloudFile['title'])
  92.                     databaseidlist.append(cloudFile['id'])
  93.                     mappedIds[cloudFile['title']] = cloudFile['id']
  94.  
  95.         self.google_filelist = databasenamelist
  96.         self.google_fileids = databaseidlist
  97.         self.google_files_mapped = mappedIds
  98.         return self
  99.  
  100.     def googledownload(self,fileID):
  101.         import urllib, urllib2, os, sys
  102.  
  103.         ga = GoogleAuth(self.googleConfig)
  104.         ga.LoadClientConfig()
  105.         ga.LoadCredentials()
  106.         ga.Authorize()
  107.         drive = GoogleDrive(ga)
  108.  
  109.  
  110.         file1 = drive.CreateFile()
  111.  
  112.         file1['id'] = fileID
  113.         file1.FetchContent()  # Force download metadata and double check content
  114.        
  115.         print "fetch: " + str(file1["webContentLink"])
  116.        
  117.        
  118.         #The pydrive package download
  119.         page=file1["webContentLink"]
  120.         request=urllib.urlretrieve(page, file1["title"])
  121.         if os.path.exists(os.path.join(os.path.dirname(__file__),file1["title"])):
  122.             print "YUP THE FILE EXISTS"
  123.             return True
  124.         else:
  125.             print "nope"
  126.             return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement