In [1]:
import sys
import imaplib
import email
import datetime
import yaml
import re
from bs4 import BeautifulSoup
sys.path.append('../')
import jobs2phones as j2p
In [2]:
with open('../config.yaml') as f:
cf = yaml.safe_load(f)
In [35]:
def parse_text_message_from_email(msg):
'''
Gets the actual text sent to the email address by parsing it out of the email body
'''
text = {}
text['sender'] = msg['from']
msg_body = ''
if msg.is_multipart():
for i,part in enumerate(msg.walk()):
if part.get_content_type() =='text/plain':
msg_body = part.get_payload(decode=True)
elif part.get_content_type() =='text/html':
msg_soup = BeautifulSoup(part.get_payload(decode=True))
else:
msg_body = msg.get_payload(decode=True)
if len(msg_body) == 0:
msg_soup.find('body').text
msg_body.replace('\r','').replace('\n','')
text['message']=msg_body
return text
def parse_choices(choices_made):
'''
Takes a numbered list of choices and maps them to the relevant search criteria.
'''
search_criteria='';
for choice in choices_made:
if choice == '1':
search_criteria='dishwasher&20philadelphia ' + search_criteria
if choice == '2':
search_criteria='warehouse&20philadelphia ' + search_criteria
if choice == '3':
search_criteria='cook&20philadelphia ' + search_criteria
return search_criteria
def read_mailbox_and_edit_users(M):
"""
Processes mail in order to add,edit, and remove users
"""
Session = j2p.bind_to_database(cf['postgres_username'],cf['postgres_password'],cf['postgres_db'])
rv, data_num = M.search(None, "ALL")
if rv != 'OK':
print "No messages found!"
return
messages=[]
for num in data_num[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print "ERROR getting message", num
return
email_data = email.message_from_string(data[0][1])
text = parse_text_message_from_email(email_data)
choices_made = re.findall(r'\d+',text['message'])
if 'stop' in text['message'].lower():
j2p.delete_user(Session,text['sender'])
j2p.send_text(cf['fromaddr'],cf['username'],cf['password'],text['sender'],cf['stop_message'])
M.store(num , '+FLAGS', '\\Deleted') #This archives the message.
elif 'start' in text['message'].lower() or 'list' in text['message'].lower():
j2p.send_text(cf['fromaddr'],cf['username'],cf['password'],text['sender'],cf['start_message'])
M.store(num , '+FLAGS', '\\Deleted') #This archives the message.
elif len(choices_made) > 0:
search_criteria = parse_choices(choices_made)
if len(search_criteria) > 0:
if j2p.check_user(Session,text['sender']):
j2p.edit_user(Session,text['sender'],search_criteria)
else:
j2p.insert_user(Session,'',text['sender'],'',search_criteria)
j2p.send_text(cf['fromaddr'],cf['username'],cf['password'],text['sender'],
str(choices_made) + '. ' + cf['chosen_message'])
M.store(num , '+FLAGS', '\\Deleted') #This archives the message.
In [36]:
EMAIL_ACCOUNT = cf['username']
EMAIL_FOLDER = "INBOX"
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(EMAIL_ACCOUNT, cf['password'])
except imaplib.IMAP4.error:
print "LOGIN FAILED!!! "
sys.exit(1)
In [37]:
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print "Processing mailbox..."
read_mailbox_and_edit_users(M)
M.close()
else:
print "ERROR: Unable to open mailbox ", rv
In [10]:
M.logout()
Out[10]:
In [19]:
data_num = '1'
In [20]:
data_num.split()
Out[20]:
In [ ]: