6 import profanity_filter
9 import xml.etree.ElementTree as ET
11 class generic_news_rss_renderer(renderer.debuggable_abstaining_renderer):
12 def __init__(self, name_to_timeout_dict, feed_site, feed_uris, page_title):
13 super(generic_news_rss_renderer, self).__init__(name_to_timeout_dict, False)
15 self.feed_site = feed_site
16 self.feed_uris = feed_uris
17 self.page_title = page_title
18 self.news = grab_bag.grab_bag()
19 self.details = grab_bag.grab_bag()
20 self.filter = profanity_filter.profanity_filter()
22 def debug_prefix(self):
25 def get_headlines_page_prefix(self):
28 def get_details_page_prefix(self):
31 def should_use_https(self):
34 def should_profanity_filter(self):
37 def find_title(self, item):
38 return item.findtext('title')
40 def munge_title(self, title):
43 def find_description(self, item):
44 return item.findtext('description')
46 def munge_description(self, description):
47 description = re.sub('<[^>]+>', '', description)
50 def find_link(self, item):
51 return item.findtext('link')
53 def find_image(self, item):
54 return item.findtext('image')
56 def item_is_interesting_for_headlines(self, title, description, item):
59 def item_is_interesting_for_article(self, title, description, item):
62 def periodic_render(self, key):
63 if key == "Fetch News":
64 return self.fetch_news()
65 elif key == "Shuffle News":
66 return self.shuffle_news()
68 raise error('Unexpected operation')
70 def shuffle_news(self):
71 headlines = page_builder.page_builder()
72 headlines.set_layout(page_builder.page_builder.LAYOUT_FOUR_ITEMS)
73 headlines.set_title("%s" % self.page_title)
74 subset = self.news.subset(4)
76 self.debug_print("Not enough messages to choose from.")
79 headlines.add_item(msg)
80 f = file_writer.file_writer('%s_4_none.html' % (
81 self.get_headlines_page_prefix()))
82 headlines.render_html(f)
85 details = page_builder.page_builder()
86 details.set_layout(page_builder.page_builder.LAYOUT_ONE_ITEM)
87 details.set_title("%s" % self.page_title)
88 subset = self.details.subset(1)
90 self.debug_print("Not enough details to choose from.");
95 details.add_item(blurb)
96 g = file_writer.file_writer('%s_6_none.html' % (
97 self.get_details_page_prefix()))
98 details.render_html(g)
102 def fetch_news(self):
107 for uri in self.feed_uris:
108 if self.should_use_https():
109 self.debug_print("Fetching: https://%s%s" % (self.feed_site, uri))
110 self.conn = http.client.HTTPSConnection(self.feed_site)
112 self.debug_print("Fetching: http://%s%s" % (self.feed_site, uri))
113 self.conn = http.client.HTTPConnection(self.feed_site)
118 {"Accept-Charset": "utf-8"})
119 response = self.conn.getresponse()
120 if response.status != 200:
121 print(("%s: RSS fetch_news error, response: %d" % (self.page_title,
123 self.debug_print(response.read())
126 rss = ET.fromstring(response.read())
128 for item in channel.getchildren():
129 title = self.find_title(item)
130 if title is not None:
131 title = self.munge_title(title)
132 description = item.findtext('description')
133 if description is not None:
134 description = self.munge_description(description)
135 link = item.findtext('link')
136 image = item.findtext('image')
139 not self.item_is_interesting_for_headlines(title,
142 self.debug_print('Item "%s" is not interesting' % title)
145 if (self.should_profanity_filter() and
146 (self.filter.contains_bad_words(title) or
147 self.filter.contains_bad_words(description))):
148 self.debug_print('Found bad words in item "%s"' % title)
151 #print u"Title: %s\nDescription: %s\nLink: %s\nImage: %s\n" % (
152 # title, description, link, image)
154 blurb = """<DIV style="padding:8px;
156 -webkit-column-break-inside:avoid;">"""
157 if image is not None:
158 blurb += '<IMG SRC="%s" ALIGN=LEFT HEIGHT=115 style="padding:8px;">\n' % image
159 blurb += '<P><B>%s</B>' % title
161 if (description is not None and
162 self.item_is_interesting_for_article(title, description, item)):
165 longblurb += description
166 longblurb += "</DIV>"
167 longblurb = longblurb.replace("font-size:34pt",
169 self.details.add(longblurb.encode('utf-8', errors='ignore'))
172 self.news.add(blurb.encode('utf-8', errors='ignore'))
177 #x = generic_news_rss_renderer(
179 # "Shuffle News" : 1},
181 # [ "/rss/generic_news_topstories.rss",
182 # "/rss/money_latest.rss",
183 # "/rss/generic_news_tech.rss",
186 #if x.fetch_news() == 0:
187 # print "Error fetching news, no items fetched."