h/plugins/urbandictionary.py

30 lines
726 B
Python
Raw Normal View History

2009-04-19 11:42:48 +00:00
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.strip():
return urban.__doc__
url = 'http://www.urbandictionary.com/define.php?term=' + \
urllib.quote(inp.strip(), safe='')
2009-04-19 11:42:48 +00:00
page = html.parse(url)
2009-07-21 20:13:35 +00:00
words = page.xpath("//td[@class='word']")
defs = page.xpath("//div[@class='definition']")
if not defs:
return 'no definitions found'
2009-07-21 20:14:29 +00:00
out = words[0].text_content().strip() + ': ' + ' '.join(
2009-07-21 20:13:35 +00:00
defs[0].text_content().split())
if len(out) > 400:
out = out[:out.rfind(' ', 0, 400)] + '...'
return out