Make gkeep use a stored token.
[kiosk.git] / bellevue_reporter_rss_renderer.py
1 #!/usr/bin/env python3
2
3 import logging
4 import re
5 from typing import List, Dict
6 import xml
7 import xml.etree.ElementTree as ET
8
9 import generic_news_rss_renderer as gnrss
10
11
12 logger = logging.getLogger(__file__)
13
14
15 class bellevue_reporter_rss_renderer(gnrss.generic_news_rss_renderer):
16     """Read the Bellevue Reporter's RSS feed."""
17
18     def __init__(
19         self,
20         name_to_timeout_dict: Dict[str, int],
21         feed_site: str,
22         feed_uris: List[str],
23         page_title: str,
24     ):
25         super().__init__(
26             name_to_timeout_dict, feed_site, feed_uris, page_title
27         )
28
29     def get_headlines_page_prefix(self) -> str:
30         return "bellevue-reporter"
31
32     def get_details_page_prefix(self) -> str:
33         return "bellevue-reporter-details"
34
35     def should_use_https(self) -> bool:
36         return True
37
38     def munge_description(self, description: str, item: ET.Element) -> str:
39         description = re.sub("<[^>]+>", "", description)
40         description = re.sub(
41             "Bellevue\s+Reporter\s+Bellevue\s+Reporter", "", description
42         )
43         description = re.sub("\s*\-\s*Your local homepage\.\s*", "", description)
44         description = re.sub("[Ww]ire [Ss]ervice", "", description)
45         return description
46
47     @staticmethod
48     def looks_like_football(title: str, description: str) -> bool:
49         return (
50             title.find("NFL") != -1
51             or re.search("[Ll]ive [Ss]tream", title) is not None
52             or re.search("[Ll]ive[Ss]tream", title) is not None
53             or re.search("[Ll]ive [Ss]tream", description) is not None
54         )
55
56     @staticmethod
57     def looks_like_review(title: str, description: str) -> bool:
58         return "review" in title or "Review" in title
59
60     @staticmethod
61     def looks_like_spam(title: str, description: str) -> bool:
62         return (
63             description is not None
64             and title is not None
65             and (
66                 'marketplace' in description
67                 or 'national-marketplace' in description
68                 or re.search('[Ww]eed', title) is not None
69                 or re.search('[Cc]annabis', title) is not None
70                 or re.search('[Cc]annabis', description) is not None
71                 or 'THC' in title
72                 or re.search('[Tt]op.[Rr]ated', title) is not None
73                 or re.search('[Ll]ose [Ww]eight', title) is not None
74                 or re.search('[Ll]ose [Ww]eight', description) is not None
75             )
76         )
77
78     def item_is_interesting_for_headlines(
79         self, title: str, description: str, item: xml.etree.ElementTree.Element
80     ) -> bool:
81         unfiltered_description = item.findtext("description")
82         if self.is_item_older_than_n_days(item, 10):
83             logger.info(f'{title}: is too old!')
84             return False
85         if bellevue_reporter_rss_renderer.looks_like_spam(title, unfiltered_description):
86             logger.debug(f'{title}: looks like spam')
87             return False
88         if bellevue_reporter_rss_renderer.looks_like_football(title, description):
89             logger.debug(f'{title}: looks like it\'s about football.')
90             return False
91         if bellevue_reporter_rss_renderer.looks_like_review(title, description):
92             logger.debug(f'{title}: looks like a review.')
93             return False
94         return True
95
96     def item_is_interesting_for_article(
97         self, title: str, description: str, item: xml.etree.ElementTree.Element
98     ) -> bool:
99         unfiltered_description = item.findtext("description")
100         if self.is_item_older_than_n_days(item, 10):
101             logger.debug(f'{title}: is too old!')
102             return False
103         if bellevue_reporter_rss_renderer.looks_like_spam(title, unfiltered_description):
104             logger.debug(f'{title}: looks like spam')
105             return False
106         if bellevue_reporter_rss_renderer.looks_like_football(title, description):
107             logger.debug(f'{title}: looks like it\'s about football.')
108             return False
109         if bellevue_reporter_rss_renderer.looks_like_review(title, description):
110             logger.debug(f'{title}: looks like a review.')
111             return False
112         return True
113
114
115 # Test
116 # x = bellevue_reporter_rss_renderer(
117 #    {"Fetch News" : 1,
118 #     "Shuffle News" : 1},
119 #    "www.bellevuereporter.com",
120 #    [ "/feed/" ],
121 #    "Test" )
122 # d = """
123 # <DIV style="padding:8px;
124 #     font-size:44pt;
125 #     -webkit-column-break-inside:avoid;"><P>
126 # <B>Task force will tackle issues of racial justice, police reform</B>
127 # <BR>Bellevue Reporter
128 # Bellevue Reporter - Your local homepage.
129 # 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
130 # Wire Service
131 # </DIV>"""
132 # d = x.munge_description(d)
133 # print(d)
134 # if x.fetch_news() == 0:
135 #    print("Error fetching news, no items fetched.")
136 # x.shuffle_news()