Ejemplo n.º 1
0
 def get_credential_json_object(self):
     
     if type(self.auth_obj)==client.OAuth2Credentials:
         return self.auth_obj.to_json()
     else:
         apps_error_logger.info("GoogleAuthError:In "+str(self.__class__.__name__)+", auth object is not credential type")
         raise Exception("Auth Object is not credential type")
Ejemplo n.º 2
0
    def download_file(self, trigger_data=None):
        try:
            if self.data_obj['path'] is not None:
                download_path = self.data_obj['path'] + '/' + self.data_obj[
                    'filename']
            else:
                download_path = '/' + self.data_obj['filename']

            metadata = self.dropbox_obj.files_get_metadata(download_path)
            dropbox_filename = metadata.name
            if metadata.name is not None:
                dropbox_filename = metadata.name
            else:
                dropbox_filename = 'downloaded_file'

            metadata = self.dropbox_obj.files_download_to_file(
                DOWNLOAD_LOCATION + dropbox_filename, download_path)

            apps_logger.info(
                "Dropbox:DownloadAction:ActionSuccessful - ZapID=" +
                str(self.data_obj['zap_id']))

        except Exception as e:
            apps_error_logger.info(
                "DropboxError:DownloadAction:ActionFailed:" + str(e))
Ejemplo n.º 3
0
    def __init__(self, auth_obj, scope):
        self.auth_obj=auth_obj
        self.service = None
        self.execute_function = None
        
        if scope is None or scope=='':
            self.scope = 'https://www.googleapis.com/auth/userinfo.email'
        else:
            self.scope = scope
        
        try:
            if auth_obj is None or auth_obj=='':
                google_auth_obj = auth.GoogleAuth(self.scope)
                credentials = google_auth_obj.authorize()
                self.auth_obj = credentials
            else:
                credentials = client.Credentials()
                self.auth_obj = credentials.new_from_json(auth_obj)

            if self.auth_obj.invalid:
                google_auth_obj = auth.GoogleAuth(self.scope)
                credentials = google_auth_obj.authorize()

                self.auth_obj = credentials
        
        except Exception as err:
            apps_error_logger.info("GoogleAuthError:In "+str(self.__class__.__name__)+", "+str(err))
Ejemplo n.º 4
0
    def download_file(self, trigger_data=None):
        try:
            file_id = self.data_obj['file_id']
            drive = self.service
            file_response = drive.files().get(fileId=file_id).execute()

            filename = file_response["name"]
            mimeType = file_response["mimeType"]

            response = drive.about().get(fields='exportFormats').execute()
            exportFormats = response['exportFormats']

            useMimeType = exportFormats[mimeType][0]

            file_data = drive.files().get_media(fileId=file_id)
            file_handle = io.BytesIO()

            downloader = http.MediaIoBaseDownload(file_handle, file_data)
            done = False
            
            while done is False:
                status, done = downloader.next_chunk()
            
            f = open(DOWNLOAD_LOCATION+filename, 'wb+')
            f.write(file_handle.getvalue())
            f.close()
            
            apps_logger.info("GoogleDrive:DownloadFileAction:ActionSuccessful - ZapID:"+str(self.data_obj['zap_id']))

        except HttpError as httperr:
            apps_logger.info("GoogleDrive:DownloadFileAction:ActionException:Got HTTP error, using export_media - ZapID:"+str(self.data_obj['zap_id']))

            file_data = drive.files().export_media(fileId=file_id, mimeType=useMimeType)
            file_handle = io.BytesIO()

            downloader = http.MediaIoBaseDownload(file_handle, file_data)
            done = False
            
            while done is False:
                status, done = downloader.next_chunk()
            
            f = open(DOWNLOAD_LOCATION+filename, 'wb+')
            f.write(file_handle.getvalue())
            f.close()

            apps_logger.info("GoogleDrive:DownloadFileAction:ActionSuccessful - ZapID:"+str(self.data_obj['zap_id']))            
        
        except Exception as err:
            apps_error_logger.info("GoogleDriveError:DownloadFileAction:ActionFailed:"+str(err))
Ejemplo n.º 5
0
    def append_row(self, trigger_data):
        try:
            sheets = self.service
            sheet_id = self.data_obj['sheet_id']
            sheet_name = self.data_obj['sheet_name']
            sheet_range = self.data_obj['sheet_range']
    
            if ' ' in sheet_name:
                sheet_name = '"'+sheet_name+'"'
            sheet_range = sheet_name+"!"+sheet_range
            
            value_list = []
            msg_data_list = trigger_data

            for msg in msg_data_list:
                msg_array = []
    
                if 'headers' in msg:
                    header_list = msg['headers_list']
                    header_data = msg['headers']
                    for header in header_list:
                        for header_dict in header_data:
                            if header==header_dict['name']:
                                msg_array.append(header_dict['value'])
                                continue
    
                for key in msg:
                    if key=='headers' or key=='headers_list':
                        pass
                    else:
                        msg_array.append(msg[key])
                
                value_list.append(msg_array)

            insertDataOption = "INSERT_ROWS"
            valueInputOption = "USER_ENTERED"

            body = {
                "majorDimension": "ROWS",
                "values": value_list
            }

            sheet_update_response = sheets.spreadsheets().values().append(spreadsheetId=sheet_id, range=sheet_range, body=body, insertDataOption=insertDataOption, valueInputOption=valueInputOption).execute()
            apps_logger.info("GoogleDrive:AppendRowAction:ActionSuccessful - ZapID:"+str(self.data_obj['zap_id']))

        except Exception as err:
            apps_error_logger.info("GoogleDriveError:AppendRowAction:ActionFailed:"+str(err))
