further pepping, spelling and other such things

This commit is contained in:
Luke Rogers 2014-02-13 15:02:44 +13:00
parent 6cc7554cd8
commit cd5ae1d32b
12 changed files with 52 additions and 56 deletions

View file

@ -10,37 +10,36 @@ def urban(inp):
"""urban <phrase> [id] -- Looks up <phrase> on urbandictionary.com."""
# clean and split the input
input = inp.lower().strip()
parts = input.split()
inp = inp.lower().strip()
parts = inp.split()
# if the last word is a number, set the ID to that number
if parts[-1].isdigit():
id = int(parts[-1])
id_num = int(parts[-1])
# remove the ID from the input string
del parts[-1]
input = " ".join(parts)
inp = " ".join(parts)
else:
id = 1
id_num = 1
# fetch the definitions
page = http.get_json(base_url, term=input, referer="http://m.urbandictionary.com")
defs = page['list']
print page
page = http.get_json(base_url, term=inp, referer="http://m.urbandictionary.com")
definitions = page['list']
if page['result_type'] == 'no_results':
return 'Not found.'
# try getting the requested definition
try:
definition = defs[id - 1]['definition'].replace('\r\n', ' ')
definition = definitions[id_num - 1]['definition'].replace('\r\n', ' ')
definition = re.sub('\s+', ' ', definition).strip() # remove excess spaces
definition = text.truncate_str(definition, 200)
except IndexError:
return 'Not found.'
url = defs[id - 1]['permalink']
url = definitions[id_num - 1]['permalink']
output = u"[%i/%i] %s :: %s" % \
(id, len(defs), definition, url)
(id_num, len(definitions), definition, url)
return output