summaryrefslogtreecommitdiff
path: root/searx/engines/wolframalpha_api.py
diff options
context:
space:
mode:
authora01200356 <a01200356@itesm.mx>2016-01-01 22:02:10 -0600
committera01200356 <a01200356@itesm.mx>2016-01-01 22:02:10 -0600
commit0871c7ca85cd19a2fa0971c7db28516a74255d5d (patch)
tree533fbb3dda61f45db2b7a970b831a91c819f7313 /searx/engines/wolframalpha_api.py
parentbe54e5269a982e272e2fe8a5064ed898373c9063 (diff)
downloadsearxng-0871c7ca85cd19a2fa0971c7db28516a74255d5d.tar.gz
searxng-0871c7ca85cd19a2fa0971c7db28516a74255d5d.zip
[enh] wolframalpha appends result
Diffstat (limited to 'searx/engines/wolframalpha_api.py')
-rw-r--r--searx/engines/wolframalpha_api.py28
1 files changed, 20 insertions, 8 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py
index 4c99eac95..6927f9707 100644
--- a/searx/engines/wolframalpha_api.py
+++ b/searx/engines/wolframalpha_api.py
@@ -14,14 +14,24 @@ from lxml import etree
# search-url
base_url = 'http://api.wolframalpha.com/v2/query'
search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
+site_url = 'http://www.wolframalpha.com/input/?{query}'
+search_query = ''
api_key = ''
+# xpath variables
+failure_xpath = '/queryresult[attribute::success="false"]'
+answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
+
# do search-request
def request(query, params):
params['url'] = search_url.format(query=urlencode({'input': query}),
api_key=api_key)
+ # used in response
+ global search_query
+ search_query = query
+
return params
@@ -45,19 +55,21 @@ def response(resp):
search_results = etree.XML(resp.content)
# return empty array if there are no results
- if search_results.xpath('/queryresult[attribute::success="false"]'):
+ if search_results.xpath(failure_xpath):
return []
# parse answer
- answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')
- if not answer:
- return results
+ answer = search_results.xpath(answer_xpath)
+ if answer:
+ answer = replace_pua_chars(answer[0].text)
+
+ results.append({'answer': answer})
- answer = replace_pua_chars(answer[0].text)
+ # result url
+ result_url = site_url.format(query=urlencode({'i': search_query}))
# append result
- # TODO: shouldn't it bind the source too?
- results.append({'answer': answer})
+ results.append({'url': result_url,
+ 'title': search_query + ' - Wolfram|Alpha'})
- # return results
return results