Ejemplo n.º 6
0
    def upload_file(self, trigger_data):
        try:
            if self.data_obj['path'] is not None:
                upload_path = self.data_obj['path'] + '/' + self.data_obj[
                    'filename']
            else:
                upload_path = '/' + self.data_obj['filename']

            self.dropbox_obj.files_upload(trigger_data['upload_data'],
                                          upload_path,
                                          mode=files.WriteMode('overwrite'))
            apps_logger.info("Dropbox:UploadAction:ActionSuccessful - ZapID=" +
                             str(self.data_obj['zap_id']))

        except Exception as e:
            apps_error_logger.info("DropboxError:UploadAction:ActionFailed:" +
                                   str(e))
Ejemplo n.º 7
0
    def email_trigger(self):
        try:
            after_time = core_utility.get_current_epoch()-900    # assumes last run time would be 15 mins back and gets that.
            email = self.service
            query = self.data_obj["search_query"]
            query += " after:"+str(after_time)

            response = email.users().messages().list(userId="me", q=query).execute()

            messages = []
            if 'messages' in response:
                messages.extend(response['messages'])

            while 'nextPageToken' in response:
                page_token = response['nextPageToken']
                response = email.users().messages().list(userId="me", q=query, pageToken=page_token).execute()
                messages.extend(response['messages'])

            header_values = self.data_obj["headers"]
            get_mail_body = self.data_obj["mailBodyBool"]

            return_body = []

            for message in messages:
                msg_dict = {}
    
                if len(header_values)>0:
                    msg_dict['headers_list'] = header_values
                    response = email.users().messages().get(userId="me", id=message["id"], format="metadata", metadataHeaders=header_values).execute()
                    msg_dict["headers"] = response["payload"]["headers"]
    
                if get_mail_body:
                    response = email.users().messages().get(userId="me", id=message["id"], format="raw").execute()
                    msg_dict["body"] = get_text_from_email(response["raw"])
    
                return_body.append(msg_dict)

            if len(return_body)>0:
                apps_logger.info("GoogleDrive:EMailTrigger:Triggered - ZapID:"+str(self.data_obj['zap_id']))
                return True, return_body
            else:
                return False, return_body
    
        except Exception as err:
            apps_error_logger.info("GoogleDriveError:EMailTrigger:TriggerFailed:"+str(err), exc_info=True)
            return False, return_body
Ejemplo n.º 8
0
    def check_last_update_time(self):
        try:
            if self.data_obj['path'] != None:
                file_path = self.data_obj['path'] + '/' + self.data_obj[
                    'filename']
            else:
                file_path = '/' + self.data_obj['filename']

            update_duration = int(
                self.data_obj['check_update_duration_seconds'])
            file_revision_list = self.dropbox_obj.files_list_revisions(
                file_path, mode=files.ListRevisionsMode('path', None), limit=1)

            if file_revision_list.is_deleted:
                return False, "File was deleted at " + str(
                    file_revision_list.server_deleted)

            else:
                list_entries = file_revision_list.entries
                latest_revision_datetime = list_entries[0].server_modified
                current_datetime = utility.get_current_datetime()

                if (current_datetime - latest_revision_datetime
                    ).total_seconds() < update_duration:
                    apps_logger.info(
                        "Dropbox:FileUpdateTrigger:Triggered ZapID=" +
                        str(self.data_obj['zap_id']))
                    return True, "File updated in last " + str(
                        int(update_duration) / 60) + " minutes"

                apps_logger.info(
                    "Dropbox:FileUpdateTrigger:Triggered:NoUpdate ZapID=" +
                    str(self.data_obj['zap_id']))
                return False, "File was not updated in last " + str(
                    int(update_duration) / 60) + " minutes"

        except KeyError as err:
            apps_error_logger.info(
                "DropboxError:FileUpdateTrigger:TriggerFailed" + str(err))
            return False, "Some data missing"

        except Exception as e:
            apps_error_logger.info(
                "DropboxError:FileUpdateTrigger:TriggerFailed:" + str(e))
            return False, "Some error occured"
Ejemplo n.º 9
0
    def file_modified(self):
        try:
            file_id = self.data_obj['file_id']
            update_check_duration_seconds = self.data_obj['check_update_duration_seconds']
            drive = self.service
    
            response = drive.files().get(fileId=file_id, fields='modifiedTime').execute()

            modifiedTime = datetime.datetime.strptime(response["modifiedTime"], "%Y-%m-%dT%H:%M:%S.%fZ")
            currentUTCTime = core_utility.get_current_utc_datetime()
            
            if (currentUTCTime-modifiedTime).total_seconds()<float(update_check_duration_seconds):
                apps_logger.info("GoogleDrive:FileModifiedTrigger:Triggered - ZapID:"+str(self.data_obj['zap_id']))
                return True, "File Modified in last check duration"
            else:
                return False, "Not modified in last check duration"
    
        except Exception as err:
            apps_error_logger.info("GoogleDriveError:FileModifiedTrigger:TriggerFailed:"+str(err))
            return False, "Some error occured"