Adding type annotations and fixing up formatting.
[kiosk.git] / google_news_rss_renderer.py
1 #!/usr/bin/env python3
2
3 from bs4 import BeautifulSoup
4 import re
5 from typing import Dict, List
6 import xml
7
8 import generic_news_rss_renderer
9
10
11 class google_news_rss_renderer(generic_news_rss_renderer.generic_news_rss_renderer):
12     def __init__(
13         self,
14         name_to_timeout_dict: Dict[str, int],
15         feed_site: str,
16         feed_uris: List[str],
17         page_title: str,
18     ) -> None:
19         super(google_news_rss_renderer, self).__init__(
20             name_to_timeout_dict, feed_site, feed_uris, page_title
21         )
22         self.debug = True
23
24     def debug_prefix(self) -> str:
25         return "google-news"
26
27     def get_headlines_page_prefix(self) -> str:
28         return "google-news"
29
30     def get_details_page_prefix(self) -> str:
31         return "google-news-details"
32
33     def find_description(self, item: xml.etree.ElementTree.Element) -> str:
34         descr = item.findtext("description")
35         source = item.findtext("source")
36         if source is not None:
37             descr = descr + " (%s)" % source
38         return descr
39
40     def munge_description_internal(self, descr: str) -> str:
41         if len(descr) > 450:
42             descr = descr[:450]
43             descr = re.sub(r"\<[^\>]*$", "", descr)
44             descr = descr + " [...]"
45         descr += "</A></LI></UL></OL></P>"
46         return descr
47
48     def munge_description(self, description: str) -> str:
49         soup = BeautifulSoup(description, features="lxml")
50         for a in soup.findAll("a"):
51             del a["href"]
52         descr = str(soup)
53         return self.munge_description_internal(descr)
54
55     def find_image(self, item: xml.etree.ElementTree.Element) -> str:
56         return None
57
58     def should_use_https(self) -> bool:
59         return True
60
61     def item_is_interesting_for_headlines(
62         self, title: str, description: str, item: xml.etree.ElementTree.Element
63     ) -> bool:
64         return not self.is_item_older_than_n_days(item, 2)
65
66     def item_is_interesting_for_article(
67         self, title: str, description: str, item: xml.etree.ElementTree.Element
68     ) -> bool:
69         return not self.is_item_older_than_n_days(item, 2)
70
71
72 # Test
73 # x = google_news_rss_renderer(
74 #    {"Fetch News" : 1,
75 #     "Shuffle News" : 1},
76 #    "news.google.com",
77 #    [ "/rss?hl=en-US&gl=US&ceid=US:en" ],
78 #    "Test" )
79 # if x.fetch_news() == 0:
80 #    print("Error fetching news, no items fetched.")
81 # x.shuffle_news()
82 #
83 # 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!"
84 # d = x.munge_description_internal(descr)
85 # print(d)