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 send(self, message):
56 """simple wrapper around bot send_message method"""
57 self.bot.send_message(mto=self.bot.room,
62 def new_posts(self, feed):
63 """Send new posts in feed"""
64 parsed_feed = feed_parse(feed)
66 # Cannot resolve address
67 if 'status' not in parsed_feed:
68 self.bot.log.error('Error from "%s": %s.' %
69 (feed, parsed_feed.bozo_exception.__repr__()))
72 # unusual return http code
73 if parsed_feed.status != 200:
75 'Got code %(status)d from "%(href)s" (please update).' %
79 feed_updated = parsed_feed.feed.get('updated_parsed', None)
81 # Avoid looping over all posts if possible
82 if feed_updated and strtm_to_dtm(feed_updated) < self.last_check:
83 self.bot.log.debug('updated : %s' % strtm_to_dtm(feed_updated))
84 self.bot.log.debug('last check: %s' % self.last_check)
87 title = '"%s":' % parsed_feed.feed.get('title', 'n/a')
88 xtitle = '<strong>%s</strong>:' % html_escape(
89 parsed_feed.feed.get('title', 'n/a'))
92 feed_id = parsed_feed.feed.get('id', feed)
93 if not self.seen.setdefault(feed_id):
94 # Fills with post id when first started (prevent from posting all
96 self.seen[feed_id] = {p.id for p in parsed_feed.entries}
100 entries = {p.id for p in parsed_feed.entries}
101 new_entries = [p for p in parsed_feed.entries
102 if p.id in entries - self.seen.get(feed_id)]
103 for post in new_entries:
104 self.bot.log.info(post.title)
106 body = '%(title)s %(link)s' % post
110 xpost['title'] = html_escape(xpost.get('title', 'n/a'))
111 xbody = '<a href="%(link)s">%(title)s</a>' % xpost
114 self.seen[feed_id] = entries
116 self.bot.log.debug('<br />'.join(xhtml))
117 self.send(('<br />'.join(xhtml), '\n'.join(text)))
120 while not self.thread_killed:
121 self.bot.log.debug('feeds check')
122 for feed in self.feeds_list:
125 except Exception as err:
126 self.bot.log.error('feeds thread crashed: %s' % err)
127 self.bot.log.error(''.join(traceback.format_exc()))
128 self.thread_killed = True
129 self.last_check = datetime.datetime.utcnow()
130 for _ in list(range(self.tempo)):
132 if self.thread_killed:
139 # not working <http://bugs.debian.org/612274>
140 # 'http://www.debian.org/security/dsa',
142 # not working <http://bugs.debian.org/612274>
143 # 'http://www.debian.org/News/news',
146 'http://www.debian.org/News/weekly/dwn.fr.rdf',
149 'http://rss.gmane.org/topics/excerpts/gmane.linux.debian.devel.announce',
150 'http://rss.gmane.org/gmane.linux.debian.user.security.announce',
151 'http://planet-fr.debian.net/users/rss20.xml',
152 'http://planet.debian.org/atom.xml',
155 def __init__(self, bot):
156 Plugin.__init__(self, bot)
157 self.last_check = None
158 self.th_mon = FeedMonitor(self)
162 self.th_mon.thread_killed = True
165 def feeds(self, message, args):
166 """feeds monitors debian project related feeds.
167 !feeds : registred feeds list
168 !feeds last : last check time"""
170 return 'Last feeds check: %s' % self.th_mon.last_check
171 return '\n'.join(Feeds.FEEDS)