summaryrefslogtreecommitdiff
path: root/searx/engines/wolframalpha_api.py
diff options
context:
space:
mode:
authora01200356 <a01200356@itesm.mx>2016-01-05 21:47:31 -0600
committera01200356 <a01200356@itesm.mx>2016-01-05 21:47:31 -0600
commit8ca574481485847d5e0f47627d20c543c39b7b66 (patch)
tree0caba6b8fedf7c2dae6f6fc119ffaedf1ffd9d64 /searx/engines/wolframalpha_api.py
parent2a15944b58089d84a930f36b42c6ef60d4e629b3 (diff)
downloadsearxng-8ca574481485847d5e0f47627d20c543c39b7b66.tar.gz
searxng-8ca574481485847d5e0f47627d20c543c39b7b66.zip
append link to result in wolframalpha_api (and the tests to validate that)
Diffstat (limited to 'searx/engines/wolframalpha_api.py')
-rw-r--r--searx/engines/wolframalpha_api.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py
index 540d81351..303c6c165 100644
--- a/searx/engines/wolframalpha_api.py
+++ b/searx/engines/wolframalpha_api.py
@@ -10,15 +10,18 @@
from urllib import urlencode
from lxml import etree
+from re import search
# 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}'
api_key = '' # defined in settings.yml
# xpath variables
failure_xpath = '/queryresult[attribute::success="false"]'
answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
+input_xpath = '//pod[starts-with(attribute::title, "Input")]/subpod/plaintext'
# do search-request
@@ -60,6 +63,15 @@ def response(resp):
results.append({'answer': answer})
- # TODO: append a result with title and link, like in the no api version
+ # if there's no input section in search_results, check if answer has the input embedded (before their "=" sign)
+ try:
+ query_input = search_results.xpath(input_xpath)[0].text
+ except IndexError:
+ query_input = search(u'([^\uf7d9]+)', answers[0].text).group(1)
+
+ # append link to site
+ result_url = site_url.format(query=urlencode({'i': query_input.encode('utf-8')}))
+ results.append({'url': result_url,
+ 'title': query_input + " - Wolfram|Alpha"})
return results