Renamed function in ./util/text.py, adjusted some values for cutting off text.

This commit is contained in:
Luke Rogers 2012-10-13 11:49:42 +13:00
parent f799e477e3
commit 28345b9c56
4 changed files with 15 additions and 22 deletions

View file

@ -1,7 +1,5 @@
import random
from util import hook, http
from util.text import truncate_words
from util import hook, http, text
def api_get(kind, query):
@ -43,13 +41,15 @@ def google(inp):
result = parsed['responseData']['results'][0]
title = http.unescape(result['titleNoFormatting'])
title = text.truncate_str(title, 60)
content = http.unescape(result['content'])
if not content:
content = "No description available."
else:
content = http.html.fromstring(content).text_content()
content = text.truncate_str(content, 150)
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content)
return truncate_words(out, 300)
return out

View file

@ -1,5 +1,4 @@
from util import hook, http
from util.text import truncate_words
from util import hook, http, text
base_url = 'http://www.urbandictionary.com/iphone/search/define'
@ -31,10 +30,10 @@ def urban(inp):
# try getting the requested definition
try:
output = "[%i/%i] %s: %s" % \
output = u"[%i/%i] %s: %s" % \
(id, len(defs), defs[id - 1]['word'],
defs[id - 1]['definition'].replace('\r\n', ' '))
except IndexError:
return 'Not found.'
return truncate_words(output, 400)
return text.truncate_str(output, 250)

View file

@ -3,8 +3,7 @@ Scaevolus 2009'''
import re
from util import hook, http
from util.text import truncate_words
from util import hook, http, text
api_prefix = "http://en.wikipedia.org/w/api.php"
@ -45,6 +44,6 @@ def wiki(inp):
desc = re.sub('\s+', ' ', desc).strip() # remove excess spaces
desc = truncate_words(desc, 300)
desc = text.truncate_str(desc, 250)
return '%s -- %s' % (desc, http.quote(url, ':/'))

View file

@ -1,11 +1,6 @@
import re
from util import hook, http
from urllib import quote_plus
from urllib2 import HTTPError
from util.web import isgd
from util.text import truncate_words
from util import hook, http, text, web
@hook.command('wa')
@ -24,10 +19,10 @@ def wolframalpha(inp, bot=None):
# get the URL for a user to view this query in a browser
query_url = "http://www.wolframalpha.com/input/?i=" + \
quote_plus(inp.encode('utf-8'))
http.quote_plus(inp.encode('utf-8'))
try:
short_url = isgd(query_url)
except (HTTPError):
short_url = web.isgd(query_url)
except (http.HTTPError):
short_url = query_url
pod_texts = []
@ -57,8 +52,8 @@ def wolframalpha(inp, bot=None):
ret = re.sub(r'\\:([0-9a-z]{4})', unicode_sub, ret)
ret = truncate_words(ret, 410)
ret = text.truncate_str(ret, 250)
if not ret:
return 'No results.'