diff options
| author | Adam Tauber <asciimoo@gmail.com> | 2015-12-28 10:57:30 +0100 |
|---|---|---|
| committer | Adam Tauber <asciimoo@gmail.com> | 2015-12-28 10:57:30 +0100 |
| commit | c350def84bb5603f99859e3a5a9824cc356e613b (patch) | |
| tree | 6c44451715d7be3e4f97f345d951b890f99d2284 | |
| parent | 91c850ca6e810a7a999dd0d0bb5794758afe67d7 (diff) | |
| parent | 0bb403bb4740d20c8a158fef622919dcd30e6e69 (diff) | |
Merge pull request #478 from a01200356/master
Wolfram Alpha API
| -rw-r--r-- | searx/engines/wolframalpha_api.py | 60 | ||||
| -rw-r--r-- | searx/settings.yml | 8 |
2 files changed, 68 insertions, 0 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py new file mode 100644 index 000000000..d61d25747 --- /dev/null +++ b/searx/engines/wolframalpha_api.py @@ -0,0 +1,60 @@ +# Wolfram Alpha (Maths) +# +# @website http://www.wolframalpha.com +# @provide-api yes (http://api.wolframalpha.com/v2/) +# +# @using-api yes +# @results XML +# @stable yes +# @parse result + +from urllib import urlencode +from lxml import etree + +# search-url +base_url = 'http://api.wolframalpha.com/v2/query' +search_url = base_url + '?appid={api_key}&{query}&format=plaintext' +api_key = '' + + +# do search-request +def request(query, params): + params['url'] = search_url.format(query=urlencode({'input': query}), + api_key=api_key) + + return params + + +# replace private user area characters to make text legible +def replace_pua_chars(text): + pua_chars = {u'\uf74c': 'd', + u'\uf74d': u'\u212f', + u'\uf74e': 'i', + u'\uf7d9': '='} + + for k, v in pua_chars.iteritems(): + text = text.replace(k, v) + + return text + + +# get response from search-request +def response(resp): + results = [] + + search_results = etree.XML(resp.content) + + # return empty array if there are no results + if search_results.xpath('/queryresult[attribute::success="false"]'): + return [] + + # parse result + result = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')[0].text + result = replace_pua_chars(result) + + # append result + # TODO: shouldn't it bind the source too? + results.append({'answer': result}) + + # return results + return results diff --git a/searx/settings.yml b/searx/settings.yml index c7f659e5f..e23e4c390 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -300,6 +300,14 @@ engines: engine : vimeo 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 +# shortcut : wa +# engine : wolframalpha_api +# api_key: 'apikey' # required! +# timeout: 6.0 + #The blekko technology and team have joined IBM Watson! -> https://blekko.com/ # - name : blekko images # engine : blekko_images |