add .dict/.define command, respond to CTCP VERSION

This commit is contained in:
Ryan Hitchman 2010-04-13 20:57:52 -06:00
parent 04b1a718f1
commit 63fea08924
3 changed files with 75 additions and 29 deletions

62
plugins/dict.py Normal file
View File

@ -0,0 +1,62 @@
import re
import urllib
from lxml import html
from util import hook
@hook.command('u')
@hook.command
def urban(inp):
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
if not inp:
return urban.__doc__
url = 'http://www.urbandictionary.com/define.php?term=' + \
urllib.quote(inp, safe='')
page = html.parse(url)
words = page.xpath("//td[@class='word']")
defs = page.xpath("//div[@class='definition']")
if not defs:
return 'no definitions found'
out = words[0].text_content().strip() + ': ' + ' '.join(
defs[0].text_content().split())
if len(out) > 400:
out = out[:out.rfind(' ', 0, 400)] + '...'
return out
## A dictionary look-up plugin for Skybot made by Ghetto Wizard and Scaevolus
@hook.command('dict')
@hook.command
def define(inp):
".define/.dict <word> -- fetches definition of <word>"
if not inp:
return define.__doc__
base_url = 'http://dictionary.reference.com/browse/'
raw_data = urllib.urlopen(base_url + urllib.quote(inp, '')).read()
raw_data = raw_data.decode('utf-8')
definition = html.fromstring(raw_data).xpath('//span[@class="dnindex"]'
' | //div[@class="dndata"]')
if not definition:
return 'No results for ' + inp
result = ' '.join(section.text_content() for section in definition)
result = re.sub(r'\s+', ' ', result)
if len(result) > 400:
result = result[:result.rfind(' ', 0, 400)]
result = re.sub(r'[^A-Za-z]+\.?$', '', result) + ' ...' #truncate
return result

View File

@ -1,4 +1,6 @@
import re
import socket
import subprocess
import time
from util import hook
@ -39,3 +41,14 @@ def onjoin(paraml, conn=None):
for channel in conn.channels:
conn.join(channel)
time.sleep(1) # don't flood JOINs
@hook.regex(r'^\x01VERSION\x01$')
def version(inp, notice=None):
p = subprocess.Popen(['hg', 'id', '-n'], stdout=subprocess.PIPE)
stdout, _ = p.communicate()
p.wait()
ret = 'r' + stdout[:-2]
notice('\x01VERSION skybot %s - http://bitbucket.org/Scaevolus/'
'skybot/\x01' % ret)

View File

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