Changes.
[kiosk.git] / bellevue_city_calendar_renderer.py
1 #!/usr/bin/env python3
2
3 import datetime
4 import logging
5 import re
6 from typing import Dict, List, Optional
7 import xml
8
9 from dateutil.parser import parse
10
11 import generic_news_rss_renderer as gnrss
12
13
14 logger = logging.getLogger(__name__)
15
16
17 class bellevue_city_calendar_renderer(gnrss.generic_news_rss_renderer):
18     """Read the Bellevue City Calendar feed."""
19
20     def __init__(
21         self,
22         name_to_timeout_dict: Dict[str, int],
23         feed_site: str,
24         feed_uris: List[str],
25         page_title: str,
26     ):
27         super().__init__(name_to_timeout_dict, feed_site, feed_uris, page_title)
28
29     def get_headlines_page_prefix(self) -> str:
30         return "bellevue-calendar"
31
32     def get_details_page_prefix(self) -> str:
33         return "bellevue-calendar-details"
34
35     def should_use_https(self) -> bool:
36         return True
37
38     def get_event_time(
39         self, item: xml.etree.ElementTree.Element
40     ) -> Optional[datetime.datetime]:
41         return parse(self.find_pubdate(item))
42
43     def find_pubdate(self, item: xml.etree.ElementTree.Element) -> Optional[str]:
44         descr = item.findtext("description")
45         if descr is None:
46             return None
47         m = re.search(r'time datetime="([^"]+)"', descr)
48         if m is None:
49             return None
50         return m.group(1)
51
52     def item_is_interesting_for_headlines(
53         self, title: str, description: str, item: xml.etree.ElementTree.Element
54     ) -> bool:
55         if "City Council" in title:
56             return False
57         if "City Offices Closed" in title:
58             return False
59         if "Regular Meeting" in title:
60             return False
61         if "Commission" in title:
62             return False
63 #        date = self.get_event_time(item)
64 #        if date is None:
65 #            return False
66 #        tzinfo = date.tzinfo
67 #        now = datetime.datetime.now(tzinfo)
68 #        delta = (now - date).total_seconds() / (60 * 60 * 24)
69 #        return delta < 0
70         return True
71
72     def do_details(self) -> bool:
73         logger.debug("No Bellevue city calendar items are interesting for articles...")
74         return False
75
76     def item_is_interesting_for_article(
77         self, title: str, description: str, item: xml.etree.ElementTree.Element
78     ) -> bool:
79         logger.debug("No Bellevue city calendar items are interesting for articles...")
80         return False
81
82
83 # Test
84 # x = bellevue_city_calendar_renderer(
85 #   {"Fetch News" : 1,
86 #    "Shuffle News" : 1},
87 #   "bellevuewa.gov",
88 #   [ "/calendar/events.xml" ],
89 #   "Test" )
90 # if x.fetch_news() == 0:
91 #    print("Error fetching news, no items fetched.")
92 # else:
93 #    x.shuffle_news()