Browse Source

append link to result in wolframalpha_api (and the tests to validate that)

a01200356 9 years ago
parent
commit
8ca5744814
3 changed files with 34 additions and 41 deletions
  1. 13
    1
      searx/engines/wolframalpha_api.py
  2. 1
    1
      searx/settings.yml
  3. 20
    39
      searx/tests/engines/test_wolframalpha_api.py

+ 13
- 1
searx/engines/wolframalpha_api.py View File

@@ -10,15 +10,18 @@
10 10
 
11 11
 from urllib import urlencode
12 12
 from lxml import etree
13
+from re import search
13 14
 
14 15
 # search-url
15 16
 base_url = 'http://api.wolframalpha.com/v2/query'
16 17
 search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
18
+site_url = 'http://www.wolframalpha.com/input/?{query}'
17 19
 api_key = ''  # defined in settings.yml
18 20
 
19 21
 # xpath variables
20 22
 failure_xpath = '/queryresult[attribute::success="false"]'
21 23
 answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
24
+input_xpath = '//pod[starts-with(attribute::title, "Input")]/subpod/plaintext'
22 25
 
23 26
 
24 27
 # do search-request
@@ -60,6 +63,15 @@ def response(resp):
60 63
 
61 64
             results.append({'answer': answer})
62 65
 
63
-    # TODO: append a result with title and link, like in the no api version
66
+    # if there's no input section in search_results, check if answer has the input embedded (before their "=" sign)
67
+    try:
68
+        query_input = search_results.xpath(input_xpath)[0].text
69
+    except IndexError:
70
+        query_input = search(u'([^\uf7d9]+)', answers[0].text).group(1)
71
+
72
+    # append link to site
73
+    result_url = site_url.format(query=urlencode({'i': query_input.encode('utf-8')}))
74
+    results.append({'url': result_url,
75
+                    'title': query_input + " - Wolfram|Alpha"})
64 76
 
65 77
     return results

+ 1
- 1
searx/settings.yml View File

@@ -305,7 +305,7 @@ engines:
305 305
     # You can use the engine using the official stable API, but you need an API key
306 306
     # See : http://products.wolframalpha.com/api/
307 307
     #    engine : wolframalpha_api
308
-    #    api_key: '' # required!
308
+    #    api_key: 'apikey' # required!
309 309
     engine : wolframalpha_noapi
310 310
     timeout: 6.0
311 311
     disabled : True

+ 20
- 39
searx/tests/engines/test_wolframalpha_api.py View File

@@ -124,8 +124,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
124 124
         response = mock.Mock(content=xml)
125 125
         results = wolframalpha_api.response(response)
126 126
         self.assertEqual(type(results), list)
127
-        self.assertEqual(len(results), 1)
127
+        self.assertEqual(len(results), 2)
128 128
         self.assertIn('i', results[0]['answer'])
129
+        self.assertIn('sqrt(-1) - Wolfram|Alpha', results[1]['title'])
130
+        self.assertEquals('http://www.wolframalpha.com/input/?i=sqrt%28-1%29', results[1]['url'])
129 131
 
130 132
         xml = """<?xml version='1.0' encoding='UTF-8'?>
131 133
             <queryresult success='true'
@@ -223,8 +225,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
223 225
         response = mock.Mock(content=xml)
224 226
         results = wolframalpha_api.response(response)
225 227
         self.assertEqual(type(results), list)
226
-        self.assertEqual(len(results), 1)
228
+        self.assertEqual(len(results), 2)
227 229
         self.assertIn('log(x)+c', results[0]['answer'])
230
+        self.assertIn('∫1/xx - Wolfram|Alpha'.decode('utf-8'), results[1]['title'])
231
+        self.assertEquals('http://www.wolframalpha.com/input/?i=%E2%88%AB1%2Fx%EF%9D%8Cx', results[1]['url'])
228 232
 
229 233
         xml = """<?xml version='1.0' encoding='UTF-8'?>
230 234
         <queryresult success='true'
@@ -233,14 +237,14 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
233 237
             datatypes='Solve'
234 238
             timedout=''
235 239
             timedoutpods=''
236
-            timing='0.883'
237
-            parsetiming='0.337'
240
+            timing='0.79'
241
+            parsetiming='0.338'
238 242
             parsetimedout='false'
239 243
             recalculate=''
240
-            id='MSPa347225h1ea85fgfbgb4000064ff000d25g5df3f'
241
-            host='http://www5a.wolframalpha.com'
242
-            server='52'
243
-            related='http://www5a.wolframalpha.com/api/v2/relatedQueries.jsp?...'
244
+            id='MSPa7481f7i06d25h3deh2900004810i3a78d9b4fdc'
245
+            host='http://www5b.wolframalpha.com'
246
+            server='23'
247
+            related='http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
244 248
             version='2.6'>
245 249
          <pod title='Input interpretation'
246 250
              scanner='Identity'
@@ -249,12 +253,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
249 253
              error='false'
250 254
              numsubpods='1'>
251 255
           <subpod title=''>
252
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP349225h1ea85fgfbgb400005dhd93b9eegg8f32?...'
253
-               alt='solve x^2+x = 0'
254
-               title='solve x^2+x = 0'
255
-               width='157'
256
-               height='35' />
257
-           <plaintext>solve x^2+x = 0</plaintext>
256
+           <plaintext>solve x^2+x0</plaintext>
258 257
           </subpod>
259 258
          </pod>
260 259
          <pod title='Results'
@@ -265,20 +264,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
265 264
              numsubpods='2'
266 265
              primary='true'>
267 266
           <subpod title=''>
268
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP350225h1ea85fgfbgb400005b1ebcefaha3ac97?...'
269
-               alt='x = -1'
270
-               title='x = -1'
271
-               width='47'
272
-               height='18' />
273
-           <plaintext>x = -1</plaintext>
267
+           <plaintext>x-1</plaintext>
274 268
           </subpod>
275 269
           <subpod title=''>
276
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP351225h1ea85fgfbgb4000032fic0ig981hc936?...'
277
-               alt='x = 0'
278
-               title='x = 0'
279
-               width='36'
280
-               height='18' />
281
-           <plaintext>x = 0</plaintext>
270
+           <plaintext>x0</plaintext>
282 271
           </subpod>
283 272
           <states count='1'>
284 273
            <state name='Step-by-step solution'
@@ -292,11 +281,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
292 281
              error='false'
293 282
              numsubpods='1'>
294 283
           <subpod title=''>
295
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP352225h1ea85fgfbgb40000464054c665hc5dee?...'
296
-               alt=''
297
-               title=''
298
-               width='300'
299
-               height='181' />
300 284
            <plaintext></plaintext>
301 285
           </subpod>
302 286
          </pod>
@@ -307,11 +291,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
307 291
              error='false'
308 292
              numsubpods='1'>
309 293
           <subpod title=''>
310
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP353225h1ea85fgfbgb400005ab1c8aai366fe46?...'
311
-               alt=''
312
-               title=''
313
-               width='310'
314
-               height='36' />
315 294
            <plaintext></plaintext>
316 295
           </subpod>
317 296
          </pod>
@@ -321,6 +300,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
321 300
         response = mock.Mock(content=xml)
322 301
         results = wolframalpha_api.response(response)
323 302
         self.assertEqual(type(results), list)
324
-        self.assertEqual(len(results), 2)
325
-        self.assertIn('x = -1', results[0]['answer'])
326
-        self.assertIn('x = 0', results[1]['answer'])
303
+        self.assertEqual(len(results), 3)
304
+        self.assertIn('x=-1', results[0]['answer'])
305
+        self.assertIn('x=0', results[1]['answer'])
306
+        self.assertIn('solve x^2+x0 - Wolfram|Alpha'.decode('utf-8'), results[2]['title'])
307
+        self.assertEquals('http://www.wolframalpha.com/input/?i=solve+x%5E2%2Bx%EF%9F%990', results[2]['url'])