Update .gitignore
[kiosk.git] / bellevue_reporter_rss_renderer.py
1 #!/usr/bin/env python3
2
3 import re
4 from typing import List, Dict
5 import xml
6
7 import generic_news_rss_renderer as gnrss
8
9
10 class bellevue_reporter_rss_renderer(gnrss.generic_news_rss_renderer):
11     """Read the Bellevue Reporter's RSS feed."""
12
13     def __init__(
14         self,
15         name_to_timeout_dict: Dict[str, int],
16         feed_site: str,
17         feed_uris: List[str],
18         page_title: str,
19     ):
20         super(bellevue_reporter_rss_renderer, self).__init__(
21             name_to_timeout_dict, feed_site, feed_uris, page_title
22         )
23         self.debug = True
24
25     def debug_prefix(self) -> str:
26         return "bellevue_reporter(%s)" % (self.page_title)
27
28     def get_headlines_page_prefix(self) -> str:
29         return "bellevue-reporter"
30
31     def get_details_page_prefix(self) -> str:
32         return "bellevue-reporter-details"
33
34     def should_use_https(self) -> bool:
35         return True
36
37     def munge_description(self, description: str) -> str:
38         description = re.sub("<[^>]+>", "", description)
39         description = re.sub(
40             "Bellevue\s+Reporter\s+Bellevue\s+Reporter", "", description
41         )
42         description = re.sub("\s*\-\s*Your local homepage\.\s*", "", description)
43         description = re.sub("[Ww]ire [Ss]ervice", "", description)
44         return description
45
46     @staticmethod
47     def looks_like_football(title: str, description: str) -> bool:
48         return (
49             title.find("NFL") != -1
50             or re.search("[Ll]ive [Ss]tream", title) is not None
51             or re.search("[Ll]ive[Ss]tream", title) is not None
52             or re.search("[Ll]ive [Ss]tream", description) is not None
53         )
54
55     @staticmethod
56     def looks_like_review(title: str, description: str) -> bool:
57         return "review" in title or "Review" in title
58
59     def item_is_interesting_for_headlines(
60         self, title: str, description: str, item: xml.etree.ElementTree.Element
61     ) -> bool:
62         if self.is_item_older_than_n_days(item, 10):
63             self.debug_print("%s: is too old!" % title)
64             return False
65         if bellevue_reporter_rss_renderer.looks_like_football(title, description):
66             self.debug_print("%s: looks like it's about football." % title)
67             return False
68         if bellevue_reporter_rss_renderer.looks_like_review(title, description):
69             self.debug_print("%s: looks like bullshit." % title)
70             return False
71         return True
72
73     def item_is_interesting_for_article(
74         self, title: str, description: str, item: xml.etree.ElementTree.Element
75     ) -> bool:
76         if self.is_item_older_than_n_days(item, 10):
77             self.debug_print("%s: is too old!" % title)
78             return False
79         if bellevue_reporter_rss_renderer.looks_like_football(title, description):
80             self.debug_print("%s: looks like it's about football." % title)
81             return False
82         if bellevue_reporter_rss_renderer.looks_like_review(title, description):
83             self.debug_print("%s: looks like bullshit." % title)
84             return False
85         return True
86
87
88 # Test
89 # x = bellevue_reporter_rss_renderer(
90 #    {"Fetch News" : 1,
91 #     "Shuffle News" : 1},
92 #    "www.bellevuereporter.com",
93 #    [ "/feed/" ],
94 #    "Test" )
95 # d = """
96 # <DIV style="padding:8px;
97 #     font-size:44pt;
98 #     -webkit-column-break-inside:avoid;"><P>
99 # <B>Task force will tackle issues of racial justice, police reform</B>
100 # <BR>Bellevue Reporter
101 # Bellevue Reporter - Your local homepage.
102 # Inslee names civil rights activists, pastors, and cops to panel that may forge ideas f#or new laws Task force will tackle issues of racial justice, police reform
103 # Wire Service
104 # </DIV>"""
105 # d = x.munge_description(d)
106 # print(d)
107 # if x.fetch_news() == 0:
108 #    print("Error fetching news, no items fetched.")
109 # x.shuffle_news()