2 from bs4 import BeautifulSoup
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/"
17 def debug_prefix(self):
21 conn = http.client.HTTPConnection(self.site)
22 conn.request("GET", self.uri, None, {})
23 response = conn.getresponse()
24 if response.status != 200:
27 "Connection to %s/%s failed, status %d"
28 % (self.site, self.uri, response.status)
32 return response.read()
34 def append_crap(self, text, tc, tr, tcomment, kind, maximum):
37 if tr != None and tr.string != None:
38 desc = tr.string.encode("utf-8")
45 if tc != None and tc.string != None:
47 count = int(tc.string.encode("utf-8"))
50 proportion = float(count) / float(maximum)
51 width = int(proportion * 600.0)
54 if tcomment != None and tcomment.string != None:
55 comment = "%s" % (tcomment.string.encode("utf-8"))
58 text = text + '<TR><TD WIDTH=10%% STYLE="font-size: 22pt">%s:</TD>' % (kind)
60 # Bar graph with text in it (possibly overspilling):
63 + '<TD HEIGHT=80><DIV STYLE="width: %d; height: 80; overflow: visible; background-color: %s; font-size: 16pt">'
66 text = text + "count=%d, %s %s</DIV>" % (count, desc, comment)
70 soup = BeautifulSoup(raw, "html.parser")
73 <H1>Pollen Count, Seattle</H1>
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:
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)
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)
91 trees = soup.find("td", text=re.compile("[Tt]rees:"))
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)
98 grasses = soup.find("td", text=re.compile("[Gg]rasses:"))
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)
105 weeds = soup.find("td", text=re.compile("[Ww]eeds:"))
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)
115 <TD COLSPAN=3 STYLE="font-size:16pt">
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.
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")
137 def periodic_render(self, key):
138 self.debug_print("executing action %s" % key)
140 return self.poll_pollen()
142 raise error("Unknown operaiton")
145 # test = pollen_count_renderer({"Test", 123})