예제 #1
0
 def start_auth_telegram(self, client_config):
     if self.telegram_bot is None:
         t.log_message('telegram bot is None. Telegram auth canceled.')
         return
     auth = GoogleAuth()
     auth.LoadClientConfigFile(client_config_file=client_config)
     if auth.flow is None:
         auth.GetFlow()
     auth.flow.redirect_uri = OOB_CALLBACK_URN
     self.telegram_bot.send_message(
         'Please go to the following link in your browser and send me a Google verification code. \nAuth url: '
         + auth.GetAuthUrl())
     dirty = False
     code = None
     save_credentials = auth.settings.get('save_credentials')
     if auth.credentials is None and save_credentials:
         auth.LoadCredentials()
     if auth.credentials is None:
         code = self.telegram_bot.get_code()
         dirty = True
     else:
         if auth.access_token_expired:
             if auth.credentials.refresh_token is not None:
                 auth.Refresh()
             else:
                 code = self.telegram_bot.get_code()
             dirty = True
     if code is not None:
         auth.Auth(code)
     if dirty and save_credentials:
         auth.SaveCredentials()
     return auth
예제 #2
0
 def getGoogleDriveInstance(self):
     gauth = GoogleAuth()
     gauth.LocalWebserverAuth()
     gauth.LoadCredentials()
     if gauth.access_token_expired:
         gauth.Refresh()
     self.drive = GoogleDrive(gauth)
     # The following disables annoying warning ref. discovery_cache
     logging.getLogger('googleapiclient.discovery_cache').setLevel(
         logging.ERROR)
     return self.drive
예제 #3
0
def get_drive():
    with open('settings.yaml', 'wb') as fh:
        fh.write(settings)

    with open(cred, 'wb') as fh:
        fh.write(environ['AIRPLANE_CHARGE'].encode('ascii'))

    gauth = GoogleAuth()
    # gauth.LocalWebserverAuth()
    gauth.LoadCredentials()
    if gauth.access_token_expired:
        gauth.Refresh()

    drive = GoogleDrive(gauth)
    return drive
예제 #4
0
def Excel(thing_id, userid, con):
    gauth = GoogleAuth()
    gauth.DEFAULT_SETTINGS['save_credentials_file'] = os.path.join(
        os.path.dirname(__file__), 'credentials.json')
    gauth.DEFAULT_SETTINGS['save_credentials'] = True
    gauth.DEFAULT_SETTINGS['save_credentials_backend'] = 'file'
    gauth.LoadCredentials()
    print(gauth.credentials)
    gauth.Refresh()
    gauth.Authorize()
    drive = GoogleDrive(gauth)
    db = con.cursor()
    db.execute("SELECT * FROM buy_list WHERE thing_id={}".format(thing_id))
    data = db.fetchall()
    os.system("touch {}.csv".format(thing_id))
    file = open('{}.csv'.format(thing_id), 'w', encoding='utf-8-sig')
    csvCursor = csv.writer(file)
    csvCursor.writerow(['買家姓名', 'Line暱稱', '電話', '購買數量', '是否出貨', '購買時間'])
    for d in data:
        profile = line_bot_api.get_profile(d[3])
        db.execute("SELECT name,phone FROM user_list WHERE userid='{}'".format(
            d[3]))
        data_2 = db.fetchone()
        if d[4] == 'check':
            status = 'yes'
        else:
            status = 'no'
        csvCursor.writerow(
            [data_2[0], profile.display_name, data_2[1], d[2], status, d[5]])
        print([data_2[0], profile.display_name, data_2[1], d[2], status, d[5]])
    file.close()
    upload_file = drive.CreateFile()
    upload_file.SetContentFile('{}.csv'.format(thing_id))
    upload_file.Upload()
    upload_file.InsertPermission({
        'type': 'anyone',
        'value': 'anyone',
        'role': 'reader'
    })
    return TextSendMessage(text=upload_file['alternateLink'])
예제 #5
0
    def test_01_LocalWebserverAuthWithClientConfigFromFile(self):

        # Test if authentication works with config read from file
        packageName = "test.z.crypt"
        foldername = "SeccuDB"
        ga = GoogleAuth('settings/test1.yaml')
        #ga.getAuthURL()
        try:
            ga.LoadClientConfig()
            ga.LoadCredentials()
            ga.Authorize()
        except:

            print ga.GetAuthUrl()
            code = raw_input("code: ").strip()

            ga.Auth(code)
            ga.SaveCredentials()

        print "complete Auth"
        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()
예제 #6
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

import datetime
import os
import re

from flask import Flask, render_template, request
from docx import Document
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

g_login = GoogleAuth()
GoogleAuth.DEFAULT_SETTINGS['client_config_file'] = os.path.join(
    os.path.dirname(__file__), 'client_secrets.json')
g_login.LoadCredentials()
#g_login.CommandLineAuth()
#drive = GoogleDrive(self.g_login)

app = Flask(__name__)


