import re
import xml.etree.ElementTree as ET
+
class generic_news_rss_renderer(renderer.debuggable_abstaining_renderer):
def __init__(self, name_to_timeout_dict, feed_site, feed_uris, page_title):
- super(generic_news_rss_renderer, self).__init__(name_to_timeout_dict,
- False)
+ super(generic_news_rss_renderer, self).__init__(name_to_timeout_dict, False)
self.debug = 1
self.feed_site = feed_site
self.feed_uris = feed_uris
def get_details_page_prefix(self):
pass
+ def get_headlines_page_priority(self):
+ return "4"
+
+ def get_details_page_priority(self):
+ return "6"
+
def should_use_https(self):
pass
return False
def find_title(self, item):
- return item.findtext('title')
+ return item.findtext("title")
def munge_title(self, title):
return title
def find_description(self, item):
- return item.findtext('description')
+ return item.findtext("description")
def munge_description(self, description):
- description = re.sub('<[^>]+>', '', description)
+ description = re.sub("<[^>]+>", "", description)
return description
def find_link(self, item):
- return item.findtext('link')
+ return item.findtext("link")
def munge_link(self, link):
return link
def find_image(self, item):
- return item.findtext('image')
+ return item.findtext("image")
def munge_image(self, image):
return image
+ def find_pubdate(self, item):
+ return item.findtext("pubDate")
+
+ def munge_pubdate(self, pubdate):
+ return pubdate
+
def item_is_interesting_for_headlines(self, title, description, item):
return True
def is_item_older_than_n_days(self, item, n):
- pubdate = item.findtext('pubDate')
+ pubdate = self.find_pubdate(item)
if pubdate is not None:
pubdate = parse(pubdate)
tzinfo = pubdate.tzinfo
now = datetime.datetime.now(tzinfo)
delta = (now - pubdate).total_seconds() / (60 * 60 * 24)
- if (delta > n):
+ if delta > n:
return True
return False
elif key == "Shuffle News":
return self.shuffle_news()
else:
- raise error('Unexpected operation')
+ raise error("Unexpected operation")
def shuffle_news(self):
headlines = page_builder.page_builder()
return False
for msg in subset:
headlines.add_item(msg)
- headlines.set_custom_html("""
+ headlines.set_custom_html(
+ """
<STYLE>
a:link {
- color: maroon;
+ color: black;
text-decoration: none;
font-weight: bold;
}
a:visited {
- color: maroon;
+ color: black;
text-decoration: none;
font-weight: bold;
}
a:active {
- color: maroon;
+ color: black;
text-decoration: none;
font-weight: bold;
}
-</STYLE>""")
- f = file_writer.file_writer('%s_4_none.html' % (
- self.get_headlines_page_prefix()))
+</STYLE>"""
+ )
+ f = file_writer.file_writer(
+ "%s_%s_25900.html"
+ % (self.get_headlines_page_prefix(), self.get_headlines_page_priority())
+ )
headlines.render_html(f)
f.close()
details = page_builder.page_builder()
details.set_layout(page_builder.page_builder.LAYOUT_ONE_ITEM)
- details.set_custom_html("""
+ details.set_custom_html(
+ """
<STYLE>
a:link {
- color: maroon;
+ color: black;
text-decoration: none;
font-weight: bold;
}
a:visited {
- color: maroon;
+ color: black;
text-decoration: none;
font-weight: bold;
}
a:active {
- color: maroon;
+ color: black;
text-decoration: none;
font-weight: bold;
}
-</STYLE>""")
+</STYLE>"""
+ )
details.set_title("%s" % self.page_title)
subset = self.details.subset(1)
if subset is None:
- self.debug_print("Not enough details to choose from.");
+ self.debug_print("Not enough details to choose from.")
return False
for msg in subset:
blurb = msg
- blurb += u'</TD>'
+ blurb += u"</TD>"
details.add_item(blurb)
- g = file_writer.file_writer('%s_6_none.html' % (
- self.get_details_page_prefix()))
+ g = file_writer.file_writer(
+ "%s_%s_86400.html"
+ % (self.get_details_page_prefix(), self.get_details_page_priority())
+ )
details.render_html(g)
g.close()
return True
for uri in self.feed_uris:
if self.should_use_https():
self.debug_print("Fetching: https://%s%s" % (self.feed_site, uri))
- self.conn = http.client.HTTPSConnection(self.feed_site)
+ self.conn = http.client.HTTPSConnection(self.feed_site, timeout=20)
else:
self.debug_print("Fetching: http://%s%s" % (self.feed_site, uri))
- self.conn = http.client.HTTPConnection(self.feed_site)
+ self.conn = http.client.HTTPConnection(self.feed_site, timeout=20)
self.conn.request(
"GET",
uri,
None,
- {"Accept-Charset": "utf-8"})
- response = self.conn.getresponse()
+ {
+ "Accept": "*/*",
+ "Cache-control": "max-age=59",
+ "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
+ },
+ )
+ try:
+ response = self.conn.getresponse()
+ except:
+ print("Exception in generic RSS renderer HTTP connection")
+ return False
+
if response.status != 200:
- print(("%s: RSS fetch_news error, response: %d" % (self.page_title,
- response.status)))
+ print(
+ (
+ "%s: RSS fetch_news error, response: %d"
+ % (self.page_title, response.status)
+ )
+ )
self.debug_print(response.read())
return False
title = self.find_title(item)
if title is not None:
title = self.munge_title(title)
- description = item.findtext('description')
+ description = item.findtext("description")
if description is not None:
description = self.munge_description(description)
image = self.find_image(item)
if image is not None:
image = self.munge_image(image)
- link = item.findtext('link')
+ link = item.findtext("link")
if link is not None:
link = self.munge_link(link)
- if (title is None or
- not self.item_is_interesting_for_headlines(title,
- description,
- item)):
+ if title is None or not self.item_is_interesting_for_headlines(
+ title, description, item
+ ):
self.debug_print('Item "%s" is not interesting' % title)
continue
- if (self.should_profanity_filter() and
- (self.filter.contains_bad_words(title) or
- self.filter.contains_bad_words(description))):
+ if self.should_profanity_filter() and (
+ self.filter.contains_bad_words(title)
+ or self.filter.contains_bad_words(description)
+ ):
self.debug_print('Found bad words in item "%s"' % title)
continue
- #print u"Title: %s\nDescription: %s\nLink: %s\nImage: %s\n" % (
- # title, description, link, image)
-
blurb = u"""<DIV style="padding:8px;
font-size:34pt;
-webkit-column-break-inside:avoid;">"""
blurb += u'style="padding:8px;">'
if link is None:
- blurb += u'<P><B>%s</B>' % title
+ blurb += u"<P><B>%s</B>" % title
else:
blurb += u'<P><B><A HREF="%s">%s</A></B>' % (link, title)
- if (description is not None and
- self.item_is_interesting_for_article(title,
- description,
- item)):
+ pubdate = self.find_pubdate(item)
+ if pubdate is not None:
+ pubdate = self.munge_pubdate(pubdate)
+ ts = parse(pubdate)
+ blurb += u" <FONT COLOR=#cccccc>%s</FONT>" % (
+ ts.strftime("%b %d")
+ )
+
+ if description is not None and self.item_is_interesting_for_article(
+ title, description, item
+ ):
longblurb = blurb
+
longblurb += u"<BR>"
longblurb += description
longblurb += u"</DIV>"
- longblurb = longblurb.replace("font-size:34pt",
- "font-size:44pt")
+ longblurb = longblurb.replace("font-size:34pt", "font-size:44pt")
self.details.add(longblurb)
blurb += u"</DIV>"