Extended formatting library
This commit is contained in:
parent
ddee3b6ab1
commit
c0ebc097b1
4 changed files with 57 additions and 18 deletions
|
@ -5,6 +5,7 @@ from util import hook, http
|
|||
|
||||
from urllib2 import HTTPError
|
||||
from util.web import bitly, ShortenError
|
||||
from util.formatting import truncate_words
|
||||
|
||||
|
||||
@hook.command('wa')
|
||||
|
@ -15,14 +16,14 @@ def wolframalpha(inp, bot=None):
|
|||
api_key = bot.config.get("api_keys", {}).get("wolframalpha", None)
|
||||
bitly_user = bot.config.get("api_keys", {}).get("bitly_user", None)
|
||||
bitly_key = bot.config.get("api_keys", {}).get("bitly_api", None)
|
||||
|
||||
|
||||
if not api_key:
|
||||
return "error: missing api key"
|
||||
|
||||
url = 'http://api.wolframalpha.com/v2/query?format=plaintext'
|
||||
|
||||
result = http.get_xml(url, input=inp, appid=api_key)
|
||||
|
||||
|
||||
# get the URL for a user to view this query in a browser
|
||||
query_url = "http://www.wolframalpha.com/input/?i=" + \
|
||||
urllib.quote(inp.encode('utf-8'))
|
||||
|
@ -46,7 +47,7 @@ def wolframalpha(inp, bot=None):
|
|||
if results:
|
||||
pod_texts.append(title + ': ' + ','.join(results))
|
||||
|
||||
ret = ' | '.join(pod_texts)
|
||||
ret = ' - '.join(pod_texts)
|
||||
|
||||
if not pod_texts:
|
||||
return 'No results.'
|
||||
|
@ -58,10 +59,8 @@ def wolframalpha(inp, bot=None):
|
|||
|
||||
ret = re.sub(r'\\:([0-9a-z]{4})', unicode_sub, ret)
|
||||
|
||||
if len(ret) > 410:
|
||||
ret = ret[:ret.rfind(' ', 0, 410)]
|
||||
ret = re.sub(r'\W+$', '', ret) + '...'
|
||||
|
||||
ret = truncate_words(ret, 410)
|
||||
|
||||
if not ret:
|
||||
return 'No results.'
|
||||
|
||||
|
|
Reference in a new issue