Created
February 15, 2010 19:43
-
-
Save gdude2002/304927 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import feedparser | |
import irclib | |
import threading | |
import time | |
import datetime | |
import os | |
class IRCBot(irclib.SimpleIRCClient): | |
def __init__(self, password, channels, SERVER, PORT, NICKNAME): | |
irclib.SimpleIRCClient.__init__(self) | |
self.password = " " | |
self.channels = ["#minecraft"] | |
self.SERVER = "irc.esper.net" | |
self.PORT = 6666 | |
self.NICKNAME = "gdude2002bot" | |
self.authorized = [] | |
checkFeed().start() | |
self.connect(self.SERVER, self.PORT, self.NICKNAME, None, self.NICKNAME, self.NICKNAME, "", 6666, False, False) | |
self.quitted = False | |
def log(self, text): | |
logfile = open("log.log", "a") | |
logfile.write("["+str(datetime.datetime.today())+"]"+text+"\n") | |
logfile.flush | |
def minelife(self, connection, event, switch): | |
feed = feedparser.parse("http://minelife.tumblr.com/rss") | |
title = feed.entries[0].title | |
link = feed.entries[0].link | |
user = event.source().split("!")[0] | |
if not switch: | |
connection.notice(user, "The latest day on minelife is..") | |
connection.notice(user, title.encode('ascii','ignore')+" ("+link.encode('ascii','ignore')+")") | |
elif switch: | |
connection.privmsg(user, "The latest day on minelife is..") | |
connection.privmsg(user, +title.encode('ascii','ignore')+" ("+link.encode('ascii','ignore')+")") | |
def help(self, connection, event, switch, command): | |
user = event.source().split("!")[0] | |
if not switch: | |
if command == False: | |
connection.notice(user, "This bot is designed to give people access to Minecraft-related updates.") | |
connection.notice(user, "The list of commands are as follows:") | |
connection.notice(user, "help, pmhelp, blog, pmblog, todo, pmtodo, questions, pmq, mlife, and pmlife.") | |
connection.notice(user, "For more info on these, please try !help command or !help about.") | |
elif command.strip("!") == "help": | |
connection.notice(user, "!help shows the main help and lets you look up other commmands. Notice format.") | |
elif command.strip("!") == "pmhelp": | |
connection.notice(user, "!pmhelp shows the main help and lets you look up other commands. Query format.") | |
elif command.strip("!") == "blog": | |
connection.notice(user, "!blog allows you to check the latest update on Notch's blog. Notice format.") | |
elif command.strip("!") == "pmblog": | |
connection.notice(user, "!pmblog allows you to check the latest update on Notch's blog. Query format.") | |
elif command.strip("!") == "todo": | |
connection.notice(user, "!todo allows you to check the latest update to Notch's todo list. Notice format.") | |
connection.notice(user, "Please note, this command hasn't been implemented yet.") | |
elif command.strip("!") == "pmtodo": | |
connection.notice(user, "!pmtodo allows you to check the latest update to Notch's todo list. Query format.") | |
connection.notice(user, "Please note, this command hasn't been implemented yet.") | |
elif command.strip("!") == "questions": | |
connection.notice(user, "!questions lets you check the three latest questions and answers on Notch's formspring. Notice format.") | |
elif command.strip("!") == "pmq": | |
connection.notice(user, "!pmq lets you check the three latest questions and answers on Notch's formspring. Query format.") | |
elif command.strip("!") == "mlife": | |
connection.notice(user, "!mlife lets you check the latest day in MineLife. Notice format.") | |
elif command.strip("!") == "pmlife": | |
connection.notice(user, "!pmlife lets you check the latest day in MineLife. Query format.") | |
elif command.strip("!") == "about": | |
connection.notice(user, "This bot is owned, developed, and coded by gdude2002.") | |
connection.notice(user, "For more information, please see http://archives.pearsoncoles.com/bot/") | |
connection.notice(user, "Downloads will be available soon.") | |
connection.notice(user, "Have a suggestion? Please type '/ms send gdude2002' and your suggestion now.") | |
else: | |
connection.notice(user, "Command '"+command+"' not found. try !help") | |
elif switch: | |
if command == False: | |
connection.privmsg(user, "This bot is designed to give people access to Minecraft-related updates.") | |
connection.privmsg(user, "The list of commands are as follows:") | |
connection.privmsg(user, "help, pmhelp, blog, pmblog, todo, pmtodo, questions, pmq, mlife, and pmlife.") | |
connection.privmsg(user, "For more info on these, please try !pmhelp command or !pmhelp about.") | |
elif command.strip("!") == "help": | |
connection.privmsg(user, "!help shows the main help and lets you look up other commmands. Notice format.") | |
elif command.strip("!") == "pmhelp": | |
connection.privmsg(user, "!pmhelp shows the main help and lets you look up other commands. Query format.") | |
elif command.strip("!") == "blog": | |
connection.privmsg(user, "!blog allows you to check the latest update on Notch's blog. Notice format.") | |
elif command.strip("!") == "pmblog": | |
connection.privmsg(user, "!pmblog allows you to check the latest update on Notch's blog. Query format.") | |
elif command.strip("!") == "todo": | |
connection.privmsg(user, "!todo allows you to check the latest update to Notch's todo list. Notice format.") | |
connection.privmsg(user, "Please note, this command hasn't been implemented yet.") | |
elif command.strip("!") == "pmtodo": | |
connection.privmsg(user, "!pmtodo allows you to check the latest update to Notch's todo list. Query format.") | |
connection.privmsg(user, "Please note, this command hasn't been implemented yet.") | |
elif command.strip("!") == "questions": | |
connection.privmsg(user, "!questions lets you check the three latest questions and answers on Notch's formspring. Notice format.") | |
elif command.strip("!") == "pmq": | |
connection.privmsg(user, "!pmq lets you check the three latest questions and answers on Notch's formspring. Query format.") | |
elif command.strip("!") == "mlife": | |
connection.privmsg(user, "!mlife lets you check the latest day in MineLife. Notice format.") | |
elif command.strip("!") == "pmlife": | |
connection.privmsg(user, "!pmlife lets you check the latest day in MineLife. Query format.") | |
elif command.strip("!") == "about": | |
connection.privmsg(user, "This bot is owned, developed, and coded by gdude2002.") | |
connection.privmsg(user, "For more information, please see http://archives.pearsoncoles.com/bot/") | |
connection.privmsg(user, "Downloads will be available soon.") | |
connection.privmsg(user, "Have a suggestion? Please type '/ms send gdude2002' and your suggestion now.") | |
else: | |
connection.privmsg(user, "Command '"+command+"' not found. try !pmhelp") | |
def blog(self, connection, event, switch): | |
feed = feedparser.parse("http://notch.tumblr.com/rss") | |
title = feed.entries[0].title | |
link = feed.entries[0].link | |
user = event.source().split("!")[0] | |
if not switch: | |
connection.notice(user, "The latest post on Notch's blog is as follows:") | |
connection.notice(user, '%s ( %s )' % (title, link)) | |
elif switch: | |
connection.privmsg(user, "The latest post on Notch's blog is as follows:") | |
connection.privmsg(user, '%s ( %s )' % (title, link)) | |
def formspring(self, connection, event, switch): | |
feed = feedparser.parse("http://www.formspring.me/profile/Notch.rss") | |
title0 = feed.entries[0].title | |
title1 = feed.entries[1].title | |
title2 = feed.entries[2].title | |
link0 = feed.entries[0].link | |
link1 = feed.entries[1].link | |
link2 = feed.entries[2].link | |
cont0 = feed.entries[0].description | |
cont1 = feed.entries[1].description | |
cont2 = feed.entries[2].description | |
user = event.source().split("!")[0] | |
if not switch: | |
connection.notice(user, "The three latest entries on Notch's formspring are as follows:") | |
connection.notice(user, "(1) ["+title0+"] "+cont0+" ("+link0+")") | |
connection.notice(user, "(2) ["+title1+"] "+cont1+" ("+link1+")") | |
connection.notice(user, "(3) ["+title2+"] "+cont2+" ("+link2+")") | |
elif switch: | |
connection.privmsg(user, "The three latest entries on Notch's formspring are as follows:") | |
connection.privmsg(user, "(1) ["+title0+"] "+cont0+" ("+link0+")") | |
connection.privmsg(user, "(2) ["+title1+"] "+cont1+" ("+link1+")") | |
connection.privmsg(user, "(3) ["+title2+"] "+cont2+" ("+link2+")") | |
def on_welcome(self, connection, event): | |
for channel in self.channels: | |
connection.join(channel) | |
feed = feedparser.parse("http://notch.tumblr.com/rss") | |
title = feed.entries[0].title | |
link = feed.entries[0].link | |
connection.privmsg("NickServ", "IDENTIFY Blast0ise") | |
# connection.privmsg("#minecraft", "Notch's latest post: "+title+" ("+link+")") | |
self.log("Welcome message recieved, channels joined.") | |
def on_privmsg(self, connection, event): | |
command = event.arguments()[0] | |
user = event.source().split("!")[0] | |
if "!help" in command: | |
connection.notice(user, "] [".join(event.arguments()[0:])) | |
connection.privmsg(user, "] [".join(event.arguments()[0:])) | |
try: | |
if len(event.arguments()[1] < 2): | |
com = False | |
self.help(connection, event, False, com) | |
else: | |
com = event.arguments()[1] | |
self.help(connection, event, False, com) | |
except IndexError: | |
com = False | |
self.help(connection, event, False, com) | |
elif "!pmhelp" in command: | |
try: | |
if len(event.arguments()[1] < 2): | |
com = False | |
self.help(connection, event, True, com) | |
else: | |
com = event.arguments()[1] | |
self.help(connection, event, True, com) | |
except IndexError: | |
com = False | |
self.help(connection, event, True, com) | |
elif command == "!blog": | |
self.blog(connection, event, False) | |
self.log(user+" has used !blog.") | |
elif command == "!pmblog": | |
self.blog(connection, event, True) | |
self.log(user+" has used !pmblog.") | |
elif command == "!todo": | |
connection.notice(user, "!todo is not implemented yet!") | |
self.log(user+" has used !todo.") | |
elif command == "!questions": | |
self.formspring(connection, event, False) | |
self.log(user+" has used !questions.") | |
elif command == "!pmq": | |
self.formspring(connection, event, True) | |
self.log(user+" has used !pmq.") | |
elif command == "!mlife": | |
self.minelife(connection, event, False) | |
self.log(user+" has used !mlife.") | |
elif command == "!pmlife": | |
self.minelife(connection, event, True) | |
self.log(user+" has used !pmlife.") | |
if checkFeed().update: | |
if not self.sent: | |
connection.privmsg("#minecraft", checkFeed().text) | |
self.sent = True | |
self.log("I have announced notch's blog to the masses.") | |
else: | |
self.sent = False | |
print "("+event.target()+") <"+event.source().split("!")[0]+"> "+" ".join(event.arguments()[0:]) | |
self.log("("+event.target()+") <"+event.source().split("!")[0]+"> "+" ".join(event.arguments()[0:])) | |
on_pubmsg = on_privmsg | |
def on_quit(self, connection, event): | |
user = event.source().split("!")[0] | |
self.log(user+" has quit.") | |
self.log("Full hostname is "+event.source()) | |
def on_join(self, connection, event): | |
user = event.source().split("!")[0] | |
self.log(user+" has joined "+event.target()) | |
self.log("Full hostname is "+event.source()) | |
def on_part(self, connection, event): | |
source = event.source() | |
user = event.source().split("!")[0] | |
if source in self.authorized: | |
self.authorized.remove(source) | |
print event.source().split("!")[0]+" has left "+event.target() | |
self.log(user+" has left "+event.target()) | |
self.log("Full hostname is "+event.source().split("!")[0]) | |
def on_disconnect(self, connection, event): | |
if self.quitted: | |
connection.connect(self.SERVER, self.PORT, self.NICKNAME) | |
self.log("Ii was unsolicited, I rejoined.") | |
else: | |
self.log("It was a command. Shutting down!") | |
os.abort | |
exit | |
class checkFeed(threading.Thread): | |
i = True | |
update = False | |
def __init__(self): | |
threading.Thread.__init__(self) | |
def log(self, text): | |
logfile = open("log.log", "a") | |
logfile.write("["+str(datetime.datetime.today())+"]"+text+"\n") | |
logfile.flush | |
def th(self): | |
self.log("Restarting RSS loop!") | |
self.i = True | |
def run(self): | |
feed = feedparser.parse("http://notch.tumblr.com/rss") | |
self.update = False | |
while True: | |
while self.i: | |
if feed.entries[0].title == feedparser.parse("http://notch.tumblr.com/rss").entries[0].title: | |
self.log("No update yet.") | |
self.update = False | |
else: | |
feed = feedparser.parse("http://notch.tumblr.com/rss") | |
title = feed.entries[0].title | |
link = feed.entries[0].link | |
self.log("Update!!") | |
self.log(title+" "+link) | |
self.update = True | |
self.text = (u"\u0002Notch has posted!\u000F "+title+" "+link) | |
threading.Timer(150.0, self.th).start() | |
self.i = False | |
IRCBot(" ", ["#minecraft"], "irc.esper.net", 6667, "gdude2002bot").start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment