Move the kiosk server to another machine; changes needed to make it work there.
[kiosk.git] / google_news_rss_renderer.py
1 #!/usr/bin/env python3
2
3 from bs4 import BeautifulSoup  # type: ignore
4 import re
5 from typing import Dict, List, Optional
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 descr is not None:
37             if source is not None:
38                 descr = descr + f" (source)"
39         else:
40             descr = ""
41         return descr
42
43     def munge_description_internal(self, descr: str) -> str:
44         if len(descr) > 450:
45             descr = descr[:450]
46             descr = re.sub(r"\<[^\>]*$", "", descr)
47             descr = descr + " [...]"
48         descr += "</A></LI></UL></OL></P>"
49         return descr
50
51     def munge_description(self, description: str) -> str:
52         soup = BeautifulSoup(description, features="lxml")
53         for a in soup.findAll("a"):
54             del a["href"]
55         descr = str(soup)
56         return self.munge_description_internal(descr)
57
58     def find_image(self, item: xml.etree.ElementTree.Element) -> Optional[str]:
59         return None
60
61     def should_use_https(self) -> bool:
62         return True
63
64     def item_is_interesting_for_headlines(
65         self, title: str, description: str, item: xml.etree.ElementTree.Element
66     ) -> bool:
67         return not self.is_item_older_than_n_days(item, 2)
68
69     def item_is_interesting_for_article(
70         self, title: str, description: str, item: xml.etree.ElementTree.Element
71     ) -> bool:
72         return not self.is_item_older_than_n_days(item, 2)
73
74
75 # Test
76 # x = google_news_rss_renderer(
77 #    {"Fetch News" : 1,
78 #     "Shuffle News" : 1},
79 #    "news.google.com",
80 #    [ "/rss?hl=en-US&gl=US&ceid=US:en" ],
81 #    "Test" )
82 # if x.fetch_news() == 0:
83 #    print("Error fetching news, no items fetched.")
84 # x.shuffle_news()
85 #
86 # 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!"
87 # d = x.munge_description_internal(descr)
88 # print(d)