2012-04-02 18:17:55 +02:00
|
|
|
# Plugin by GhettoWizard and Scaevolus
|
2011-11-20 10:23:31 +01:00
|
|
|
import re
|
2012-04-02 18:17:55 +02:00
|
|
|
from util import hook
|
|
|
|
from util import http
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
@hook.command('u')
|
|
|
|
@hook.command
|
|
|
|
def urban(inp):
|
2012-03-19 21:01:31 +01:00
|
|
|
".urban <phrase> [id] -- Looks up <phrase> on urbandictionary.com."
|
2012-04-02 18:17:55 +02:00
|
|
|
|
2012-03-19 21:01:31 +01:00
|
|
|
# set a default definition number
|
|
|
|
id = 1
|
|
|
|
|
|
|
|
# clean and split the input
|
|
|
|
input = inp.lower().strip()
|
|
|
|
parts = input.split()
|
|
|
|
|
|
|
|
# if the last word is a number, set the ID to that number
|
|
|
|
if parts[-1].isdigit():
|
|
|
|
id = int(parts[-1])
|
|
|
|
del parts[-1]
|
|
|
|
input = " ".join(parts)
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-02-15 19:01:41 +01:00
|
|
|
url = 'http://www.urbandictionary.com/iphone/search/define'
|
2012-03-19 21:01:31 +01:00
|
|
|
page = http.get_json(url, term=input)
|
2012-02-15 19:01:41 +01:00
|
|
|
defs = page['list']
|
2011-11-20 10:23:31 +01:00
|
|
|
|
2012-02-15 19:01:41 +01:00
|
|
|
if page['result_type'] == 'no_results':
|
2012-02-24 04:17:59 +01:00
|
|
|
return 'Not found.'
|
2012-04-02 18:17:55 +02:00
|
|
|
|
2012-03-19 21:01:31 +01:00
|
|
|
# try getting the requested definition
|
|
|
|
try:
|
2012-03-21 03:41:44 +01:00
|
|
|
out = "[%s/%s] %s: %s" % \
|
2012-04-02 18:17:55 +02:00
|
|
|
(str(id), str(len(defs)), defs[id - 1]['word'],
|
|
|
|
defs[id - 1]['definition'])
|
2012-03-19 21:01:31 +01:00
|
|
|
except IndexError:
|
|
|
|
return 'Not found.'
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
if len(out) > 400:
|
|
|
|
out = out[:out.rfind(' ', 0, 400)] + '...'
|
|
|
|
|
|
|
|
return out
|
|
|
|
|
2012-02-29 09:29:53 +01:00
|
|
|
|
2011-11-20 10:23:31 +01:00
|
|
|
@hook.command('dictionary')
|
|
|
|
@hook.command
|
|
|
|
def define(inp):
|
2012-02-28 03:03:43 +01:00
|
|
|
".define <word> -- Fetches definition of <word>."
|
2011-11-20 10:23:31 +01: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):
|
|
|
|
result = '%s: ' % h.xpath('//dt[@class="title-word"]/a/text()')[0]
|
|
|
|
|
|
|
|
correction = h.xpath('//span[@class="correct-word"]/text()')
|
|
|
|
if correction:
|
2012-02-24 04:17:59 +01:00
|
|
|
result = 'Definition for "%s": ' % correction[0]
|
2011-11-20 10:23:31 +01: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()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command('e')
|
|
|
|
@hook.command
|
|
|
|
def etymology(inp):
|
2012-02-28 03:03:43 +01:00
|
|
|
".etymology <word> -- Retrieves the etymology of <word>."
|
2011-11-20 10:23:31 +01:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
etym = ' '.join(etym.split())
|
|
|
|
|
|
|
|
if len(etym) > 400:
|
|
|
|
etym = etym[:etym.rfind(' ', 0, 400)] + ' ...'
|
|
|
|
|
|
|
|
return etym
|