diff options
| author | Adam Tauber <asciimoo@gmail.com> | 2016-03-01 10:41:56 +0100 |
|---|---|---|
| committer | Adam Tauber <asciimoo@gmail.com> | 2016-03-01 10:41:56 +0100 |
| commit | 308613e586108fd7b713fc2a14f8200a0a781105 (patch) | |
| tree | 9584ab403f132083c480ae206213672f8bd42e58 /searx/engines/wolframalpha_api.py | |
| parent | 45b5073f3e10cad68ddda6a1a83d3273a3d734fc (diff) | |
| parent | 8f3b33de23090e6d11a5a29a239719bb6915d2ec (diff) | |
Merge pull request #513 from a01200356/wolframalpha
WolframAlpha infobox
Diffstat (limited to 'searx/engines/wolframalpha_api.py')
| -rw-r--r-- | searx/engines/wolframalpha_api.py | 99 |
1 files changed, 72 insertions, 27 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 303c6c165..4526c825f 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -1,43 +1,60 @@ -# Wolfram Alpha (Maths) +# Wolfram Alpha (Science) # -# @website http://www.wolframalpha.com -# @provide-api yes (http://api.wolframalpha.com/v2/) +# @website https://www.wolframalpha.com +# @provide-api yes (https://api.wolframalpha.com/v2/) # # @using-api yes # @results XML # @stable yes -# @parse result +# @parse url, infobox 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}' +search_url = 'https://api.wolframalpha.com/v2/query?appid={api_key}&{query}' +site_url = 'https://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' +input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext' +pods_xpath = '//pod' +subpods_xpath = './subpod' +pod_id_xpath = './@id' +pod_title_xpath = './@title' +plaintext_xpath = './plaintext' +image_xpath = './img' +img_src_xpath = './@src' +img_alt_xpath = './@alt' + +# pods to display as image in infobox +# this pods do return a plaintext, but they look better and are more useful as images +image_pods = {'VisualRepresentation', + 'Illustration'} # do search-request def request(query, params): params['url'] = search_url.format(query=urlencode({'input': query}), api_key=api_key) + params['headers']['Referer'] = site_url.format(query=urlencode({'i': 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': '='} + pua_chars = {u'\uf522': u'\u2192', # rigth arrow + u'\uf7b1': u'\u2115', # set of natural numbers + u'\uf7b4': u'\u211a', # set of rational numbers + u'\uf7b5': u'\u211d', # set of real numbers + u'\uf7bd': u'\u2124', # set of integer numbers + u'\uf74c': 'd', # differential + u'\uf74d': u'\u212f', # euler's number + u'\uf74e': 'i', # imaginary number + u'\uf7d9': '='} # equals sign for k, v in pua_chars.iteritems(): text = text.replace(k, v) @@ -55,23 +72,51 @@ def response(resp): if search_results.xpath(failure_xpath): return [] - # parse answers - answers = search_results.xpath(answer_xpath) - if answers: - for answer in answers: - answer = replace_pua_chars(answer.text) + try: + infobox_title = search_results.xpath(input_xpath)[0].text + except: + infobox_title = None - results.append({'answer': answer}) + pods = search_results.xpath(pods_xpath) + result_chunks = [] + for pod in pods: + pod_id = pod.xpath(pod_id_xpath)[0] + pod_title = pod.xpath(pod_title_xpath)[0] - # 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) + subpods = pod.xpath(subpods_xpath) + if not subpods: + continue + + # Appends either a text or an image, depending on which one is more suitable + for subpod in subpods: + content = subpod.xpath(plaintext_xpath)[0].text + image = subpod.xpath(image_xpath) + + if content and pod_id not in image_pods: + + # if no input pod was found, title is first plaintext pod + if not infobox_title: + infobox_title = content + + content = replace_pua_chars(content) + result_chunks.append({'label': pod_title, 'value': content}) + + elif image: + result_chunks.append({'label': pod_title, + 'image': {'src': image[0].xpath(img_src_xpath)[0], + 'alt': image[0].xpath(img_alt_xpath)[0]}}) + + if not result_chunks: + return [] + + # append infobox + results.append({'infobox': infobox_title, + 'attributes': result_chunks, + 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]}) # 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"}) + results.append({'url': resp.request.headers['Referer'].decode('utf8'), + 'title': 'Wolfram|Alpha', + 'content': infobox_title}) return results |