Bläddra i källkod

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

a01200356 9 år sedan
förälder
incheckning
8ca5744814
3 ändrade filer med 34 tillägg och 41 borttagningar
  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 Visa fil

10
 
10
 
11
 from urllib import urlencode
11
 from urllib import urlencode
12
 from lxml import etree
12
 from lxml import etree
13
+from re import search
13
 
14
 
14
 # search-url
15
 # search-url
15
 base_url = 'http://api.wolframalpha.com/v2/query'
16
 base_url = 'http://api.wolframalpha.com/v2/query'
16
 search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17
 search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
18
+site_url = 'http://www.wolframalpha.com/input/?{query}'
17
 api_key = ''  # defined in settings.yml
19
 api_key = ''  # defined in settings.yml
18
 
20
 
19
 # xpath variables
21
 # xpath variables
20
 failure_xpath = '/queryresult[attribute::success="false"]'
22
 failure_xpath = '/queryresult[attribute::success="false"]'
21
 answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
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
 # do search-request
27
 # do search-request
60
 
63
 
61
             results.append({'answer': answer})
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
     return results
77
     return results

+ 1
- 1
searx/settings.yml Visa fil

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

+ 20
- 39
searx/tests/engines/test_wolframalpha_api.py Visa fil

124
         response = mock.Mock(content=xml)
124
         response = mock.Mock(content=xml)
125
         results = wolframalpha_api.response(response)
125
         results = wolframalpha_api.response(response)
126
         self.assertEqual(type(results), list)
126
         self.assertEqual(type(results), list)
127
-        self.assertEqual(len(results), 1)
127
+        self.assertEqual(len(results), 2)
128
         self.assertIn('i', results[0]['answer'])
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
         xml = """<?xml version='1.0' encoding='UTF-8'?>
132
         xml = """<?xml version='1.0' encoding='UTF-8'?>
131
             <queryresult success='true'
133
             <queryresult success='true'
223
         response = mock.Mock(content=xml)
225
         response = mock.Mock(content=xml)
224
         results = wolframalpha_api.response(response)
226
         results = wolframalpha_api.response(response)
225
         self.assertEqual(type(results), list)
227
         self.assertEqual(type(results), list)
226
-        self.assertEqual(len(results), 1)
228
+        self.assertEqual(len(results), 2)
227
         self.assertIn('log(x)+c', results[0]['answer'])
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
         xml = """<?xml version='1.0' encoding='UTF-8'?>
233
         xml = """<?xml version='1.0' encoding='UTF-8'?>
230
         <queryresult success='true'
234
         <queryresult success='true'
233
             datatypes='Solve'
237
             datatypes='Solve'
234
             timedout=''
238
             timedout=''
235
             timedoutpods=''
239
             timedoutpods=''
236
-            timing='0.883'
237
-            parsetiming='0.337'
240
+            timing='0.79'
241
+            parsetiming='0.338'
238
             parsetimedout='false'
242
             parsetimedout='false'
239
             recalculate=''
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
             version='2.6'>
248
             version='2.6'>
245
          <pod title='Input interpretation'
249
          <pod title='Input interpretation'
246
              scanner='Identity'
250
              scanner='Identity'
249
              error='false'
253
              error='false'
250
              numsubpods='1'>
254
              numsubpods='1'>
251
           <subpod title=''>
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
           </subpod>
257
           </subpod>
259
          </pod>
258
          </pod>
260
          <pod title='Results'
259
          <pod title='Results'
265
              numsubpods='2'
264
              numsubpods='2'
266
              primary='true'>
265
              primary='true'>
267
           <subpod title=''>
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
           </subpod>
268
           </subpod>
275
           <subpod title=''>
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
           </subpod>
271
           </subpod>
283
           <states count='1'>
272
           <states count='1'>
284
            <state name='Step-by-step solution'
273
            <state name='Step-by-step solution'
292
              error='false'
281
              error='false'
293
              numsubpods='1'>
282
              numsubpods='1'>
294
           <subpod title=''>
283
           <subpod title=''>
295
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP352225h1ea85fgfbgb40000464054c665hc5dee?...'
296
-               alt=''
297
-               title=''
298
-               width='300'
299
-               height='181' />
300
            <plaintext></plaintext>
284
            <plaintext></plaintext>
301
           </subpod>
285
           </subpod>
302
          </pod>
286
          </pod>
307
              error='false'
291
              error='false'
308
              numsubpods='1'>
292
              numsubpods='1'>
309
           <subpod title=''>
293
           <subpod title=''>
310
-           <img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP353225h1ea85fgfbgb400005ab1c8aai366fe46?...'
311
-               alt=''
312
-               title=''
313
-               width='310'
314
-               height='36' />
315
            <plaintext></plaintext>
294
            <plaintext></plaintext>
316
           </subpod>
295
           </subpod>
317
          </pod>
296
          </pod>
321
         response = mock.Mock(content=xml)
300
         response = mock.Mock(content=xml)
322
         results = wolframalpha_api.response(response)
301
         results = wolframalpha_api.response(response)
323
         self.assertEqual(type(results), list)
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'])