2 from bs4 import BeautifulSoup
7 class pollen_count_renderer(renderer.debuggable_abstaining_renderer):
8 def __init__(self, name_to_timeout_dict):
9 super(pollen_count_renderer, self).__init__(name_to_timeout_dict, False)
10 self.site = 'www.nwasthma.com'
11 self.uri = '/pollen/pollen-count/'
16 def debug_prefix(self):
20 conn = http.client.HTTPConnection(self.site)
26 response = conn.getresponse()
27 if response.status != 200:
28 print(('Connection to %s/%s failed, status %d' % (self.site,
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):
61 text = text + '<TD HEIGHT=80><DIV STYLE="width: %d; height: 80; overflow: visible; background-color: %s; font-size: 16pt">' % (width, color)
62 text = text + 'count=%d, %s %s</DIV>' % (count, desc, comment)
66 soup = BeautifulSoup(raw, "html.parser")
69 <H1>Pollen Count, Seattle</H1>
72 <TABLE BODER WIDTH=800>"""
73 date = "<CENTER><B>Unknown Date</B></CENTER>"
74 for x in soup.find_all('p'):
75 if x == None or x.string == None:
77 txt = x.string.encode('utf-8')
78 m = re.match("[0-9][0-9].[0-9][0-9].20[0-9][0-9]", txt)
80 date = "<CENTER><B>%s</B></CENTER>" % (txt)
81 y = x.find_next_sibling('p')
82 if y != None and y.string != None:
83 txt = y.string.encode('utf-8')
84 date = date + "<BR>%s<HR>" % txt
85 text = text + '<TR><TD COLSPAN=3 STYLE="font-size:16pt">%s</TD></TR>\n' % (
88 trees = soup.find('td', text=re.compile('[Tt]rees:'))
90 tc = trees.find_next_sibling('td')
91 tr = tc.find_next_sibling('td')
92 tcomment = tr.find_next_sibling('td')
93 text = self.append_crap(text, tc, tr, tcomment, "Trees", 650)
95 grasses = soup.find('td', text=re.compile('[Gg]rasses:'))
97 gc = grasses.find_next_sibling('td')
98 gr = gc.find_next_sibling('td')
99 gcomment = gr.find_next_sibling('td')
100 text = self.append_crap(text, gc, gr, gcomment, "Grasses", 35)
102 weeds = soup.find('td', text=re.compile('[Ww]eeds:'))
104 wc = weeds.find_next_sibling('td')
105 wr = wc.find_next_sibling('td')
106 wcomment = wr.find_next_sibling('td')
107 text = self.append_crap(text, wc, wr, wcomment, "Weeds", 25)
110 <TD COLSPAN=3 STYLE="font-size:16pt">
112 <B>Absent:</B> No symptoms.<BR>
113 <B>Low:</B> Only individuals extremely sensitive to these pollens will experience symptoms.<BR>
114 <B>Moderate:</B> Many individuals sensitive to these pollens will experience symptoms<BR>
115 <B>High:</B> Most individuals with any sensitivity to these pollens will experience symptoms.<BR>
116 <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.
123 def poll_pollen(self):
124 raw = self.fetch_html()
125 cooked = self.munge(raw)
126 f = file_writer.file_writer('pollen_4_360.html')
131 def periodic_render(self, key):
132 self.debug_print("executing action %s" % key)
134 return self.poll_pollen()
136 raise error("Unknown operaiton")
138 #test = pollen_count_renderer({"Test", 123})