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 import random
from util import hook, http from util import hook, http, text
from util.text import truncate_words
def api_get(kind, query): def api_get(kind, query):
@ -43,13 +41,15 @@ def google(inp):
result = parsed['responseData']['results'][0] result = parsed['responseData']['results'][0]
title = http.unescape(result['titleNoFormatting']) title = http.unescape(result['titleNoFormatting'])
title = text.truncate_str(title, 60)
content = http.unescape(result['content']) content = http.unescape(result['content'])
if not content: if not content:
content = "No description available." content = "No description available."
else: else:
content = http.html.fromstring(content).text_content() content = http.html.fromstring(content).text_content()
content = text.truncate_str(content, 150)
out = '%s -- \x02%s\x02: "%s"' % (result['unescapedUrl'], title, content) 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 import hook, http, text
from util.text import truncate_words
base_url = 'http://www.urbandictionary.com/iphone/search/define' base_url = 'http://www.urbandictionary.com/iphone/search/define'
@ -31,10 +30,10 @@ def urban(inp):
# try getting the requested definition # try getting the requested definition
try: try:
output = "[%i/%i] %s: %s" % \ output = u"[%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.'
return truncate_words(output, 400) return text.truncate_str(output, 250)

View file

@ -3,8 +3,7 @@ Scaevolus 2009'''
import re import re
from util import hook, http from util import hook, http, text
from util.text import truncate_words
api_prefix = "http://en.wikipedia.org/w/api.php" 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 = 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, ':/')) return '%s -- %s' % (desc, http.quote(url, ':/'))

View file

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