List now takes a parameter. Now lists only the most recent ten URLs by default.
This commit is contained in:
parent
45dcb7d7f3
commit
b00292025b
1 changed files with 47 additions and 16 deletions
63
lolbot.py
63
lolbot.py
|
|
@ -122,7 +122,7 @@ class LolBot(SingleServerIRCBot):
|
||||||
SqlBase.metadata.create_all()
|
SqlBase.metadata.create_all()
|
||||||
self.get_session = sessionmaker(bind=self.dbengine)
|
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; <url> - 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; <url> - adds the URL to the list; help - this message."
|
||||||
|
|
||||||
self.queue = OutputManager(self.connection)
|
self.queue = OutputManager(self.connection)
|
||||||
self.queue.start()
|
self.queue.start()
|
||||||
|
|
@ -196,6 +196,7 @@ class LolBot(SingleServerIRCBot):
|
||||||
return datetime.today().strftime("%Y-%m-%d %H:%M:%S")
|
return datetime.today().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
def save_url(self, nickname, url):
|
def save_url(self, nickname, url):
|
||||||
|
title = False
|
||||||
try:
|
try:
|
||||||
db = self.get_session()
|
db = self.get_session()
|
||||||
if not db.query(Url).filter(Url.url == url).count():
|
if not db.query(Url).filter(Url.url == url).count():
|
||||||
|
|
@ -203,17 +204,12 @@ class LolBot(SingleServerIRCBot):
|
||||||
db.add(theurl)
|
db.add(theurl)
|
||||||
db.commit()
|
db.commit()
|
||||||
else:
|
else:
|
||||||
try:
|
theurl = db.query(Url).filter(Url.url == url).one()
|
||||||
theurl = db.query(Url).filter(Url.url == url).one()
|
print theurl
|
||||||
print theurl
|
title = theurl.title
|
||||||
return theurl.title
|
|
||||||
except MultipleResultsFound, ex:
|
|
||||||
print ex #wtf
|
|
||||||
except NoResultsFound, ex:
|
|
||||||
print ex #wtf
|
|
||||||
except Exception, ex:
|
except Exception, ex:
|
||||||
print "Exception caught saving URL: %s" % ex
|
print "Exception caught saving URL: %s" % ex
|
||||||
return ""
|
return title
|
||||||
|
|
||||||
def log_event(self, nick, text):
|
def log_event(self, nick, text):
|
||||||
try:
|
try:
|
||||||
|
|
@ -253,7 +249,10 @@ class LolBot(SingleServerIRCBot):
|
||||||
for w in words:
|
for w in words:
|
||||||
if w.startswith('http://') or w.startswith('https://'):
|
if w.startswith('http://') or w.startswith('https://'):
|
||||||
title = self.save_url(from_nick, w)
|
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):
|
def say_public(self, text):
|
||||||
"Print TEXT into public channel, for all to see."
|
"Print TEXT into public channel, for all to see."
|
||||||
|
|
@ -299,17 +298,47 @@ class LolBot(SingleServerIRCBot):
|
||||||
|
|
||||||
elif cmd == 'urls' or cmd == 'list':
|
elif cmd == 'urls' or cmd == 'list':
|
||||||
db = self.get_session()
|
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)
|
line = "%s %s" % (url.url, url.title)
|
||||||
self.reply(line, target)
|
self.reply(line, target)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
elif cmd.startswith('http:') or cmd.startswith('https:'):
|
elif cmd.startswith('http:') or cmd.startswith('https:'):
|
||||||
title = self.save_url(from_private, cmd)
|
title = self.save_url(from_private, cmd)
|
||||||
if title == '':
|
if title == False:
|
||||||
self.reply('URL added.', target)
|
self.say_public("Sorry, I'm useless at UTF-8.")
|
||||||
if title != '':
|
else:
|
||||||
self.reply('URL added: %s' % title, target)
|
self.reply('URL added. %s' % title, target)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.reply(self.exclaim(), target)
|
self.reply(self.exclaim(), target)
|
||||||
|
|
@ -317,6 +346,8 @@ class LolBot(SingleServerIRCBot):
|
||||||
except Exception, ex:
|
except Exception, ex:
|
||||||
print "Exception caught processing command: %s" % ex
|
print "Exception caught processing command: %s" % ex
|
||||||
print " command was '%s' from %s" % (cmd, target)
|
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():
|
def usage():
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue