logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git
commit: 09ee2aa69dbd4815e0e1e1de53f3571972e04903
parent 390ad59bfcd256e9145d7ef539acaf3a83a73c8b
Author: marc <a01200356@itesm.mx>
Date:   Wed,  6 Jul 2016 17:29:40 -0500

[fix] Result text in Wolfram|Alpha (#607)

Diffstat:

Msearx/engines/wolframalpha_api.py10++++++++--
Msearx/engines/wolframalpha_noapi.py9+++++++--
Mtests/unit/engines/test_wolframalpha_api.py6++++--
Mtests/unit/engines/test_wolframalpha_noapi.py6++++--
4 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py @@ -22,6 +22,7 @@ answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext' input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext' pods_xpath = '//pod' subpods_xpath = './subpod' +pod_primary_xpath = './@primary' pod_id_xpath = './@id' pod_title_xpath = './@title' plaintext_xpath = './plaintext' @@ -78,10 +79,12 @@ def response(resp): infobox_title = None pods = search_results.xpath(pods_xpath) + result = "" result_chunks = [] for pod in pods: pod_id = pod.xpath(pod_id_xpath)[0] pod_title = pod.xpath(pod_title_xpath)[0] + pod_is_result = pod.xpath(pod_primary_xpath) subpods = pod.xpath(subpods_xpath) if not subpods: @@ -94,6 +97,9 @@ def response(resp): if content and pod_id not in image_pods: + if pod_is_result: + result = content + # if no input pod was found, title is first plaintext pod if not infobox_title: infobox_title = content @@ -116,7 +122,7 @@ def response(resp): # append link to site results.append({'url': resp.request.headers['Referer'].decode('utf8'), - 'title': 'Wolfram|Alpha', - 'content': infobox_title}) + 'title': infobox_title + ' - Wolfram|Alpha', + 'content': result}) return results diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py @@ -81,9 +81,11 @@ def response(resp): # TODO handle resp_json['queryresult']['assumptions'] result_chunks = [] infobox_title = None + result = "" for pod in resp_json['queryresult']['pods']: pod_id = pod.get('id', '') pod_title = pod.get('title', '') + pod_is_result = pod.get('primary', None) if 'subpods' not in pod: continue @@ -97,6 +99,9 @@ def response(resp): if subpod['plaintext'] != '(requires interactivity)': result_chunks.append({'label': pod_title, 'value': subpod['plaintext']}) + if pod_is_result: + result = subpod['plaintext'] + elif 'img' in subpod: result_chunks.append({'label': pod_title, 'image': subpod['img']}) @@ -108,7 +113,7 @@ def response(resp): 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]}) results.append({'url': resp.request.headers['Referer'].decode('utf8'), - 'title': 'Wolfram|Alpha', - 'content': infobox_title}) + 'title': infobox_title + ' - Wolfram|Alpha', + 'content': result}) return results diff --git a/tests/unit/engines/test_wolframalpha_api.py b/tests/unit/engines/test_wolframalpha_api.py @@ -103,7 +103,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase): self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) - self.assertEqual('Wolfram|Alpha', results[1]['title']) + self.assertEqual('input_plaintext - Wolfram|Alpha', results[1]['title']) + self.assertIn('result_plaintext', results[1]['content']) # test calc xml = """<?xml version='1.0' encoding='UTF-8'?> @@ -161,4 +162,5 @@ class TestWolframAlphaAPIEngine(SearxTestCase): self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) - self.assertEqual('Wolfram|Alpha', results[1]['title']) + self.assertEqual('integral_plaintext - Wolfram|Alpha', results[1]['title']) + self.assertIn('integral_plaintext', results[1]['content']) diff --git a/tests/unit/engines/test_wolframalpha_noapi.py b/tests/unit/engines/test_wolframalpha_noapi.py @@ -140,7 +140,8 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) - self.assertEqual('Wolfram|Alpha', results[1]['title']) + self.assertEqual('input_plaintext - Wolfram|Alpha', results[1]['title']) + self.assertIn('result_plaintext', results[1]['content']) # test calc json = r""" @@ -219,4 +220,5 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): self.assertEqual(referer_url, results[0]['urls'][0]['url']) self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) - self.assertEqual('Wolfram|Alpha', results[1]['title']) + self.assertEqual('integral_plaintext - Wolfram|Alpha', results[1]['title']) + self.assertIn('integral_plaintext', results[1]['content'])