Remove myq
[kiosk.git] / google_news_rss_renderer.py
index 334f10d96345fd63165d9d8bf1a4a39809bdc5ba..3acdbb9fa0f7c61fedb7444b2fa1a4ae8320525e 100644 (file)
@@ -1,57 +1,87 @@
-from bs4 import BeautifulSoup
-import generic_news_rss_renderer
+#!/usr/bin/env python3
+
+import logging
 import re
+from typing import Dict, List, Optional
+import xml
+import xml.etree.ElementTree as ET
+
+from bs4 import BeautifulSoup  # type: ignore
+
+import generic_news_rss_renderer
+
+logger = logging.getLogger(__name__)
+
 
 class google_news_rss_renderer(generic_news_rss_renderer.generic_news_rss_renderer):
-    def __init__(self, name_to_timeout_dict, feed_site, feed_uris, page_title):
-        super(google_news_rss_renderer, self).__init__(
-            name_to_timeout_dict,
-            feed_site,
-            feed_uris,
-            page_title)
-        self.debug = 1
-
-    def debug_prefix(self):
-        return "google-news"
+    def __init__(
+        self,
+        name_to_timeout_dict: Dict[str, int],
+        feed_site: str,
+        feed_uris: List[str],
+        page_title: str,
+    ) -> None:
+        super().__init__(name_to_timeout_dict, feed_site, feed_uris, page_title)
 
-    def get_headlines_page_prefix(self):
+    def get_headlines_page_prefix(self) -> str:
         return "google-news"
 
-    def get_details_page_prefix(self):
+    def get_details_page_prefix(self) -> str:
         return "google-news-details"
 
-    def find_description(self, item):
-        descr = item.findtext('description')
-        source = item.findtext('source')
-        if source is not None:
-            descr = descr + " (%s)" % source
+    def find_description(self, item: xml.etree.ElementTree.Element) -> str:
+        descr = item.findtext("description")
+        if descr is not None:
+            source = item.findtext("source")
+            if source is not None:
+                descr = descr + f" ({source})"
+        else:
+            descr = ""
         return descr
 
-    def munge_description(self, description):
-        soup = BeautifulSoup(description)
-        for a in soup.findAll('a'):
-            del a['href']
-        return str(soup)
+    def munge_description_internal(self, descr: str, item: ET.Element) -> str:
+        if len(descr) > 450:
+            descr = descr[:450]
+            descr = re.sub(r"\<[^\>]*$", "", descr)
+            descr = descr + " [...]"
+        descr += "</A></LI></UL></OL></P>"
+        return descr
 
-    def find_image(self, item):
+    def munge_description(self, description: str, item: ET.Element) -> str:
+        soup = BeautifulSoup(description, features="lxml")
+        for a in soup.findAll("a"):
+            del a["href"]
+        descr = str(soup)
+        return self.munge_description_internal(descr, item)
+
+    def find_image(self, item: xml.etree.ElementTree.Element) -> Optional[str]:
         return None
 
-    def should_use_https(self):
+    def should_use_https(self) -> bool:
         return True
 
-    def item_is_interesting_for_headlines(self, title, description, item):
+    def item_is_interesting_for_headlines(
+        self, title: str, description: str, item: xml.etree.ElementTree.Element
+    ) -> bool:
         return not self.is_item_older_than_n_days(item, 2)
 
-    def item_is_interesting_for_article(self, title, description, item):
+    def item_is_interesting_for_article(
+        self, title: str, description: str, item: xml.etree.ElementTree.Element
+    ) -> bool:
         return not self.is_item_older_than_n_days(item, 2)
 
+
 # Test
-#x = google_news_rss_renderer(
+# x = google_news_rss_renderer(
 #    {"Fetch News" : 1,
 #     "Shuffle News" : 1},
 #    "news.google.com",
 #    [ "/rss?hl=en-US&gl=US&ceid=US:en" ],
 #    "Test" )
-#if x.fetch_news() == 0:
+# if x.fetch_news() == 0:
 #    print("Error fetching news, no items fetched.")
-#x.shuffle_news()
+# x.shuffle_news()
+#
+# descr = "this is a lot of really long text about nothign in particular.  It's pretty interesting, don't you think?  I hope that the munge description method works by both truncating it and remembering to close any open <LI>items as well as making sure not to truncate in the middle of a <A HREF=\"whatever\" these are a bunch of useless arguments to the A tag that make it really long so that the truncate will happen in the middle of it.  I'm getting kind of tired of typing shit so I'm going to revert to copy pasta now.  Sorry if you were getting into this story.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.  The quick brown fox jumps over the lazy dog.</A></LI> Out!"
+# d = x.munge_description_internal(descr)
+# print(d)