Commit 45dcb7d7 authored by Jonathan Harker's avatar Jonathan Harker

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

parent a187045c
...@@ -10,18 +10,22 @@ Useful bot for folks stuck behind censor walls at work ...@@ -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 mechanize import Browser from datetime import datetime
from mechanize import Browser
import getopt
from sqlalchemy import MetaData, Table, Column, String, Text, Integer, DateTime, engine_from_config import getopt
from sqlalchemy.orm import sessionmaker from sqlalchemy import MetaData, Table, Column, String, Text, Integer, DateTime, engine_from_config
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker
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 = [ ...@@ -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): ...@@ -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): ...@@ -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':
self.reply(self.ponder(), target) elif cmd == 'lol':
self.reply(self.ponder(), target)
elif cmd == 'urls' or cmd == 'list':
db = self.get_session() elif cmd == 'urls' or cmd == 'list':
for url in db.query(Url).order_by(Url.timestamp): db = self.get_session()
line = "%s %s" % (url.url, url.title) for url in db.query(Url).order_by(Url.timestamp):
self.reply(line, target) line = "%s %s" % (url.url, url.title)
time.sleep(1) self.reply(line, target)
time.sleep(1)
elif cmd.startswith('http:') or cmd.startswith('https:'):
title = self.save_url(from_private, cmd) elif cmd.startswith('http:') or cmd.startswith('https:'):
if title == '': title = self.save_url(from_private, cmd)
self.reply('URL added.', target) if title == '':
if title != '': self.reply('URL added.', target)
self.reply('URL added: %s' % title, target) if title != '':
self.reply('URL added: %s' % title, target)
else:
self.reply(self.exclaim(), target) else:
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():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment