ad92c26523cf690c0062ee00e1a0ab9332635b97
[kiosk.git] / google_news_rss_renderer.py
1 from bs4 import BeautifulSoup
2 import generic_news_rss_renderer
3 import re
4
5
6 class google_news_rss_renderer(generic_news_rss_renderer.generic_news_rss_renderer):
7     def __init__(self, name_to_timeout_dict, feed_site, feed_uris, page_title):
8         super(google_news_rss_renderer, self).__init__(
9             name_to_timeout_dict, feed_site, feed_uris, page_title
10         )
11         self.debug = 1
12
13     def debug_prefix(self):
14         return "google-news"
15
16     def get_headlines_page_prefix(self):
17         return "google-news"
18
19     def get_details_page_prefix(self):
20         return "google-news-details"
21
22     def find_description(self, item):
23         descr = item.findtext("description")
24         source = item.findtext("source")
25         if source is not None:
26             descr = descr + " (%s)" % source
27         return descr
28
29     def munge_description_internal(self, descr):
30         if len(descr) > 450:
31             descr = descr[:450]
32             descr = re.sub(r"\<[^\>]*$", "", descr)
33             descr = descr + " [...]"
34         descr += "</A></LI></UL></OL></P>"
35         return descr
36
37     def munge_description(self, description):
38         soup = BeautifulSoup(description)
39         for a in soup.findAll("a"):
40             del a["href"]
41         descr = str(soup)
42         return munge_description_internal(descr)
43
44     def find_image(self, item):
45         return None
46
47     def should_use_https(self):
48         return True
49
50     def item_is_interesting_for_headlines(self, title, description, item):
51         return not self.is_item_older_than_n_days(item, 2)
52
53     def item_is_interesting_for_article(self, title, description, item):
54         return not self.is_item_older_than_n_days(item, 2)
55
56
57 # Test
58 # x = google_news_rss_renderer(
59 #    {"Fetch News" : 1,
60 #     "Shuffle News" : 1},
61 #    "news.google.com",
62 #    [ "/rss?hl=en-US&gl=US&ceid=US:en" ],
63 #    "Test" )
64 # if x.fetch_news() == 0:
65 #    print("Error fetching news, no items fetched.")
66 # x.shuffle_news()
67 #
68 # 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!"
69 # d = x.munge_description_internal(descr)
70 # print(d)