Skip to content

Instantly share code, notes, and snippets.

@GaZ3ll3
Created May 18, 2013 04:17
Show Gist options
  • Save GaZ3ll3/5603212 to your computer and use it in GitHub Desktop.
Save GaZ3ll3/5603212 to your computer and use it in GitHub Desktop.
use gmail to reply metro bus route 19/FW schedule...
{
"SOUTH": {"1":"5:39","2":"6:29","3":"6:56","4":"7:31","5":"8:06","6":"8:41","7":"9:14","8":"9:49","9":"10:24","10":"11:30","11":"12:36","12":"13:42","13":"14:46","14":"15:52","15":"16:32","16":"17:19","17":"17:56", "18":"18:33","19":"19:07","20":"19:44","21":"20:44","22":"21:44"},
"NORTH": {"1":"5:46", "2":"6:26","3": "6:17","4": "7:52","5": "8:27","6":"9:02","7": "10:11", "8":"11:19","9":"12:25","10": "13:31","11":"14:37", "12":"15:49", "13":"16:30","14": "16:55", "15":"17:35","16": "18:22","17":"19:33","18": "20:33", "19":"21:33", "20":"22:33"}
}
import smtplib
import imaplib
import datetime
import email
import json
from pprint import pprint
import time
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
sender = 'your_gmail@gmail.com'
password = "your_password"
def send_mail(msg):
recipient = sender
subject = 'REPLY YOUR QUERY'
body = "" + msg + ""
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient,
"MIME-Version: 1.0",
"Content-Type: text/html"]
headers = "\r\n".join(headers)
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, password)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
session.quit()
def check_query():
#only today's email
session = imaplib.IMAP4_SSL('imap.gmail.com','993')
session.login(sender,password)
session.select("inbox")
date = mail_date("%d-%b-%Y")
(_, data) = session.search(None, ('UNSEEN'), '(SENTSINCE {0})'.format(date), '(FROM {0})'.format(sender.strip()))
ids = data[0].split()
#should check all ids
if len(ids)!=0:
for current_id in ids:
[_, bodies] = session.fetch(current_id, "(RFC822)") # fetch the email body (RFC822) for the given ID
email_message = email.message_from_string(bodies[0][1])
#print email_message['To']
#print email_message['Subject']
#print email.utils.parseaddr(email_message['From'])
#print email_message.items() # print all headers
query_entry = email_message['Subject']
query_data = get_msg(email_message)
time_list = mail_analysis(query_entry,query_data)#get time table
reply_msg = '\n'.join(time_list)+'\n\t Original Query:'+query_data+'\n'
send_mail(reply_msg)#forward the original query
else:
pass
def mail_date(format):
return (datetime.date.today() - datetime.timedelta(0)).strftime(format)
def mail_analysis(query_entry,email_msg):
if query_entry.lower() == 'bus':
query_string = email_msg.lower()
if '19' in query_string:
if 'south' in query_string:
return nextbus('19','south')#at home
elif 'north' in query_string:
return nextbus('19','north')#at school
else:
return nextbus('19','both')#print both
elif 'fw' in query_string:
if 'inbound' in query_string:
return nextbus('fw','inbound')#at home
elif 'outbound' in query_string:
return nextbus('fw','outbound')#at school
else:
return nextbus('fw','both')#print both
else:
pass
def write_log():
pass
def check_log():
pass
def get_msg(msg):
maintype = msg.get_content_maintype()
if maintype == 'multipart':
for part in msg.get_payload():
if part.get_content_maintype() == 'text':
return part.get_payload()
elif maintype == 'text':
return msg.get_payload()
def nextbus(route,direction):
reply_msg = []
if route == '19':
fid = open('19.json')
time_data = json.load(fid)
fid.close()
if direction.lower() in ['south','north']:
south_time = time_data[direction.upper()]
date = mail_date("%Y-%m-%d")
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
now_time_stamp = time.mktime(time.strptime(str(now), '%Y-%m-%d %H:%M:%S'))
for x in range(len(south_time)):
time_entry = date +" "+ south_time[str(x+1)]
time_stamp = time.mktime(time.strptime(time_entry, '%Y-%m-%d %H:%M'))
if now_time_stamp<time_stamp:
reply_msg.append(datetime.datetime.fromtimestamp(time_stamp).strftime('%Y-%m-%d %H:%M:%S'))
return reply_msg
if __name__ == '__main__':
check_query()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment