Bladeren bron

[fix] remove unnecesary async calls in wolframalpha_noapi

setting async to false in the request did the job, lol.
a01200356 9 jaren geleden
bovenliggende
commit
8f3b33de23

+ 2
- 54
searx/engines/wolframalpha_noapi.py Bestand weergeven

@@ -20,7 +20,7 @@ from searx.poolrequests import get as http_get
20 20
 url = 'https://www.wolframalpha.com/'
21 21
 
22 22
 search_url = url + 'input/json.jsp'\
23
-    '?async=true'\
23
+    '?async=false'\
24 24
     '&banners=raw'\
25 25
     '&debuggingdata=false'\
26 26
     '&format=image,plaintext,imagemap,minput,moutput'\
@@ -38,14 +38,6 @@ referer_url = url + 'input/?{query}'
38 38
 token = {'value': '',
39 39
          'last_updated': None}
40 40
 
41
-# xpath variables
42
-success_xpath = '/pod[attribute::error="false"]'
43
-plaintext_xpath = './plaintext'
44
-title_xpath = './@title'
45
-image_xpath = './img'
46
-img_src_xpath = './img/@src'
47
-img_alt_xpath = './img/@alt'
48
-
49 41
 # pods to display as image in infobox
50 42
 # this pods do return a plaintext, but they look better and are more useful as images
51 43
 image_pods = {'VisualRepresentation',
@@ -79,42 +71,6 @@ def request(query, params):
79 71
     return params
80 72
 
81 73
 
82
-# get additional pod
83
-# NOTE: this makes an additional requests to server, so the response will take longer and might reach timeout
84
-def get_async_pod(url):
85
-    try:
86
-        resp = http_get(url, timeout=2.0)
87
-    except:
88
-        return None
89
-
90
-    if resp:
91
-        return parse_async_pod(resp)
92
-
93
-
94
-def parse_async_pod(resp):
95
-    pod = {'subpods': []}
96
-
97
-    resp_pod = XML(resp.content)
98
-
99
-    if resp_pod.xpath(success_xpath):
100
-        for subpod in resp_pod:
101
-            new_subpod = {'title': subpod.xpath(title_xpath)[0]}
102
-
103
-            plaintext = subpod.xpath(plaintext_xpath)[0].text
104
-            if plaintext:
105
-                new_subpod['plaintext'] = plaintext
106
-            else:
107
-                new_subpod['plaintext'] = ''
108
-
109
-            if subpod.xpath(image_xpath):
110
-                new_subpod['img'] = {'src': subpod.xpath(img_src_xpath)[0],
111
-                                     'alt': subpod.xpath(img_alt_xpath)[0]}
112
-
113
-            pod['subpods'].append(new_subpod)
114
-
115
-    return pod
116
-
117
-
118 74
 # get response from search-request
119 75
 def response(resp):
120 76
     results = []
@@ -132,15 +88,7 @@ def response(resp):
132 88
         pod_title = pod.get('title', '')
133 89
 
134 90
         if 'subpods' not in pod:
135
-            # comment this section if your requests always reach timeout
136
-            if pod['async']:
137
-                result = get_async_pod(pod['async'])
138
-                if result:
139
-                    pod = result
140
-                else:
141
-                    continue
142
-            else:
143
-                continue
91
+            continue
144 92
 
145 93
         if pod_id == 'Input' or not infobox_title:
146 94
             infobox_title = pod['subpods'][0]['plaintext']

+ 16
- 16
tests/unit/engines/test_wolframalpha_api.py Bestand weergeven

@@ -87,16 +87,16 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
87 87
         results = wolframalpha_api.response(response)
88 88
         self.assertEqual(type(results), list)
89 89
         self.assertEqual(len(results), 2)
90
-        self.assertIn('input_plaintext', results[0]['infobox'])
90
+        self.assertEqual('input_plaintext', results[0]['infobox'])
91 91
 
92 92
         self.assertEqual(len(results[0]['attributes']), 3)
93
-        self.assertIn('Input', results[0]['attributes'][0]['label'])
94
-        self.assertIn('input_plaintext', results[0]['attributes'][0]['value'])
95
-        self.assertIn('Result', results[0]['attributes'][1]['label'])
96
-        self.assertIn('result_plaintext', results[0]['attributes'][1]['value'])
97
-        self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label'])
98
-        self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
99
-        self.assertIn('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
93
+        self.assertEqual('Input', results[0]['attributes'][0]['label'])
94
+        self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
95
+        self.assertEqual('Result', results[0]['attributes'][1]['label'])
96
+        self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
97
+        self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
98
+        self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
99
+        self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
100 100
 
101 101
         self.assertEqual(len(results[0]['urls']), 1)
102 102
 
@@ -129,8 +129,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
129 129
                     <plaintext>integral_plaintext</plaintext>
130 130
                 </subpod>
131 131
             </pod>
132
-            <pod title='Plot'
133
-                scanner='Plotter'
132
+            <pod title='Plot of the integral'
133
+                scanner='Integral'
134 134
                 id='Plot'
135 135
                 error='false'
136 136
                 numsubpods='1'>
@@ -147,14 +147,14 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
147 147
         results = wolframalpha_api.response(response)
148 148
         self.assertEqual(type(results), list)
149 149
         self.assertEqual(len(results), 2)
150
-        self.assertIn('integral_plaintext', results[0]['infobox'])
150
+        self.assertEqual('integral_plaintext', results[0]['infobox'])
151 151
 
152 152
         self.assertEqual(len(results[0]['attributes']), 2)
153
-        self.assertIn('Indefinite integral', results[0]['attributes'][0]['label'])
154
-        self.assertIn('integral_plaintext', results[0]['attributes'][0]['value'])
155
-        self.assertIn('Plot', results[0]['attributes'][1]['label'])
156
-        self.assertIn('plot.gif', results[0]['attributes'][1]['image']['src'])
157
-        self.assertIn('plot_alt', results[0]['attributes'][1]['image']['alt'])
153
+        self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
154
+        self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
155
+        self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
156
+        self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
157
+        self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
158 158
 
159 159
         self.assertEqual(len(results[0]['urls']), 1)
160 160
 

+ 28
- 44
tests/unit/engines/test_wolframalpha_noapi.py Bestand weergeven

@@ -124,16 +124,16 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
124 124
         results = wolframalpha_noapi.response(response)
125 125
         self.assertEqual(type(results), list)
126 126
         self.assertEqual(len(results), 2)
127
-        self.assertIn('input_plaintext', results[0]['infobox'])
127
+        self.assertEqual('input_plaintext', results[0]['infobox'])
128 128
 
129 129
         self.assertEqual(len(results[0]['attributes']), 3)
130
-        self.assertIn('Input', results[0]['attributes'][0]['label'])
131
-        self.assertIn('input_plaintext', results[0]['attributes'][0]['value'])
132
-        self.assertIn('Result', results[0]['attributes'][1]['label'])
133
-        self.assertIn('result_plaintext', results[0]['attributes'][1]['value'])
134
-        self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label'])
135
-        self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
136
-        self.assertIn('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
130
+        self.assertEqual('Input', results[0]['attributes'][0]['label'])
131
+        self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
132
+        self.assertEqual('Result', results[0]['attributes'][1]['label'])
133
+        self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
134
+        self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
135
+        self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
136
+        self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
137 137
 
138 138
         self.assertEqual(len(results[0]['urls']), 1)
139 139
 
@@ -184,8 +184,19 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
184 184
                     ],
185 185
                     "id" : "Plot",
186 186
                     "error" : false,
187
-                    "numsubpods" : 0,
188
-                    "async" : "invalid_async_url"
187
+                    "numsubpods" : 1,
188
+                    "subpods" : [
189
+                        {
190
+                            "title" : "",
191
+                            "img" : {
192
+                                "src" : "plot.gif",
193
+                                "alt" : "plot_alt",
194
+                                "title" : "plot_title"
195
+                            },
196
+                            "plaintext" : "",
197
+                            "minput" : "plot_minput"
198
+                        }
199
+                    ]
189 200
                 }
190 201
             ]
191 202
         }}
@@ -194,11 +205,14 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
194 205
         results = wolframalpha_noapi.response(response)
195 206
         self.assertEqual(type(results), list)
196 207
         self.assertEqual(len(results), 2)
197
-        self.assertIn('integral_plaintext', results[0]['infobox'])
208
+        self.assertEqual('integral_plaintext', results[0]['infobox'])
198 209
 
199
-        self.assertEqual(len(results[0]['attributes']), 1)
200
-        self.assertIn('Indefinite integral', results[0]['attributes'][0]['label'])
201
-        self.assertIn('integral_plaintext', results[0]['attributes'][0]['value'])
210
+        self.assertEqual(len(results[0]['attributes']), 2)
211
+        self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
212
+        self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
213
+        self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
214
+        self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
215
+        self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
202 216
 
203 217
         self.assertEqual(len(results[0]['urls']), 1)
204 218
 
@@ -206,33 +220,3 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
206 220
         self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
207 221
         self.assertEqual(referer_url, results[1]['url'])
208 222
         self.assertEqual('Wolfram|Alpha', results[1]['title'])
209
-
210
-    def test_parse_async_pod(self):
211
-        self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, None)
212
-        self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, [])
213
-        self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, '')
214
-        self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, '[]')
215
-
216
-        # test plot
217
-        xml = '''<?xml version='1.0' encoding='UTF-8'?>
218
-        <pod title='Plot'
219
-            scanner='Plot'
220
-            id='Plot'
221
-            error='false'
222
-            numsubpods='1'>
223
-            <subpod title=''>
224
-                <img src='plot_img_src.gif'
225
-                    alt='plot_img_alt'
226
-                    title='plot_img_title' />
227
-                <plaintext>plot_plaintext</plaintext>
228
-                <minput>plot_minput</minput>
229
-            </subpod>
230
-        </pod>
231
-        '''
232
-        response = mock.Mock(content=xml)
233
-        pod = wolframalpha_noapi.parse_async_pod(response)
234
-        self.assertEqual(len(pod['subpods']), 1)
235
-        self.assertEqual('', pod['subpods'][0]['title'])
236
-        self.assertEqual('plot_plaintext', pod['subpods'][0]['plaintext'])
237
-        self.assertEqual('plot_img_src.gif', pod['subpods'][0]['img']['src'])
238
-        self.assertEqual('plot_img_alt', pod['subpods'][0]['img']['alt'])