Fix wakeword.
[kiosk.git] / pollen_renderer.py
1 import file_writer
2 from bs4 import BeautifulSoup
3 import renderer
4 import http.client
5 import re
6
7
8 class pollen_count_renderer(renderer.debuggable_abstaining_renderer):
9     def __init__(self, name_to_timeout_dict):
10         super(pollen_count_renderer, self).__init__(name_to_timeout_dict, False)
11         self.site = "www.nwasthma.com"
12         self.uri = "/pollen/pollen-count/"
13         self.trees = []
14         self.grasses = []
15         self.weeds = []
16
17     def debug_prefix(self):
18         return "pollen"
19
20     def fetch_html(self):
21         conn = http.client.HTTPConnection(self.site)
22         conn.request("GET", self.uri, None, {})
23         response = conn.getresponse()
24         if response.status != 200:
25             print(
26                 (
27                     "Connection to %s/%s failed, status %d"
28                     % (self.site, self.uri, response.status)
29                 )
30             )
31             return False
32         return response.read()
33
34     def append_crap(self, text, tc, tr, tcomment, kind, maximum):
35         desc = ""
36         color = "#00d000"
37         if tr != None and tr.string != None:
38             desc = tr.string.encode("utf-8")
39             if "edium" in desc:
40                 color = "#a0a000"
41             elif "igh" in desc:
42                 color = "#d00000"
43
44         count = 0
45         if tc != None and tc.string != None:
46             try:
47                 count = int(tc.string.encode("utf-8"))
48             except:
49                 count = 0
50         proportion = float(count) / float(maximum)
51         width = int(proportion * 600.0)
52
53         comment = ""
54         if tcomment != None and tcomment.string != None:
55             comment = "%s" % (tcomment.string.encode("utf-8"))
56
57         # Label:
58         text = text + '<TR><TD WIDTH=10%% STYLE="font-size: 22pt">%s:</TD>' % (kind)
59
60         # Bar graph with text in it (possibly overspilling):
61         text = (
62             text
63             + '<TD HEIGHT=80><DIV STYLE="width: %d; height: 80; overflow: visible; background-color: %s; font-size: 16pt">'
64             % (width, color)
65         )
66         text = text + "count=%d,&nbsp;%s&nbsp;%s</DIV>" % (count, desc, comment)
67         return text
68
69     def munge(self, raw):
70         soup = BeautifulSoup(raw, "html.parser")
71
72         text = """
73 <H1>Pollen Count, Seattle</H1>
74 <HR>
75 <CENTER>
76 <TABLE BODER WIDTH=800>"""
77         date = "<CENTER><B>Unknown Date</B></CENTER>"
78         for x in soup.find_all("p"):
79             if x == None or x.string == None:
80                 continue
81             txt = x.string.encode("utf-8")
82             m = re.match("[0-9][0-9].[0-9][0-9].20[0-9][0-9]", txt)
83             if m != None:
84                 date = "<CENTER><B>%s</B></CENTER>" % (txt)
85                 y = x.find_next_sibling("p")
86                 if y != None and y.string != None:
87                     txt = y.string.encode("utf-8")
88                     date = date + "<BR>%s<HR>" % txt
89         text = text + '<TR><TD COLSPAN=3 STYLE="font-size:16pt">%s</TD></TR>\n' % (date)
90
91         trees = soup.find("td", text=re.compile("[Tt]rees:"))
92         if trees != None:
93             tc = trees.find_next_sibling("td")
94             tr = tc.find_next_sibling("td")
95             tcomment = tr.find_next_sibling("td")
96             text = self.append_crap(text, tc, tr, tcomment, "Trees", 650)
97
98         grasses = soup.find("td", text=re.compile("[Gg]rasses:"))
99         if grasses != None:
100             gc = grasses.find_next_sibling("td")
101             gr = gc.find_next_sibling("td")
102             gcomment = gr.find_next_sibling("td")
103             text = self.append_crap(text, gc, gr, gcomment, "Grasses", 35)
104
105         weeds = soup.find("td", text=re.compile("[Ww]eeds:"))
106         if weeds != None:
107             wc = weeds.find_next_sibling("td")
108             wr = wc.find_next_sibling("td")
109             wcomment = wr.find_next_sibling("td")
110             text = self.append_crap(text, wc, wr, wcomment, "Weeds", 25)
111         text = (
112             text
113             + """
114 <TR>
115   <TD COLSPAN=3 STYLE="font-size:16pt">
116 <HR>
117 <B>Absent:</B> No symptoms.<BR>
118 <B>Low:</B> Only individuals extremely sensitive to these pollens will experience symptoms.<BR>
119 <B>Moderate:</B> Many individuals sensitive to these pollens will experience symptoms<BR>
120 <B>High:</B> Most individuals with any sensitivity to these pollens will experience symptoms.<BR>
121 <B>Very High:</B> Almost all individuals with any sensitivity at all to these pollens will experience symptoms. Extremely sensitive people could have severe problems.
122   </TD>
123 </TR>
124 </TABLE>
125 </CENTER>"""
126         )
127         return text
128
129     def poll_pollen(self):
130         raw = self.fetch_html()
131         cooked = self.munge(raw)
132         f = file_writer.file_writer("pollen_4_360.html")
133         f.write(cooked)
134         f.close()
135         return True
136
137     def periodic_render(self, key):
138         self.debug_print("executing action %s" % key)
139         if key == "Poll":
140             return self.poll_pollen()
141         else:
142             raise error("Unknown operaiton")
143
144
145 # test = pollen_count_renderer({"Test", 123})