import http.client
import random
import re
-from typing import Dict, List
+from typing import Dict, List, Optional, Union
import xml.etree.ElementTree as ET
import file_writer
def should_profanity_filter(self) -> bool:
return False
- def find_title(self, item: ET.Element) -> str:
+ def find_title(self, item: ET.Element) -> Optional[str]:
return item.findtext("title")
def munge_title(self, title: str) -> str:
return title
- def find_description(self, item: ET.Element) -> str:
+ def find_description(self, item: ET.Element) -> Optional[str]:
return item.findtext("description")
def munge_description(self, description: str) -> str:
description = re.sub("<[^>]+>", "", description)
return description
- def find_link(self, item: ET.Element) -> str:
+ def find_link(self, item: ET.Element) -> Optional[str]:
return item.findtext("link")
def munge_link(self, link: str) -> str:
return link
- def find_image(self, item: ET.Element) -> str:
+ def find_image(self, item: ET.Element) -> Optional[str]:
return item.findtext("image")
def munge_image(self, image: str) -> str:
return image
- def find_pubdate(self, item: ET.Element) -> str:
+ def find_pubdate(self, item: ET.Element) -> Optional[str]:
return item.findtext("pubDate")
def munge_pubdate(self, pubdate: str) -> str:
pubdate = self.find_pubdate(item)
if pubdate is None:
return False
- pubdate = parse(pubdate)
- tzinfo = pubdate.tzinfo
+ pubdatetime = parse(pubdate)
+ tzinfo = pubdatetime.tzinfo
now = datetime.datetime.now(tzinfo)
- delta = (now - pubdate).total_seconds() / (60 * 60 * 24)
+ delta = (now - pubdatetime).total_seconds() / (60 * 60 * 24)
return delta > n
def item_is_interesting_for_article(
elif key == "Shuffle News":
return self.shuffle_news()
else:
- raise error("Unexpected operation")
+ raise Exception
def shuffle_news(self) -> bool:
headlines = page_builder.page_builder()
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():
else:
self.debug_print("Fetching: http://%s%s" % (self.feed_site, uri))
self.conn = http.client.HTTPConnection(self.feed_site, timeout=20)
+ assert(self.conn is not None)
self.conn.request(
"GET",
uri,
print(
f"{self.page_title}: RSS fetch_news error, response: {response.status}"
)
- self.debug_print(response.read())
+ self.debug_print(str(response.read()))
return False
rss = ET.fromstring(response.read())
description = item.findtext("description")
if description is not None:
description = self.munge_description(description)
+ else:
+ description = ""
image = self.find_image(item)
if image is not None:
image = self.munge_image(image)