From b25c5ddeea7251703f6bb0ffceb91d6fbd77c388 Mon Sep 17 00:00:00 2001 From: Jonathan Harker Date: Mon, 31 May 2010 03:07:55 +1200 Subject: [PATCH] Now logging to stdout. --- lolbot.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lolbot.py b/lolbot.py index c3a985c..8ca0d68 100644 --- a/lolbot.py +++ b/lolbot.py @@ -7,13 +7,16 @@ """ Useful bot for folks stuck behind censor walls at work +Logs a channel and collects URLs for later. """ import sys, string, random, time from ircbot import SingleServerIRCBot from irclib import nm_to_n, nm_to_h, irc_lower import botcommon -import time +import os + +from datetime import datetime from mechanize import Browser # Exclamations - wrong input @@ -31,6 +34,8 @@ ponderings = [ "Hi, can I have a medium lamb roast, with just potatoes.", "Can I slurp on your Big Cock?", "Your Mum likes it two in the pink one in the stink.", + "Quentin Tarantino is so awesome I want to have his babies.", + "No it's a week night 8pm is past my bedtime.", ] class LolBot(SingleServerIRCBot): @@ -40,7 +45,6 @@ class LolBot(SingleServerIRCBot): self.channel = channel self.nickname = nickname self.urls = {} - self.logfile = open('/tmp/lolbot.log', 'a') self.helptext = "Adds URLs to a list. Commands: list - prints a bunch of URLs; clear - clears the list; lol - say something funny; - adds the URL to the list; help - this message." @@ -54,11 +58,18 @@ class LolBot(SingleServerIRCBot): br.open(url) title = br.title() except Exception as ex: - print ex title = '' self.urls[url] = title return title + def now(self): + return datetime.today().strftime("%Y-%m-%d %H:%M:%S") + + def log_event(self, event): + nick = nm_to_n(event.source()) + text = event.arguments()[0] + print "(%s) %s: %s" % (self.now(), nick, text) + def on_nicknameinuse(self, connection, event): self.nickname = connection.get_nickname() + "_" connection.nick(self.nickname) @@ -75,7 +86,7 @@ class LolBot(SingleServerIRCBot): "Deal with a public message in a channel." # log it - self.logfile.write("%s\n" % event.arguments()[0]) + self.log_event(event) from_nick = nm_to_n(event.source()) args = string.split(event.arguments()[0], ":", 1) @@ -92,6 +103,7 @@ class LolBot(SingleServerIRCBot): def say_public(self, text): "Print TEXT into public channel, for all to see." self.queue.send(text, self.channel) + print "(%s) %s: %s" % (self.now(), self.nickname, text) def say_private(self, nick, text): "Send private message of TEXT to NICK." @@ -105,7 +117,7 @@ class LolBot(SingleServerIRCBot): self.say_public(text) def ponder(self): - "Return a random string indicating what Pinky's pondering." + "Return a random pondering." return random.choice(ponderings) def exclaim(self): @@ -119,15 +131,10 @@ class LolBot(SingleServerIRCBot): """ if event.eventtype() == "pubmsg": - # self.reply() sees 'from_private = None' and sends to public channel. target = None else: - # assume that from_private comes from a 'privmsg' event. target = from_private.strip() - # Be forgiving about capitalization and whitespace. - #cmd = cmd.replace(" ", "").lower() - if cmd == 'help': self.reply(self.helptext, target)