def dataAtual(doc):
    meses = {
        '1': "Janeiro",
        '2': "Fevereiro",
        '3': "Março",
        '4': "Abril",
        '5': "Maio",
        '6': "Junho",
        '7': "Julho",
예제 #7
0
class CloudHandler:
	'''




	'''




	def __init__(self):
		self.DBConfig = AppConfig()
		self.FH = FileHandler()
		self.googleConfig = 'configs/gdrive.yaml'

	def ssl_seed(self,size=24, chars=string.ascii_uppercase + string.digits):
		self.randomString = ''.join(random.choice(chars) for _ in range(size))
		return self


	def authenticategoogle(self):
		self.ga = GoogleAuth(self.googleConfig)
		try:
			self.ga.LoadClientConfig()
			self.ga.LoadCredentials()
			self.ga.Authorize()
		except:
			return self.ga.GetAuthUrl()
			#code = raw_input("code: ").strip()
		else:
			return True
			#auth loaded ok

	def googleauthorize(self,authCode):
		code = authCode.strip()
		self.ga.Auth(code)
		self.ga.SaveCredentials()
		return True

	def googleupload(self,filename):
		packageName = filename
		self.ga = GoogleAuth(self.googleConfig)
		self.ga.LoadClientConfig()
		self.ga.LoadCredentials()
		self.ga.Authorize()
		drive = GoogleDrive(self.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 getgooglefileid(self,title):

		print os.getcwd()
		self.ga = GoogleAuth(self.DBConfig.googleyaml)
		self.ga.LocalWebserverAuth()
		drive = GoogleDrive(self.ga)
		flist = drive.ListFile({'q': "title = '%s' and trashed = false"%title})
		files = flist.GetList() 
		if len(files)==0:
			return False
		else:
			return files[0]['id']


	def simpleUploadGoogle(self,filename): #test_01_Files_Insert
		drive = GoogleDrive(self.ga)
		#f = drive.CreateFile({'fluidSecurity': parent_id})
		file1 = drive.CreateFile()
		file1.SetContentFile(filename) # Read local file
		file1.Upload() # Upload it
		
	

		return True
	##save file ID to config
	
	
	def simpleUpdateGoogle(self,filename,dbID):
		drive = GoogleDrive(self.ga)
	
		file1=drive.CreateFile({'id': dbID}) #overwrite by ID
		file1['title'] = filename
		file1.FetchContent()
		file1.SetContentFile(filename)
		file1.Upload() 
		return True
		
	
	def simpleDownloadGoogle(self,filename):
		DeleteOldFile(self.filename) #delete local version, not that safe
	
		drive = GoogleDrive(self.ga)
		file1 = drive.CreateFile()
		file1['title'] = filename
		file1.FetchContent()  # Force download and double check content
		return True
	
	
#	
#	
#	def ftpDown(filename,host,user,ftpass):
#		import ftplib
#		session = ftplib.FTP(host,user,ftpass)
#		file = open(filename, 'wb')  
#		session.retrbinary('RETR %s' % filename, file.write)
#		file.close()
#		session.quit()
#	


	


	def uploadftp(self,filename,host,user,password):	  

		# upload logic
		import ftplib
		session = ftplib.FTP(host,user,password)
		file = open(filename,'rb')				  # file to send
		session.storbinary('STOR ' +filename, file)	 # send the file
		file.close()									# close file and FTP
		session.quit()

		return True

	def authenticatedropbox(self):
		import dropbox

		# Get your app key and secret from the Dropbox developer website
		app_key = 'p2iu4n7f9yegl3u'
		app_secret = '0903whnau7p2zde'

		flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)

		authorize_url = flow.start()
		print "============= authenticate dropbox" + str(authorize_url)

		pickled  = pickle.dumps(flow)
		encodedPickle = base64.b64encode(pickled)
		self.DBConfig.putmap('cloudinfo','dropboxobject', str(encodedPickle) )

		return authorize_url



	def uploaddropbox(self,filename,authcode):
		import socket
		socket.RAND_add(self.ssl_seed().randomString, 75.0)
		import dropbox

		try:
			# Get your app key and secret from the Dropbox developer website
			# encodedPickle =self.DBConfig.mapget('cloudinfo')['dropboxobject']
			# decodedPickle = base64.b64decode(encodedPickle)
			# flow = pickle.loads(decodedPickle)

			app_key = 'p2iu4n7f9yegl3u'
			app_secret = '0903whnau7p2zde'

			flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)

			authorize_url = flow.start()
			print "============= authenticate dropbox" + str(authorize_url)


			print "loaded ok"
			print "------ authcode: " + str(authcode)
			access_token, user_id = flow.finish(authcode)

			print "finidhed oko"
			client = dropbox.client.DropboxClient(access_token)
			print "finidhed client"
			print 'linked account: ', client.account_info()

			#Uploading files
			f = open(filename, 'rb')
			response = client.put_file('/'+str(filename), f)
			print "uploaded:", response

		except Exception as e:
			print("Upload failed: " + str(e))
			return False
		return True

	def uploadgoogledrive(self,filename):
		googlefileid = self.getgooglefileid(filename)
		if googlefileid == False:
			self.simpleUploadGoogle(filename)
		else:
			self.simpleUpdateGoogle(filename,googlefileid)
		return True

	def uploadicloud(self,filename,email,password):

		return True

	def uploadskydrive(self,filename,email,password):	

		return True