def get_label_ids(label_names): service = cm.create_service() label_ids = [] label_ids_to_names = { } new_labels = label_names[:] try: label_data = service.users().labels().list(userId = 'me').execute() label_list = label_data['labels'] for label in label_list: if label['name'] in label_names: label_ids_to_names[label['id']] = label['name'] label_ids.append(label['id']) new_labels.remove(label['name']) for new_label in new_labels: label_object = { 'messsageListVisilibity': 'show', 'name': new_label, 'labelListVisibility': 'labelShow' } result = service.users().labels().create(userId = 'me', body = label_object).execute() label_ids_to_names[result['id']] = new_label label_ids.append(result['id']) print('Created label %s' % (new_label,)) except errors.HttpError: error = None print(error) return (label_ids, label_ids_to_names)
def __init__(self, label_id, label_name, queue, lock, freq): threading.Thread.__init__(self) self.service = cm.create_service() self.threadID = label_id + 'respond' self.label_id = label_id self.label_name = label_name self.q = queue self.q_lock = lock self.freq = freq self.counter = 0
def format_session_info(label_names): #add start_day functionality later service = cm.create_service() session_addresses = { label_name: { 'from_emails': set() } for label_name in label_names } provider_addresses = {} label_ids, label_ids_to_names = lt.get_label_ids(label_names) user_id = 'me' message_format = "full" try: for label_id in label_ids: label_name = label_ids_to_names[label_id] message_ids = cm.get_message_ids(service, label_id) for message in message_ids: try: message_data = service.users().messages().get( userId=user_id, id=message, format=message_format).execute() #print(message_data['payload']['parts']) from_data = filter(lambda x: x['name'] == 'From', message_data['payload']['headers']) if len(from_data) == 0: continue from_value = from_data[0]['value'].encode('UTF-8') body_data = message_data['payload']['parts'][0]['body'][ 'data'].encode('UTF-8') body_value = base64.urlsafe_b64decode(body_data) if address_is_phone(from_value): phone_number = get_phone_from_text(from_value).group() email_matches = get_email_from_text(body_value) if phone_number not in provider_addresses: provider_addresses[ phone_number] = get_email_from_text( from_value)[0] if phone_number in session_addresses[label_name].keys( ): session_addresses[label_name][phone_number].update( email_matches) else: session_addresses[label_name][phone_number] = set( email_matches) else: email_address = get_email_from_text(from_value) session_addresses[label_name]['from_emails'].update( email_address) except errors.HttpError, error: print(error) continue except errors.HttpError, error: print(error) pass
def send_emails_to_all(email_list, subject, text): service = cm.create_service() user_id = 'me' for email_address in email_list: email_message = lt.createMessage(email_address, subject, text) try: message_result = service.users().messages().send( userId=user_id, body=email_message).execute() except errors.HttpError, error: print(error) continue