-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_reader.py
executable file
·67 lines (55 loc) · 2.06 KB
/
menu_reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
import feedparser
import smtplib
import datetime
from BeautifulSoup import BeautifulSoup
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
now = datetime.datetime.now().strftime('%a, %d %b %Y')
def get_menu(dining_hall):
feed = {'Maseeh':398, 'Baker':399}[dining_hall]
url = 'http://legacy.cafebonappetit.com/rss/menu/' + str(feed)
week = feedparser.parse(url)
today = 0
for entry in week['entries']:
today = entry['summary'] if entry['title'] == now else today
if not today:
return 'Error getting today\'s menu for ' + dining_hall
today = BeautifulSoup(today)
current_meal = 'Breakfast'
meals = {'Lunch':'', 'Breakfast':'', 'Brunch':'', 'Dinner':''}
for item in today:
for meal in meals.keys():
current_meal = current_meal if meal not in item else meal
if item == '\n':
continue
meals[current_meal] += str(item).replace(' ', '').replace('\n','')
return meals['Dinner']
print 'Maseeh:'
print get_menu('Maseeh') + '\n'
print 'Baker:'
print get_menu('Baker')
me = "cyrbritt@mit.edu"
you = "cyrbritt@mit.edu"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Dinner Menus"
msg['From'] = me
msg['To'] = you
# Create the body of the message (a plain-text and an HTML version).
text = "Menus"
html = "<h1>Maseeh</h1>" + get_menu('Maseeh') + "<h1>Baker</h1>" + get_menu('Baker')
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()