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/>.
21 from feedparser import parse as feed_parse
23 from .plugin import Plugin, botcmd
35 def html_escape(text):
36 """Produce entities within text."""
37 return ''.join(html_escape_table.get(c, c) for c in text)
40 def strtm_to_dtm(struc_time):
41 return datetime.datetime(*struc_time[:6])
44 class FeedMonitor(threading.Thread):
45 def __init__(self, plugin):
46 threading.Thread.__init__(self)
47 self.feeds_list = plugin.FEEDS
48 self.tempo = plugin.TEMPO
50 self.last_check = datetime.datetime.utcnow()
52 self.thread_killed = False
54 def send(self, message):
55 """simple wrapper around bot send_message method"""
56 self.bot.send_message(mto=self.bot.room,
61 def new_posts(self, feed):
62 """Send new posts in feed"""
63 parsed_feed = feed_parse(feed)
65 # Cannot resolve address
66 if 'status' not in parsed_feed:
67 self.bot.log.error('Error from "%s": %s.' %
68 (feed, parsed_feed.bozo_exception.__repr__()))
71 # unusual return http code
72 if parsed_feed.status != 200:
74 'Got code %(status)d from "%(href)s" (please update).' %
78 feed_updated = parsed_feed.feed.get('updated_parsed', None)
80 # Avoid looping over all posts if possible
81 if feed_updated and strtm_to_dtm(feed_updated) < self.last_check:
82 self.bot.log.debug('updated : %s' % strtm_to_dtm(feed_updated))
83 self.bot.log.debug('last check: %s' % self.last_check)
86 title = '"%s":' % parsed_feed.feed.get('title', 'n/a')
87 xtitle = '<strong>%s</strong>:' % html_escape(
88 parsed_feed.feed.get('title', 'n/a'))
91 feed_id = parsed_feed.feed.get('id', feed)
92 if not self.seen.setdefault(feed_id):
93 # Fills with post id when first started (prevent from posting all
95 self.seen[feed_id] = [post.id for post in parsed_feed.entries]
98 for post in parsed_feed.entries:
99 if post.id not in self.seen.get(feed_id):
100 self.seen[feed_id].append(post.id)
101 self.bot.log.info(post.title)
103 body = '%(title)s %(link)s' % post
107 xpost['title'] = html_escape(xpost.get('title', 'n/a'))
108 xbody = '<a href="%(link)s">%(title)s</a>' % xpost
112 self.send(('<br/>'.join(xhtml), '\n'.join(text)))
115 while not self.thread_killed:
116 self.bot.log.debug('feeds check')
117 for feed in self.feeds_list:
120 except Exception as err:
121 self.bot.log.error('feeds thread crashed')
122 self.bot.log.error(err)
123 self.thread_killed = True
124 self.last_check = datetime.datetime.utcnow()
125 for _ in list(range(self.tempo)):
127 if self.thread_killed:
134 # not working <http://bugs.debian.org/612274>
135 # 'http://www.debian.org/security/dsa',
137 # not working <http://bugs.debian.org/612274>
138 # 'http://www.debian.org/News/news',
141 'http://www.debian.org/News/weekly/dwn.fr.rdf',
144 'http://rss.gmane.org/topics/excerpts/gmane.linux.debian.devel.announce',
145 'http://rss.gmane.org/gmane.linux.debian.user.security.announce',
146 'http://planet-fr.debian.net/users/rss20.xml',
147 'http://planet.debian.org/atom.xml',
150 def __init__(self, bot):
151 Plugin.__init__(self, bot)
152 self.last_check = None
153 self.th_mon = FeedMonitor(self)
157 self.th_mon.thread_killed = True
160 def feeds(self, message, args):
161 """feeds monitors debian project related feeds.
162 !feeds : registred feeds list
163 !feeds last : last check time"""
165 return 'Last feeds check: %s' % self.th_mon.last_check
166 return '\n'.join(Feeds.FEEDS)