Pepping and tweaks

This commit is contained in:
Luke Rogers 2012-08-21 08:02:49 +12:00
parent 7bdbb376b8
commit 98930f8285

View file

@ -1,5 +1,7 @@
# Plugin by Lukeroge
from util import hook, http from util import hook, http
from util.text import truncate_words
base_url = 'http://www.urbandictionary.com/iphone/search/define'
@hook.command('u') @hook.command('u')
@ -7,9 +9,6 @@ from util import hook, http
def urban(inp): def urban(inp):
"urban <phrase> [id] -- Looks up <phrase> on urbandictionary.com." "urban <phrase> [id] -- Looks up <phrase> on urbandictionary.com."
# set a default definition number
id = 1
# clean and split the input # clean and split the input
input = inp.lower().strip() input = inp.lower().strip()
parts = input.split() parts = input.split()
@ -20,10 +19,11 @@ def urban(inp):
# remove the ID from the input string # remove the ID from the input string
del parts[-1] del parts[-1]
input = " ".join(parts) input = " ".join(parts)
else:
id = 1
# fetch the definitions # fetch the definitions
url = 'http://www.urbandictionary.com/iphone/search/define' page = http.get_json(base_url, term=input, referer="http://m.urbandictionary.com")
page = http.get_json(url, term=input, referer="http://m.urbandictionary.com")
defs = page['list'] defs = page['list']
if page['result_type'] == 'no_results': if page['result_type'] == 'no_results':
@ -31,13 +31,10 @@ def urban(inp):
# try getting the requested definition # try getting the requested definition
try: try:
out = "[%i/%i] %s: %s" % \ output = "[%i/%i] %s: %s" % \
(id, len(defs), defs[id - 1]['word'], (id, len(defs), defs[id - 1]['word'],
defs[id - 1]['definition'].replace('\r\n',' ')) defs[id - 1]['definition'].replace('\r\n', ' '))
except IndexError: except IndexError:
return 'Not found.' return 'Not found.'
if len(out) > 400: return truncate_words(output, 400)
out = out[:out.rfind(' ', 0, 400)] + '...'
return out