-
Notifications
You must be signed in to change notification settings - Fork 3
/
cafe_winner.py
executable file
·48 lines (32 loc) · 988 Bytes
/
cafe_winner.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
#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText
from bs4 import BeautifulSoup
import requests
URL = 'https://staff.ucar.edu/for-staff/daily/menu'
def has_cg(tag):
return tag.get_text()[:2] == 'CG'
def get_menu_page(url):
r = requests.get(url)
return r.text
def get_winner(url):
html_doc = get_menu_page(url)
soup = BeautifulSoup(html_doc, 'lxml')
cg = soup.find_all(has_cg)
cg_winner = cg[0].get_text()
cg_winner = ' '.join(cg_winner.split())
return cg_winner
def send_winner(winner, address):
msg = MIMEText(winner)
msg['Subject'] = 'This week\'s cafeteria winner'
msg['From'] = 'mgalloy@ucar.edu'
msg['To'] = address
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
if __name__ == "__main__":
try:
winner = 'Cafeteria winner: %s' % get_winner(URL)
except:
winner = 'Problem retreiving cafeteria winner'
send_winner(winner, 'mgalloy@ucar.edu')