misc cleaning, fix urban

This commit is contained in:
Ryan Hitchman 2009-04-19 05:42:48 -06:00
parent e284ed75ba
commit 322205eb92
6 changed files with 21 additions and 20 deletions

8
bot.py
View File

@ -6,18 +6,12 @@ channel = "#cobol"
import sys
import os
import glob
import re
import thread
import Queue
import collections
sys.path += ['plugins'] # so 'import hook' works without duplication
os.chdir(sys.path[0]) # do stuff relative to the installation directory
import irc
import yaml
os.chdir(sys.path[0]) # do stuff relative to the installation directory
class Bot(object):

View File

@ -1,3 +1,5 @@
import thread
class Input(object):
def __init__(self, raw, prefix, command,

View File

@ -1,3 +1,6 @@
import glob
import collections
if 'plugin_mtimes' not in globals():
mtimes = {}

16
irc.py
View File

@ -52,10 +52,10 @@ class crlf_tcp(asynchat.async_chat):
self.iqueue.put(decode(line))
self.buffer = ''
irc_prefix_re = re.compile(r'(.*?) (.*?) (.*)')
irc_noprefix_re = re.compile(r'()(.*?) (.*)')
irc_param_re = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)')
irc_netmask_re = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)')
irc_prefix_rem = re.compile(r'(.*?) (.*?) (.*)').match
irc_noprefix_rem = re.compile(r'()(.*?) (.*)').match
irc_netmask_rem = re.compile(r':?([^!@]*)!?([^@]*)@?(.*)').match
irc_param_ref = re.compile(r'(?:^|(?<= ))(:.*|[^ ]+)').findall
class irc(object):
@ -76,11 +76,11 @@ class irc(object):
while True:
msg = self.conn.iqueue.get()
if msg.startswith(":"): #has a prefix
prefix, command, params = irc_prefix_re.match(msg).groups()
prefix, command, params = irc_prefix_rem(msg).groups()
else:
prefix, command, params = irc_noprefix_re.match(msg).groups()
nick, user, host = irc_netmask_re.match(prefix).groups()
paramlist = irc_param_re.findall(params)
prefix, command, params = irc_noprefix_rem(msg).groups()
nick, user, host = irc_netmask_rem(prefix).groups()
paramlist = irc_param_ref(params)
lastparam = ""
if paramlist and paramlist[-1].startswith(':'):
lastparam = paramlist[-1][1:]

View File

@ -46,7 +46,10 @@ def command(func=None, hook=None, **kwargs):
if func is not None:
args['name'] = func
if hook is not None:
args['hook'] = hook
if isinstance(hook, list):
args['hook'] = '(?:' + '|'.join(hook) + ')'
else:
args['hook'] = hook
args.update(kwargs)
return command_wrapper
else:

View File

@ -1,11 +1,10 @@
import lxml
from lxml import html
import urllib
import hook
@hook.command('u')
@hook.command
@hook.command(['u', 'urban'])
def urban(inp):
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
if not inp.strip():
@ -13,7 +12,7 @@ def urban(inp):
url = 'http://www.urbandictionary.com/define.php?term=' + \
urllib.quote(inp.strip(), safe='')
page = lxml.html.parse(url)
page = html.parse(url)
defs = page.xpath("//div[@class='definition']")
if not defs: