Procházet zdrojové kódy

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

marc před 8 roky
rodič
revize
09ee2aa69d

+ 8
- 2
searx/engines/wolframalpha_api.py Zobrazit soubor

@@ -22,6 +22,7 @@ answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
22 22
 input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
23 23
 pods_xpath = '//pod'
24 24
 subpods_xpath = './subpod'
25
+pod_primary_xpath = './@primary'
25 26
 pod_id_xpath = './@id'
26 27
 pod_title_xpath = './@title'
27 28
 plaintext_xpath = './plaintext'
@@ -78,10 +79,12 @@ def response(resp):
78 79
         infobox_title = None
79 80
 
80 81
     pods = search_results.xpath(pods_xpath)
82
+    result = ""
81 83
     result_chunks = []
82 84
     for pod in pods:
83 85
         pod_id = pod.xpath(pod_id_xpath)[0]
84 86
         pod_title = pod.xpath(pod_title_xpath)[0]
87
+        pod_is_result = pod.xpath(pod_primary_xpath)
85 88
 
86 89
         subpods = pod.xpath(subpods_xpath)
87 90
         if not subpods:
@@ -94,6 +97,9 @@ def response(resp):
94 97
 
95 98
             if content and pod_id not in image_pods:
96 99
 
100
+                if pod_is_result:
101
+                    result = content
102
+
97 103
                 # if no input pod was found, title is first plaintext pod
98 104
                 if not infobox_title:
99 105
                     infobox_title = content
@@ -116,7 +122,7 @@ def response(resp):
116 122
 
117 123
     # append link to site
118 124
     results.append({'url': resp.request.headers['Referer'].decode('utf8'),
119
-                    'title': 'Wolfram|Alpha',
120
-                    'content': infobox_title})
125
+                    'title': infobox_title + ' - Wolfram|Alpha',
126
+                    'content': result})
121 127
 
122 128
     return results

+ 7
- 2
searx/engines/wolframalpha_noapi.py Zobrazit soubor

@@ -81,9 +81,11 @@ def response(resp):
81 81
     # TODO handle resp_json['queryresult']['assumptions']
82 82
     result_chunks = []
83 83
     infobox_title = None
84
+    result = ""
84 85
     for pod in resp_json['queryresult']['pods']:
85 86
         pod_id = pod.get('id', '')
86 87
         pod_title = pod.get('title', '')
88
+        pod_is_result = pod.get('primary', None)
87 89
 
88 90
         if 'subpods' not in pod:
89 91
             continue
@@ -97,6 +99,9 @@ def response(resp):
97 99
                 if subpod['plaintext'] != '(requires interactivity)':
98 100
                     result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})
99 101
 
102
+                if pod_is_result:
103
+                    result = subpod['plaintext']
104
+
100 105
             elif 'img' in subpod:
101 106
                 result_chunks.append({'label': pod_title, 'image': subpod['img']})
102 107
 
@@ -108,7 +113,7 @@ def response(resp):
108 113
                     'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]})
109 114
 
110 115
     results.append({'url': resp.request.headers['Referer'].decode('utf8'),
111
-                    'title': 'Wolfram|Alpha',
112
-                    'content': infobox_title})
116
+                    'title': infobox_title + ' - Wolfram|Alpha',
117
+                    'content': result})
113 118
 
114 119
     return results

+ 4
- 2
tests/unit/engines/test_wolframalpha_api.py Zobrazit soubor

@@ -103,7 +103,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
103 103
         self.assertEqual(referer_url, results[0]['urls'][0]['url'])
104 104
         self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
105 105
         self.assertEqual(referer_url, results[1]['url'])
106
-        self.assertEqual('Wolfram|Alpha', results[1]['title'])
106
+        self.assertEqual('input_plaintext - Wolfram|Alpha', results[1]['title'])
107
+        self.assertIn('result_plaintext', results[1]['content'])
107 108
 
108 109
         # test calc
109 110
         xml = """<?xml version='1.0' encoding='UTF-8'?>
@@ -161,4 +162,5 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
161 162
         self.assertEqual(referer_url, results[0]['urls'][0]['url'])
162 163
         self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
163 164
         self.assertEqual(referer_url, results[1]['url'])
164
-        self.assertEqual('Wolfram|Alpha', results[1]['title'])
165
+        self.assertEqual('integral_plaintext - Wolfram|Alpha', results[1]['title'])
166
+        self.assertIn('integral_plaintext', results[1]['content'])

+ 4
- 2
tests/unit/engines/test_wolframalpha_noapi.py Zobrazit soubor

@@ -140,7 +140,8 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
140 140
         self.assertEqual(referer_url, results[0]['urls'][0]['url'])
141 141
         self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
142 142
         self.assertEqual(referer_url, results[1]['url'])
143
-        self.assertEqual('Wolfram|Alpha', results[1]['title'])
143
+        self.assertEqual('input_plaintext - Wolfram|Alpha', results[1]['title'])
144
+        self.assertIn('result_plaintext', results[1]['content'])
144 145
 
145 146
         # test calc
146 147
         json = r"""
@@ -219,4 +220,5 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
219 220
         self.assertEqual(referer_url, results[0]['urls'][0]['url'])
220 221
         self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
221 222
         self.assertEqual(referer_url, results[1]['url'])
222
-        self.assertEqual('Wolfram|Alpha', results[1]['title'])
223
+        self.assertEqual('integral_plaintext - Wolfram|Alpha', results[1]['title'])
224
+        self.assertIn('integral_plaintext', results[1]['content'])