urbandictionary plugin, '!' as command prefix, iambuttbot & twitter tweaks

This commit is contained in:
Ryan Hitchman 2009-04-16 21:52:47 -06:00
parent c93d99147a
commit 9deb8e244a
4 changed files with 35 additions and 3 deletions

5
bot.py
View File

@ -13,11 +13,12 @@ import thread
import Queue
import collections
sys.path += ['plugins'] # so 'import hook' works without duplication
import irc
import yaml
os.chdir(sys.path[0]) # do stuff relative to the installation directory
sys.path += ['plugins'] # so 'import hook' works without duplication
class Bot(object):
def __init__(self, nick, channel, network):
@ -68,7 +69,7 @@ print
print 'Connecting to IRC'
bot.irc = irc.irc(network, nick)
bot.irc.join(channel)
bot.commandprefix = '^(?:\.|'+nick+'[:,]*\s*)'
bot.commandprefix = r'^(?:[.!]|'+nick+r'[:,]*\s*)'
bot.persist_dir = os.path.abspath('persist')
print 'Running main loop'

View File

@ -11,6 +11,9 @@ def iambuttbot(bot, input):
if input.nick.lower() != 'buttbot':
return
if '@' in input or '#' in input:
return #prevent abuse
password = open('iambuttbot_password').readlines()[0].strip()
status = input.inp if len(input.inp) <= 140 else input.inp[:137] + "..."
data = urllib.urlencode({"status": status.encode('utf8')})

View File

@ -19,7 +19,7 @@ def twitter(bot, input):
try:
tweet = etree.parse(url)
except IOError:
return 'RoR: XTREME scalability (twitter is unresponsive)'
return 'RoR: XTREME scalability (twitter gave an error)'
if tweet.find('error') is not None:
return "can't find that username"

View File

@ -0,0 +1,28 @@
from lxml import html
import urllib
import hook
@hook.command('u')
@hook.command
def urban(inp):
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
if not inp.strip():
return urban.__doc__
url = 'http://www.urbandictionary.com/define.php?term=' + \
urllib.quote(inp.strip(), safe='')
page = html.parse(url)
defs = page.xpath("//div[@class='definition']")
print repr(defs[0].text_content())
if not defs:
return 'no definitions found'
out = ' '.join(defs[0].text_content().split())
if len(out) > 400:
out = out[:out.rfind(' ', 0, 400)] + '...'
return out