For now, catch all the UTF-8 exceptions until I can fix it properly.

This commit is contained in:
Jonathan Harker 2010-10-20 14:01:18 +01:00
parent a187045c0c
commit 45dcb7d7f3

113
lolbot.py
View file

@ -10,18 +10,22 @@ Useful bot for folks stuck behind censor walls at work
Logs a channel and collects URLs for later. Logs a channel and collects URLs for later.
""" """
import sys, string, random, time try:
from ircbot import SingleServerIRCBot, OutputManager import sys, string, random, time
from irclib import nm_to_n, nm_to_h, irc_lower from ircbot import SingleServerIRCBot, OutputManager
import os from irclib import nm_to_n, nm_to_h, irc_lower
import os
from datetime import datetime from datetime import datetime
from mechanize import Browser from mechanize import Browser
import getopt import getopt
from sqlalchemy import MetaData, Table, Column, String, Text, Integer, DateTime, engine_from_config from sqlalchemy import MetaData, Table, Column, String, Text, Integer, DateTime, engine_from_config
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
except ImportError:
print "Some modules could not be loaded: Lolbot relies on Mechanize and SQLAlchemy.\n"
sys.exit
# Exclamations - wrong input # Exclamations - wrong input
exclamations = [ exclamations = [
@ -37,7 +41,6 @@ exclamations = [
ponderings = [ ponderings = [
"Hi, can I have a medium lamb roast, with just potatoes.", "Hi, can I have a medium lamb roast, with just potatoes.",
"Can I slurp on your Big Cock?", "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.", "Quentin Tarantino is so awesome I want to have his babies.",
"No it's a week night 8pm is past my bedtime.", "No it's a week night 8pm is past my bedtime.",
] ]
@ -193,27 +196,34 @@ 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):
db = self.get_session() try:
if not db.query(Url).filter(Url.url == url).count(): db = self.get_session()
theurl = Url(nickname, url) if not db.query(Url).filter(Url.url == url).count():
db.add(theurl) theurl = Url(nickname, url)
db.commit() db.add(theurl)
else: db.commit()
try: else:
theurl = db.query(Url).filter(Url.url == url).one() try:
except MultipleResultsFound, ex: theurl = db.query(Url).filter(Url.url == url).one()
print ex #wtf print theurl
except NoResultsFound, ex: return theurl.title
print ex #wtf except MultipleResultsFound, ex:
print theurl print ex #wtf
return theurl.title except NoResultsFound, ex:
print ex #wtf
except Exception, ex:
print "Exception caught saving URL: %s" % ex
return ""
def log_event(self, nick, text): def log_event(self, nick, text):
entry = Log(nick, text) try:
db = self.get_session() entry = Log(nick, text)
db.add(entry) db = self.get_session()
db.commit() db.add(entry)
print entry db.commit()
print entry
except Exception, ex:
print "Exception caught logging event: %s" % ex
def on_nicknameinuse(self, connection, event): def on_nicknameinuse(self, connection, event):
self.nickname = connection.get_nickname() + "_" self.nickname = connection.get_nickname() + "_"
@ -280,28 +290,33 @@ class LolBot(SingleServerIRCBot):
else: else:
target = from_private.strip() target = from_private.strip()
if cmd == 'help': try:
self.reply(self.helptext, target) if cmd == 'help':
self.reply(self.helptext, target)
elif cmd == 'lol': elif cmd == 'lol':
self.reply(self.ponder(), target) self.reply(self.ponder(), target)
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):
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 == '':
self.reply('URL added.', target) self.reply('URL added.', target)
if title != '': if title != '':
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)
except Exception, ex:
print "Exception caught processing command: %s" % ex
print " command was '%s' from %s" % (cmd, target)
def usage(): def usage():