1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2011, 2014 kaliko <kaliko@azylum.org>
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, version 3 only.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 from feedparser import parse as feed_parse
24 from .plugin import Plugin, botcmd
36 def html_escape(text):
37 """Produce entities within text."""
38 return ''.join(html_escape_table.get(c, c) for c in text)
41 def strtm_to_dtm(struc_time):
42 return datetime.datetime(*struc_time[:6])
45 class FeedMonitor(threading.Thread):
46 def __init__(self, plugin):
47 threading.Thread.__init__(self)
48 self.feeds_list = plugin.FEEDS
49 self.tempo = plugin.TEMPO
51 self.last_check = datetime.datetime.utcnow()
53 self.thread_killed = False
55 def new_posts(self, feed):
56 """Send new posts in feed"""
57 parsed_feed = feed_parse(feed)
59 # Cannot resolve address
60 if 'status' not in parsed_feed:
61 self.plugin.log.error('Error from "%s": %s.' %
62 (feed, parsed_feed.bozo_exception.__repr__()))
65 # unusual return http code
66 if parsed_feed.status != 200:
67 self.plugin.log.warning(
68 'Got code %(status)d from "%(href)s" (please update).' %
72 feed_updated = parsed_feed.feed.get('updated_parsed', None)
74 # Avoid looping over all posts if possible
75 if feed_updated and strtm_to_dtm(feed_updated) < self.last_check:
76 self.plugin.log.debug('updated : %s' % strtm_to_dtm(feed_updated))
77 self.plugin.log.debug('last check: %s' % self.last_check)
80 title = '"%s":' % parsed_feed.feed.get('title', 'n/a')
81 xtitle = '<strong>%s</strong>:' % html_escape(
82 parsed_feed.feed.get('title', 'n/a'))
85 feed_id = parsed_feed.feed.get('id', feed)
86 if not self.seen.setdefault(feed_id):
87 # Fills with post id when first started (prevent from posting all
89 self.seen[feed_id] = {p.id for p in parsed_feed.entries}
93 entries = {p.id for p in parsed_feed.entries}
94 new_entries = [p for p in parsed_feed.entries
95 if p.id in entries - self.seen.get(feed_id)]
96 for post in new_entries:
97 self.plugin.log.info(post.title)
99 body = '%(title)s %(link)s' % post
102 xpost = {'title': html_escape(post.get('title', 'n/a'))}
103 xpost['link'] = html_escape(post.get('link',))
104 xbody = '<a href="{link}">{title}</a>'.format(**xpost)
107 self.seen[feed_id] = entries
109 self.plugin.send({'mhtml':'<br />'.join(xhtml), 'mbody':'\n'.join(text)})
112 while not self.thread_killed:
113 self.plugin.log.debug('feeds check')
114 for feed in self.feeds_list:
117 except Exception as err:
118 self.plugin.log.error('feeds thread crashed: %s' % err)
119 self.plugin.log.error(''.join(traceback.format_exc()))
120 self.thread_killed = True
121 self.last_check = datetime.datetime.utcnow()
122 for _ in list(range(self.tempo)):
124 if self.thread_killed:
131 # not working <http://bugs.debian.org/612274>
132 # 'http://www.debian.org/security/dsa',
134 # not working <http://bugs.debian.org/612274>
135 # 'http://www.debian.org/News/news',
138 'http://www.debian.org/News/weekly/dwn.fr.rdf',
141 'http://rss.gmane.org/topics/excerpts/gmane.linux.debian.devel.announce',
142 'http://rss.gmane.org/gmane.linux.debian.user.security.announce',
143 'http://planet-fr.debian.net/users/rss20.xml',
144 'http://planet.debian.org/atom.xml',
147 def __init__(self, bot):
148 Plugin.__init__(self, bot)
149 self.last_check = None
150 self.th_mon = FeedMonitor(self)
154 self.th_mon.thread_killed = True
157 def feeds(self, message, args):
158 """feeds monitors debian project related feeds.
159 !feeds : registred feeds list
160 !feeds last : last check time"""
162 self.send('Last feeds check: %s' % self.th_mon.last_check)
164 html = ['<a href="{0}">{1}</a>'.format(html_escape(u),
166 ) for u in Feeds.FEEDS]
168 'mbody': 'Feeds:\n' + '\n'.join(Feeds.FEEDS),
169 'mhtml': 'Feeds:<br />' + '<br />'.join(html),