[enh] wolframalpha appends result

This commit is contained in:
a01200356 2016-01-01 22:02:10 -06:00
parent be54e5269a
commit 0871c7ca85
3 changed files with 43 additions and 18 deletions

View File

@ -14,14 +14,24 @@ from lxml import etree
# search-url # search-url
base_url = 'http://api.wolframalpha.com/v2/query' base_url = 'http://api.wolframalpha.com/v2/query'
search_url = base_url + '?appid={api_key}&{query}&format=plaintext' search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
site_url = 'http://www.wolframalpha.com/input/?{query}'
search_query = ''
api_key = '' api_key = ''
# xpath variables
failure_xpath = '/queryresult[attribute::success="false"]'
answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
# do search-request # do search-request
def request(query, params): def request(query, params):
params['url'] = search_url.format(query=urlencode({'input': query}), params['url'] = search_url.format(query=urlencode({'input': query}),
api_key=api_key) api_key=api_key)
# used in response
global search_query
search_query = query
return params return params
@ -45,19 +55,21 @@ def response(resp):
search_results = etree.XML(resp.content) search_results = etree.XML(resp.content)
# return empty array if there are no results # return empty array if there are no results
if search_results.xpath('/queryresult[attribute::success="false"]'): if search_results.xpath(failure_xpath):
return [] return []
# parse answer # parse answer
answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext') answer = search_results.xpath(answer_xpath)
if not answer: if answer:
return results
answer = replace_pua_chars(answer[0].text) answer = replace_pua_chars(answer[0].text)
# append result
# TODO: shouldn't it bind the source too?
results.append({'answer': answer}) results.append({'answer': answer})
# return results # result url
result_url = site_url.format(query=urlencode({'i': search_query}))
# append result
results.append({'url': result_url,
'title': search_query + ' - Wolfram|Alpha'})
return results return results

View File

@ -1,6 +1,7 @@
# WolframAlpha (Maths) # WolframAlpha (Maths)
# #
# @website http://www.wolframalpha.com/ # @website http://www.wolframalpha.com/
# @provide-api yes (http://api.wolframalpha.com/v2/)
# #
# @using-api no # @using-api no
# @results HTML # @results HTML
@ -14,12 +15,17 @@ from urllib import urlencode
# search-url # search-url
url = 'http://www.wolframalpha.com/' url = 'http://www.wolframalpha.com/'
search_url = url+'input/?{query}' search_url = url+'input/?{query}'
search_query = ''
# do search-request # do search-request
def request(query, params): def request(query, params):
params['url'] = search_url.format(query=urlencode({'i': query})) params['url'] = search_url.format(query=urlencode({'i': query}))
# used in response
global search_query
search_query = query
return params return params
@ -42,9 +48,7 @@ def response(resp):
except AttributeError: except AttributeError:
continue continue
if not line: if line:
return results
# extract answer from json # extract answer from json
answer = line[line.find('{'):line.rfind('}')+1] answer = line[line.find('{'):line.rfind('}')+1]
answer = loads(answer.encode('unicode-escape')) answer = loads(answer.encode('unicode-escape'))
@ -52,4 +56,12 @@ def response(resp):
results.append({'answer': answer}) results.append({'answer': answer})
# failed result
elif search('pfail', webpage):
return results
# append result
results.append({'url': request(search_query, {})['url'],
'title': search_query + ' - Wolfram|Alpha'})
return results return results

View File

@ -300,12 +300,13 @@ engines:
engine : vimeo engine : vimeo
shortcut : vm shortcut : vm
# You can use the engine using the official stable API, but you need an API key
# See : http://products.wolframalpha.com/api/
- name : wolframalpha - name : wolframalpha
shortcut : wa shortcut : wa
# You can use the engine using the official stable API, but you need an API key
# See : http://products.wolframalpha.com/api/
# engine : wolframalpha_api
# api_key: 'api_key' # required!
engine : wolframalpha_noapi engine : wolframalpha_noapi
# api_key: 'apikey' # required!
timeout: 6.0 timeout: 6.0
#The blekko technology and team have joined IBM Watson! -> https://blekko.com/ #The blekko technology and team have joined IBM Watson! -> https://blekko.com/