1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-06 05:10:11 +01:00
searxng/searx/engines/wolframalpha_api.py

61 lines
1.4 KiB
Python
Raw Normal View History

2015-12-23 07:01:00 +01:00
# 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'
2015-12-28 08:17:42 +01:00
api_key = ''
2015-12-23 07:01:00 +01:00
# do search-request
def request(query, params):
params['url'] = search_url.format(query=urlencode({'input': query}),
api_key=api_key)
return params
2015-12-28 08:17:42 +01:00
2015-12-23 07:01:00 +01:00
# replace private user area characters to make text legible
def replace_pua_chars(text):
2015-12-28 08:17:42 +01:00
pua_chars = {u'\uf74c': 'd',
u'\uf74d': u'\u212f',
u'\uf74e': 'i',
u'\uf7d9': '='}
2015-12-23 07:01:00 +01:00
for k, v in pua_chars.iteritems():
text = text.replace(k, v)
return text
2015-12-28 08:17:42 +01:00
2015-12-23 07:01:00 +01:00
# 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?
2015-12-28 08:17:42 +01:00
results.append({'answer': result})
2015-12-23 07:01:00 +01:00
# return results
return results