Skip to content

Instantly share code, notes, and snippets.

@iScripters
Created September 4, 2015 18:35
Show Gist options
  • Save iScripters/26bc961a4f85efa98a9b to your computer and use it in GitHub Desktop.
Save iScripters/26bc961a4f85efa98a9b to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol, task
from twisted.python import log
import time
import sys
import feedparser
import ConfigParser
class RSS:
def __init__(self):
self.feeds = ['http://forum.pluton-team.org/forums/news/index.rss',
'http://forum.pluton-team.org/forums/pluton-feature-requests.3/index.rss',
'http://forum.pluton-team.org/forums/coresupport/index.rss',
'http://forum.pluton-team.org/forums/pluginsupport/index.rss',
'http://forum.pluton-team.org/forums/tutorials-f-a-q.25/index.rss',
'http://forum.pluton-team.org/forums/wiki.27/index.rss',
'http://forum.pluton-team.org/forums/community-plugins.21/index.rss',
'http://forum.pluton-team.org/forums/official-plugins.22/index.rss',
'http://forum.pluton-team.org/forums/work-in-progress-plugins.14/index.rss',
'http://forum.pluton-team.org/forums/plugin-requests.15/index.rss',
'http://forum.pluton-team.org/forums/plugin-development-support.18/index.rss',
'http://forum.pluton-team.org/forums/com_server/index.rss',
'http://forum.pluton-team.org/forums/general-discussion.12/index.rss']
self.delay = 60
self.news = []
self.ini = ConfigParser.ConfigParser()
def parse(self):
self.ini.read('rss.ini')
for feed in self.feeds:
rss = feedparser.parse(feed)
feedtitle = rss['feed']['title']
topic = rss['entries'][0]['title']
url = rss['entries'][0]['link']
author = rss['entries'][0]['author']
published = rss['entries'][0]['published']
if feedtitle in self.ini.sections():
feedinfo = self.ini.items(feedtitle)
if feedinfo[0][1] == topic and feedinfo[1][1] == url and feedinfo[2][1] == published:
pass
else:
self.news.append("[{}] Forum activity in topic '{}', started by {} __ {}"
.format(feedtitle, topic, author, url))
self.ini.set(feedtitle, 'topic', topic)
self.ini.set(feedtitle, 'url', url)
self.ini.set(feedtitle, 'published', published)
with open('rss.ini', 'wb') as inifile:
self.ini.write(inifile)
else:
self.ini.add_section(feedtitle)
self.ini.set(feedtitle, 'topic', topic)
self.ini.set(feedtitle, 'url', url)
self.ini.set(feedtitle, 'published', published)
with open('rss.ini', 'wb') as inifile:
self.ini.write(inifile)
print self.news
return self.news
class MessageLogger:
def __init__(self, file):
self.file = file
def log(self, message):
timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
self.file.write('%s %s\n' % (timestamp, message))
self.file.flush()
def close(self):
self.file.close()
class LogBot(irc.IRCClient):
nickname = "PlutonForums"
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger(open(self.factory.filename, "a"))
self.logger.log("[connected at %s]" %
time.asctime(time.localtime(time.time())))
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self.logger.log("[disconnected at %s]" %
time.asctime(time.localtime(time.time())))
self.logger.close()
def signedOn(self):
self.join(self.factory.channel)
tasks = task.LoopingCall(self.rssupdates)
tasks.start(10)
def joined(self, channel):
self.logger.log("[I have joined %s]" % channel)
def privmsg(self, user, channel, msg):
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
if channel == self.nickname:
msg = "It isn't nice to whisper! Play nice with the group."
self.msg(user, msg)
return
if msg.startswith(self.nickname + ":"):
msg = "%s: I am a log bot" % user
self.msg(channel, msg)
self.logger.log("<%s> %s" % (self.nickname, msg))
def rssupdates(self):
rss = RSS()
data = rss.parse()
if data:
for item in data:
print item
self.sendLine("PRIVMSG #pluton :{}".format(item))
def action(self, user, channel, msg):
user = user.split('!', 1)[0]
self.logger.log("* %s %s" % (user, msg))
# irc callbacks
def irc_NICK(self, prefix, params):
old_nick = prefix.split('!')[0]
new_nick = params[0]
self.logger.log("%s is now known as %s" % (old_nick, new_nick))
def alterCollidedNick(self, nickname):
return nickname + '^'
class LogBotFactory(protocol.ClientFactory):
protocol = LogBot
def __init__(self, channel, filename):
self.channel = channel
self.filename = filename
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
if __name__ == '__main__':
log.startLogging(sys.stdout)
f = LogBotFactory("pluton", "pluton.log") # channel, logfile
reactor.connectTCP("irc.freenode.net", 6667, f)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment