summaryrefslogtreecommitdiff
path: root/searx/engines/wolframalpha_api.py
diff options
context:
space:
mode:
authora01200356 <a01200356@itesm.mx>2015-12-23 00:01:00 -0600
committera01200356 <a01200356@itesm.mx>2015-12-23 00:01:00 -0600
commit4578575c284584a58cce0acd85f86bef2f49d77f (patch)
tree0eb5e147f4a4525d4e3a08d6b6c127d7d4d34e14 /searx/engines/wolframalpha_api.py
parent47b4a85fe33cfbb1c83556bdf3c3a6cdfdb178ab (diff)
downloadsearxng-4578575c284584a58cce0acd85f86bef2f49d77f.tar.gz
searxng-4578575c284584a58cce0acd85f86bef2f49d77f.zip
Wolfie kinda works using API
Diffstat (limited to 'searx/engines/wolframalpha_api.py')
-rw-r--r--searx/engines/wolframalpha_api.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py
new file mode 100644
index 000000000..1cf908b62
--- /dev/null
+++ b/searx/engines/wolframalpha_api.py
@@ -0,0 +1,70 @@
+# 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
+from searx.engines.xpath import extract_text
+from searx.utils import html_to_text
+
+# search-url
+base_url = 'http://api.wolframalpha.com/v2/query'
+search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
+site_url = 'http://wolframalpha.com/input/?{query}'
+
+#embedded_url = '<iframe width="540" height="304" ' +\
+# 'data-src="//www.youtube-nocookie.com/embed/{videoid}" ' +\
+# 'frameborder="0" allowfullscreen></iframe>'
+
+# do search-request
+def request(query, params):
+ params['url'] = search_url.format(query=urlencode({'input': query}),
+ api_key=api_key)
+
+ # need this for url in response
+ global my_query
+ my_query = query
+
+ 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)
+
+ # bind url from site
+ result_url = site_url.format(query=urlencode({'i': my_query}))
+
+ # append result
+ results.append({'url': result_url,
+ 'title': result})
+
+ # return results
+ return results