2010-04-25 21:39:31 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from util import hook, http
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command('u')
|
|
|
|
@hook.command
|
|
|
|
def urban(inp):
|
|
|
|
'''.u/.urban <phrase> -- looks up <phrase> on urbandictionary.com'''
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2012-02-10 21:04:05 +00:00
|
|
|
url = 'http://www.urbandictionary.com/iphone/search/define'
|
|
|
|
page = http.get_json(url, term=inp)
|
|
|
|
defs = page['list']
|
2010-04-25 21:39:31 +00:00
|
|
|
|
2012-02-10 21:04:05 +00:00
|
|
|
if page['result_type'] == 'no_results':
|
|
|
|
return 'not found.'
|
2010-04-25 21:39:31 +00:00
|
|
|
|
2012-02-10 21:04:05 +00:00
|
|
|
out = defs[0]['word'] + ': ' + defs[0]['definition']
|
2010-04-25 21:39:31 +00:00
|
|
|
|
|
|
|
if len(out) > 400:
|
|
|
|
out = out[:out.rfind(' ', 0, 400)] + '...'
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
# define plugin by GhettoWizard & Scaevolus
|
2010-05-07 23:16:44 +00:00
|
|
|
@hook.command('dictionary')
|
2010-04-25 21:39:31 +00:00
|
|
|
@hook.command
|
|
|
|
def define(inp):
|
2010-05-07 23:16:44 +00:00
|
|
|
".define/.dictionary <word> -- fetches definition of <word>"
|
2010-04-25 21:39:31 +00:00
|
|
|
|
|
|
|
url = 'http://ninjawords.com/'
|
|
|
|
|
|
|
|
h = http.get_html(url + http.quote_plus(inp))
|
|
|
|
|
|
|
|
definition = h.xpath('//dd[@class="article"] | '
|
|
|
|
'//div[@class="definition"] |'
|
|
|
|
'//div[@class="example"]')
|
|
|
|
|
|
|
|
if not definition:
|
|
|
|
return 'No results for ' + inp
|
|
|
|
|
|
|
|
def format_output(show_examples):
|
2010-07-08 17:22:43 +00:00
|
|
|
result = '%s: ' % h.xpath('//dt[@class="title-word"]/a/text()')[0]
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-07-08 17:22:43 +00:00
|
|
|
correction = h.xpath('//span[@class="correct-word"]/text()')
|
2010-04-25 21:39:31 +00:00
|
|
|
if correction:
|
2010-07-08 17:22:43 +00:00
|
|
|
result = 'definition for "%s": ' % correction[0]
|
2010-04-25 21:39:31 +00:00
|
|
|
|
|
|
|
sections = []
|
|
|
|
for section in definition:
|
|
|
|
if section.attrib['class'] == 'article':
|
|
|
|
sections += [[section.text_content() + ': ']]
|
|
|
|
elif section.attrib['class'] == 'example':
|
|
|
|
if show_examples:
|
|
|
|
sections[-1][-1] += ' ' + section.text_content()
|
|
|
|
else:
|
|
|
|
sections[-1] += [section.text_content()]
|
|
|
|
|
|
|
|
for article in sections:
|
|
|
|
result += article[0]
|
|
|
|
if len(article) > 2:
|
|
|
|
result += ' '.join('%d. %s' % (n + 1, section)
|
|
|
|
for n, section in enumerate(article[1:]))
|
|
|
|
else:
|
|
|
|
result += article[1] + ' '
|
|
|
|
|
|
|
|
synonyms = h.xpath('//dd[@class="synonyms"]')
|
|
|
|
if synonyms:
|
|
|
|
result += synonyms[0].text_content()
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-04-25 21:39:31 +00:00
|
|
|
result = re.sub(r'\s+', ' ', result)
|
|
|
|
result = re.sub('\xb0', '', result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
result = format_output(True)
|
|
|
|
if len(result) > 450:
|
|
|
|
result = format_output(False)
|
|
|
|
|
|
|
|
if len(result) > 450:
|
|
|
|
result = result[:result.rfind(' ', 0, 450)]
|
|
|
|
result = re.sub(r'[^A-Za-z]+\.?$', '', result) + ' ...'
|
|
|
|
|
|
|
|
return result
|
2010-05-11 22:48:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command('e')
|
|
|
|
@hook.command
|
|
|
|
def etymology(inp):
|
|
|
|
".e/.etymology <word> -- Retrieves the etymology of chosen word"
|
|
|
|
|
|
|
|
url = 'http://www.etymonline.com/index.php'
|
|
|
|
|
|
|
|
h = http.get_html(url, term=inp)
|
|
|
|
|
|
|
|
etym = h.xpath('//dl')
|
|
|
|
|
|
|
|
if not etym:
|
|
|
|
return 'No etymology found for ' + inp
|
|
|
|
|
|
|
|
etym = etym[0].text_content()
|
2010-08-30 03:35:27 +00:00
|
|
|
|
2010-05-11 22:48:05 +00:00
|
|
|
etym = ' '.join(etym.split())
|
|
|
|
|
|
|
|
if len(etym) > 400:
|
|
|
|
etym = etym[:etym.rfind(' ', 0, 400)] + ' ...'
|
|
|
|
|
|
|
|
return etym
|