-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu.py
executable file
·58 lines (40 loc) · 1.4 KB
/
menu.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
#!/usr/bin/env python
from bs4 import BeautifulSoup
import requests
URL = 'http://www2.ucar.edu/for-staff/daily/menu'
def has_class(tag):
return tag.has_attr('class') and 'views-field-field-date-required' in tag['class']
def has_cg(tag):
return tag.get_text()[:3] == 'CG:'
def get_menu_page(url):
r = requests.get(url)
return r.text
def get_menu(url):
html_doc = get_menu_page(url)
soup = BeautifulSoup(html_doc, 'lxml')
dates = soup.find_all(has_class)
today = dates[0]
today_string = ''.join(today.stripped_strings)
underscores = len(today_string) * '-'
breakfast = today.next_sibling.next_sibling
breakfast_string = '\n '.join(breakfast.stripped_strings)
lunch = breakfast.next_sibling.next_sibling
lunch_string = ''
indent = False
sstrings = list(lunch.stripped_strings)
for s in sstrings[0:-1]:
if s.endswith(':'):
if indent:
lunch_string += s + '\n' + 4 * ' '
else:
lunch_string += s + '\n' + 2 * ' '
indent = True
else:
lunch_string += s + '\n' + 2 * ' '
lunch_string += sstrings[-1]
cg = soup.find_all(has_cg)
cg_winner = cg[0].get_text()
cg_winner = ' '.join(cg_winner.split())
return [today_string, underscores, cg_winner, '', breakfast_string, lunch_string]
if __name__ == "__main__":
print '\n'.join(get_menu(URL))