+#!/usr/bin/env python3
+
+from abc import abstractmethod
+import datetime
+from dateutil.parser import parse
+import http.client
+import random
+import re
+import sys
+import traceback
+from typing import Dict, List, Optional, Union
+import xml.etree.ElementTree as ET
+
import file_writer
import grab_bag
import renderer
-import http.client
import page_builder
import profanity_filter
-import random
-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):
+ def __init__(
+ self,
+ name_to_timeout_dict: Dict[str, int],
+ feed_site: str,
+ feed_uris: List[str],
+ page_title: str,
+ ):
super(generic_news_rss_renderer, self).__init__(name_to_timeout_dict, False)
- self.debug = 1
+ self.debug = True
self.feed_site = feed_site
self.feed_uris = feed_uris
self.page_title = page_title
self.news = grab_bag.grab_bag()
self.details = grab_bag.grab_bag()
- self.filter = profanity_filter.profanity_filter()
+ self.filter = profanity_filter.ProfanityFilter()
- def debug_prefix(self):
+ @abstractmethod
+ def debug_prefix(self) -> str:
pass
- def get_headlines_page_prefix(self):
+ @abstractmethod
+ def get_headlines_page_prefix(self) -> str:
pass
- def get_details_page_prefix(self):
+ @abstractmethod
+ def get_details_page_prefix(self) -> str:
pass
- def should_use_https(self):
+ def get_headlines_page_priority(self) -> str:
+ return "4"
+
+ def get_details_page_priority(self) -> str:
+ return "6"
+
+ @abstractmethod
+ def should_use_https(self) -> bool:
pass
- def should_profanity_filter(self):
+ def should_profanity_filter(self) -> bool:
return False
- def find_title(self, item):
- return item.findtext('title')
+ def find_title(self, item: ET.Element) -> Optional[str]:
+ return item.findtext("title")
- def munge_title(self, title):
+ def munge_title(self, title: str, item: ET.Element) -> str:
return title
- def find_description(self, item):
- return item.findtext('description')
+ def find_description(self, item: ET.Element) -> Optional[str]:
+ return item.findtext("description")
- def munge_description(self, description):
- description = re.sub('<[^>]+>', '', description)
+ def munge_description(
+ self,
+ description: str,
+ item: ET.Element
+ ) -> str:
+ description = re.sub("<[^>]+>", "", description)
return description
- def find_link(self, item):
- return item.findtext('link')
+ def find_link(self, item: ET.Element) -> Optional[str]:
+ return item.findtext("link")
- def find_image(self, item):
- return item.findtext('image')
+ def munge_link(self, link: str) -> str:
+ return link
- def item_is_interesting_for_headlines(self, title, description, item):
- pass
+ def find_image(self, item: ET.Element) -> Optional[str]:
+ return item.findtext("image")
- def item_is_interesting_for_article(self, title, description, item):
- pass
+ def munge_image(self, image: str) -> str:
+ return image
+
+ def find_pubdate(self, item: ET.Element) -> Optional[str]:
+ return item.findtext("pubDate")
+
+ def munge_pubdate(self, pubdate: str) -> str:
+ return pubdate
+
+ def item_is_interesting_for_headlines(
+ self, title: str, description: str, item: ET.Element
+ ) -> bool:
+ return True
+
+ def do_headlines(self) -> bool:
+ return True
- def periodic_render(self, key):
+ def do_details(self) -> bool:
+ return True
+
+ def is_item_older_than_n_days(self, item: ET.Element, n: int) -> bool:
+ pubdate = self.find_pubdate(item)
+ if pubdate is None:
+ return False
+ pubdatetime = parse(pubdate)
+ tzinfo = pubdatetime.tzinfo
+ now = datetime.datetime.now(tzinfo)
+ delta = (now - pubdatetime).total_seconds() / (60 * 60 * 24)
+ return delta > n
+
+ def item_is_interesting_for_article(
+ self, title: str, description: str, item: ET.Element
+ ) -> bool:
+ return True
+
+ def periodic_render(self, key: str) -> bool:
if key == "Fetch News":
return self.fetch_news()
elif key == "Shuffle News":
return self.shuffle_news()
else:
- raise error('Unexpected operation')
-
- def shuffle_news(self):
- headlines = page_builder.page_builder()
- headlines.set_layout(page_builder.page_builder.LAYOUT_FOUR_ITEMS)
- headlines.set_title("%s" % self.page_title)
- subset = self.news.subset(4)
- if subset is None:
- self.debug_print("Not enough messages to choose from.")
- return False
- for msg in subset:
- headlines.add_item(msg)
- f = file_writer.file_writer('%s_4_none.html' % (
- self.get_headlines_page_prefix()))
- headlines.render_html(f)
- f.close()
-
- details = page_builder.page_builder()
- details.set_layout(page_builder.page_builder.LAYOUT_ONE_ITEM)
- 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.");
- return False
- for msg in subset:
- blurb = msg
- blurb += "</TD>\n"
- details.add_item(blurb)
- g = file_writer.file_writer('%s_6_none.html' % (
- self.get_details_page_prefix()))
- details.render_html(g)
- g.close()
+ raise Exception
+
+ def shuffle_news(self) -> bool:
+ if self.do_headlines():
+ headlines = page_builder.page_builder()
+ headlines.set_layout(page_builder.page_builder.LAYOUT_FOUR_ITEMS)
+ headlines.set_title("%s" % self.page_title)
+ subset = self.news.subset(4)
+ if subset is None:
+ self.debug_print("Not enough messages to choose from.")
+ return False
+ for msg in subset:
+ headlines.add_item(msg)
+ headlines.set_custom_html(
+ """
+ <STYLE>
+ a:link {
+ color: black;
+ text-decoration: none;
+ font-weight: bold;
+ }
+ a:visited {
+ color: black;
+ text-decoration: none;
+ font-weight: bold;
+ }
+ a:active {
+ color: black;
+ text-decoration: none;
+ font-weight: bold;
+ }
+ </STYLE>"""
+ )
+ _ = f"{self.get_headlines_page_prefix()}_{self.get_headlines_page_priority()}_25900.html"
+ with file_writer.file_writer(_) as f:
+ headlines.render_html(f)
+
+ if self.do_details():
+ details = page_builder.page_builder()
+ details.set_layout(page_builder.page_builder.LAYOUT_ONE_ITEM)
+ details.set_custom_html(
+ """
+ <STYLE>
+ a:link {
+ color: black;
+ text-decoration: none;
+ font-weight: bold;
+ }
+ a:visited {
+ color: black;
+ text-decoration: none;
+ font-weight: bold;
+ }
+ a:active {
+ color: black;
+ text-decoration: none;
+ font-weight: bold;
+ }
+ </STYLE>"""
+ )
+ details.set_title(f"{self.page_title}")
+ subset = self.details.subset(1)
+ if subset is None:
+ self.debug_print("Not enough details to choose from.")
+ return False
+ for msg in subset:
+ blurb = msg
+ blurb += "</TD>"
+ details.add_item(blurb)
+ _ = f"{self.get_details_page_prefix()}_{self.get_details_page_priority()}_86400.html"
+ with file_writer.file_writer(_) as g:
+ details.render_html(g)
return True
- def fetch_news(self):
+ def fetch_news(self) -> bool:
count = 0
self.news.clear()
self.details.clear()
+ self.conn: Optional[Union[http.client.HTTPConnection,
+ http.client.HTTPSConnection]] = None
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=10)
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=10)
+ assert(self.conn is not None)
self.conn.request(
"GET",
uri,
None,
- {"Accept-Charset": "utf-8"})
- response = self.conn.getresponse()
+ {
+ "Accept": "*/*",
+# "Cache-control": "max-age=50",
+# "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 Exception as e:
+ traceback.print_exc(file=sys.stdout)
+ print(
+ f"Exception in generic RSS renderer HTTP connection fetching {self.feed_site}{uri}"
+ )
+ return False
+
if response.status != 200:
- print(("%s: RSS fetch_news error, response: %d" % (self.page_title,
- response.status)))
- self.debug_print(response.read())
+ print(
+ f"{self.page_title}: RSS fetch_news error, response: {response.status}"
+ )
+ self.debug_print(str(response.read()))
return False
rss = ET.fromstring(response.read())
channel = rss[0]
+ title_filter = set()
for item in channel.getchildren():
title = self.find_title(item)
+ description = item.findtext("description")
if title is not None:
- title = self.munge_title(title)
- description = item.findtext('description')
+ title = self.munge_title(title, item)
if description is not None:
- description = self.munge_description(description)
- link = item.findtext('link')
- image = item.findtext('image')
-
- if (title is None or
- not self.item_is_interesting_for_headlines(title,
- description,
- item)):
- self.debug_print('Item "%s" is not interesting' % title)
+ description = self.munge_description(description, item)
+ else:
+ description = ""
+ image = self.find_image(item)
+ if image is not None:
+ image = self.munge_image(image)
+ 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
+ ):
+ self.debug_print(f'Item "{title}" is not interesting')
continue
- 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)
+ if self.should_profanity_filter() and (
+ self.filter.contains_bad_word(title)
+ or self.filter.contains_bad_word(description)
+ ):
+ self.debug_print(f'Found bad words in item "{title}"')
continue
- #print u"Title: %s\nDescription: %s\nLink: %s\nImage: %s\n" % (
- # title, description, link, image)
+ if title in title_filter:
+ self.debug_print(f'Already saw title {title}, skipping.')
+ title_filter.add(title)
blurb = """<DIV style="padding:8px;
- font-size:34pt;
- -webkit-column-break-inside:avoid;">"""
+ font-size:34pt;
+ -webkit-column-break-inside:avoid;">"""
if image is not None:
- blurb += '<IMG SRC="%s" ALIGN=LEFT HEIGHT=115 style="padding:8px;">\n' % image
- blurb += '<P><B>%s</B>' % title
+ blurb += f'<IMG SRC="{image}" ALIGN=LEFT HEIGHT=115 '
+ blurb += 'style="padding:8px;">'
+
+ if link is None:
+ blurb += f"<P><B>{title}</B>"
+ else:
+ blurb += f'<P><B><A HREF="{link}">{title}</A></B>'
+
+ pubdate = self.find_pubdate(item)
+ if pubdate is not None:
+ pubdate = self.munge_pubdate(pubdate)
+ ts = parse(pubdate)
+ blurb += f' <FONT COLOR=#cccccc>{ts.strftime("%b %d")}</FONT>'
- if (description is not None and
- self.item_is_interesting_for_article(title, description, item)):
+ if self.item_is_interesting_for_article(title, description, item):
longblurb = blurb
longblurb += "<BR>"
longblurb += description
longblurb += "</DIV>"
- longblurb = longblurb.replace("font-size:34pt",
- "font-size:44pt")
- self.details.add(longblurb.encode('utf-8', errors='ignore'))
-
+ longblurb = longblurb.replace("font-size:34pt", "font-size:44pt")
+ self.details.add(longblurb)
blurb += "</DIV>"
- self.news.add(blurb.encode('utf-8', errors='ignore'))
+ self.news.add(blurb)
count += 1
return count > 0
-
-# Test
-#x = generic_news_rss_renderer(
-# {"Fetch News" : 1,
-# "Shuffle News" : 1},
-# "rss.cnn.com",
-# [ "/rss/generic_news_topstories.rss",
-# "/rss/money_latest.rss",
-# "/rss/generic_news_tech.rss",
-# ],
-# "Test" )
-#if x.fetch_news() == 0:
-# print "Error fetching news, no items fetched."
-#x.shuffle_news()