From b00292025b7c74fda538bcf2cc1c1cbef3e703c2 Mon Sep 17 00:00:00 2001 From: Jonathan Harker Date: Wed, 20 Oct 2010 16:55:51 +0100 Subject: [PATCH] List now takes a parameter. Now lists only the most recent ten URLs by default. --- lolbot.py | 63 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/lolbot.py b/lolbot.py index d682f97..8b0606a 100755 --- a/lolbot.py +++ b/lolbot.py @@ -122,7 +122,7 @@ class LolBot(SingleServerIRCBot): SqlBase.metadata.create_all() self.get_session = sessionmaker(bind=self.dbengine) - 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." + self.helptext = "Keeps a list of URLs. Commands: list [n|x-y] - prints the last 10 URLs (or n URLs, or x through y); clear - clears the list; lol - say something funny; - adds the URL to the list; help - this message." self.queue = OutputManager(self.connection) self.queue.start() @@ -196,6 +196,7 @@ class LolBot(SingleServerIRCBot): return datetime.today().strftime("%Y-%m-%d %H:%M:%S") def save_url(self, nickname, url): + title = False try: db = self.get_session() if not db.query(Url).filter(Url.url == url).count(): @@ -203,17 +204,12 @@ class LolBot(SingleServerIRCBot): db.add(theurl) db.commit() else: - try: - theurl = db.query(Url).filter(Url.url == url).one() - print theurl - return theurl.title - except MultipleResultsFound, ex: - print ex #wtf - except NoResultsFound, ex: - print ex #wtf + theurl = db.query(Url).filter(Url.url == url).one() + print theurl + title = theurl.title except Exception, ex: print "Exception caught saving URL: %s" % ex - return "" + return title def log_event(self, nick, text): try: @@ -253,7 +249,10 @@ class LolBot(SingleServerIRCBot): for w in words: if w.startswith('http://') or w.startswith('https://'): title = self.save_url(from_nick, w) - self.say_public(title) + if title == False: + self.say_public("Sorry, I'm useless at UTF-8.") + else: + self.say_public("URL added. %s" % title) def say_public(self, text): "Print TEXT into public channel, for all to see." @@ -299,17 +298,47 @@ class LolBot(SingleServerIRCBot): elif cmd == 'urls' or cmd == 'list': db = self.get_session() - for url in db.query(Url).order_by(Url.timestamp): + for url in db.query(Url).order_by(Url.timestamp.desc())[:10]: + line = "%s %s" % (url.url, url.title) + self.reply(line, target) + time.sleep(1) + + elif cmd.startswith('urls ') or cmd.startswith('list '): + db = self.get_session() + (listcmd, n) = cmd.split(" ", 1) + n = n.strip() + if n == "all": + rows = db.query(Url).order_by(Url.timestamp.desc()) + elif n.find("-") > 0: + (x, y) = n.split("-", 1) + try: + x = abs(int(x)) + y = abs(int(y)) + if y < x: + x, y = y, x + except ValueError, ex: + self.reply("Give me a number or a range of numbers, e.g. list 5 or list 11-20", target) + raise ex + rows = db.query(Url).order_by(Url.timestamp.desc())[x-1:y] + else: + try: + n = abs(int(n)) + except ValueError, ex: + self.reply("Give me a number or a range of numbers, e.g. list 5 or list 11-20", target) + raise ex + rows = db.query(Url).order_by(Url.timestamp.desc())[:n] + + for url in rows: line = "%s %s" % (url.url, url.title) self.reply(line, target) time.sleep(1) elif cmd.startswith('http:') or cmd.startswith('https:'): title = self.save_url(from_private, cmd) - if title == '': - self.reply('URL added.', target) - if title != '': - self.reply('URL added: %s' % title, target) + if title == False: + self.say_public("Sorry, I'm useless at UTF-8.") + else: + self.reply('URL added. %s' % title, target) else: self.reply(self.exclaim(), target) @@ -317,6 +346,8 @@ class LolBot(SingleServerIRCBot): except Exception, ex: print "Exception caught processing command: %s" % ex print " command was '%s' from %s" % (cmd, target) + self.reply("Sorry, I didn't understand: %s" % cmd, target) + self.reply(self.helptext, target) def usage():