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