mirror of
https://github.com/searxng/searxng
synced 2024-01-01 18:24:07 +00:00
append link to result in wolframalpha_api (and the tests to validate that)
This commit is contained in:
parent
2a15944b58
commit
8ca5744814
@ -10,15 +10,18 @@
|
|||||||
|
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
|
from re import search
|
||||||
|
|
||||||
# search-url
|
# search-url
|
||||||
base_url = 'http://api.wolframalpha.com/v2/query'
|
base_url = 'http://api.wolframalpha.com/v2/query'
|
||||||
search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
|
search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
|
||||||
|
site_url = 'http://www.wolframalpha.com/input/?{query}'
|
||||||
api_key = '' # defined in settings.yml
|
api_key = '' # defined in settings.yml
|
||||||
|
|
||||||
# xpath variables
|
# xpath variables
|
||||||
failure_xpath = '/queryresult[attribute::success="false"]'
|
failure_xpath = '/queryresult[attribute::success="false"]'
|
||||||
answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
|
answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
|
||||||
|
input_xpath = '//pod[starts-with(attribute::title, "Input")]/subpod/plaintext'
|
||||||
|
|
||||||
|
|
||||||
# do search-request
|
# do search-request
|
||||||
@ -60,6 +63,15 @@ def response(resp):
|
|||||||
|
|
||||||
results.append({'answer': answer})
|
results.append({'answer': answer})
|
||||||
|
|
||||||
# TODO: append a result with title and link, like in the no api version
|
# if there's no input section in search_results, check if answer has the input embedded (before their "=" sign)
|
||||||
|
try:
|
||||||
|
query_input = search_results.xpath(input_xpath)[0].text
|
||||||
|
except IndexError:
|
||||||
|
query_input = search(u'([^\uf7d9]+)', answers[0].text).group(1)
|
||||||
|
|
||||||
|
# append link to site
|
||||||
|
result_url = site_url.format(query=urlencode({'i': query_input.encode('utf-8')}))
|
||||||
|
results.append({'url': result_url,
|
||||||
|
'title': query_input + " - Wolfram|Alpha"})
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
@ -305,7 +305,7 @@ engines:
|
|||||||
# You can use the engine using the official stable API, but you need an API key
|
# You can use the engine using the official stable API, but you need an API key
|
||||||
# See : http://products.wolframalpha.com/api/
|
# See : http://products.wolframalpha.com/api/
|
||||||
# engine : wolframalpha_api
|
# engine : wolframalpha_api
|
||||||
# api_key: '' # required!
|
# api_key: 'apikey' # required!
|
||||||
engine : wolframalpha_noapi
|
engine : wolframalpha_noapi
|
||||||
timeout: 6.0
|
timeout: 6.0
|
||||||
disabled : True
|
disabled : True
|
||||||
|
@ -124,8 +124,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
response = mock.Mock(content=xml)
|
response = mock.Mock(content=xml)
|
||||||
results = wolframalpha_api.response(response)
|
results = wolframalpha_api.response(response)
|
||||||
self.assertEqual(type(results), list)
|
self.assertEqual(type(results), list)
|
||||||
self.assertEqual(len(results), 1)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn('i', results[0]['answer'])
|
self.assertIn('i', results[0]['answer'])
|
||||||
|
self.assertIn('sqrt(-1) - Wolfram|Alpha', results[1]['title'])
|
||||||
|
self.assertEquals('http://www.wolframalpha.com/input/?i=sqrt%28-1%29', results[1]['url'])
|
||||||
|
|
||||||
xml = """<?xml version='1.0' encoding='UTF-8'?>
|
xml = """<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<queryresult success='true'
|
<queryresult success='true'
|
||||||
@ -223,8 +225,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
response = mock.Mock(content=xml)
|
response = mock.Mock(content=xml)
|
||||||
results = wolframalpha_api.response(response)
|
results = wolframalpha_api.response(response)
|
||||||
self.assertEqual(type(results), list)
|
self.assertEqual(type(results), list)
|
||||||
self.assertEqual(len(results), 1)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn('log(x)+c', results[0]['answer'])
|
self.assertIn('log(x)+c', results[0]['answer'])
|
||||||
|
self.assertIn('∫1/xx - Wolfram|Alpha'.decode('utf-8'), results[1]['title'])
|
||||||
|
self.assertEquals('http://www.wolframalpha.com/input/?i=%E2%88%AB1%2Fx%EF%9D%8Cx', results[1]['url'])
|
||||||
|
|
||||||
xml = """<?xml version='1.0' encoding='UTF-8'?>
|
xml = """<?xml version='1.0' encoding='UTF-8'?>
|
||||||
<queryresult success='true'
|
<queryresult success='true'
|
||||||
@ -233,14 +237,14 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
datatypes='Solve'
|
datatypes='Solve'
|
||||||
timedout=''
|
timedout=''
|
||||||
timedoutpods=''
|
timedoutpods=''
|
||||||
timing='0.883'
|
timing='0.79'
|
||||||
parsetiming='0.337'
|
parsetiming='0.338'
|
||||||
parsetimedout='false'
|
parsetimedout='false'
|
||||||
recalculate=''
|
recalculate=''
|
||||||
id='MSPa347225h1ea85fgfbgb4000064ff000d25g5df3f'
|
id='MSPa7481f7i06d25h3deh2900004810i3a78d9b4fdc'
|
||||||
host='http://www5a.wolframalpha.com'
|
host='http://www5b.wolframalpha.com'
|
||||||
server='52'
|
server='23'
|
||||||
related='http://www5a.wolframalpha.com/api/v2/relatedQueries.jsp?...'
|
related='http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=...'
|
||||||
version='2.6'>
|
version='2.6'>
|
||||||
<pod title='Input interpretation'
|
<pod title='Input interpretation'
|
||||||
scanner='Identity'
|
scanner='Identity'
|
||||||
@ -249,12 +253,7 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
error='false'
|
error='false'
|
||||||
numsubpods='1'>
|
numsubpods='1'>
|
||||||
<subpod title=''>
|
<subpod title=''>
|
||||||
<img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP349225h1ea85fgfbgb400005dhd93b9eegg8f32?...'
|
<plaintext>solve x^2+x0</plaintext>
|
||||||
alt='solve x^2+x = 0'
|
|
||||||
title='solve x^2+x = 0'
|
|
||||||
width='157'
|
|
||||||
height='35' />
|
|
||||||
<plaintext>solve x^2+x = 0</plaintext>
|
|
||||||
</subpod>
|
</subpod>
|
||||||
</pod>
|
</pod>
|
||||||
<pod title='Results'
|
<pod title='Results'
|
||||||
@ -265,20 +264,10 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
numsubpods='2'
|
numsubpods='2'
|
||||||
primary='true'>
|
primary='true'>
|
||||||
<subpod title=''>
|
<subpod title=''>
|
||||||
<img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP350225h1ea85fgfbgb400005b1ebcefaha3ac97?...'
|
<plaintext>x-1</plaintext>
|
||||||
alt='x = -1'
|
|
||||||
title='x = -1'
|
|
||||||
width='47'
|
|
||||||
height='18' />
|
|
||||||
<plaintext>x = -1</plaintext>
|
|
||||||
</subpod>
|
</subpod>
|
||||||
<subpod title=''>
|
<subpod title=''>
|
||||||
<img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP351225h1ea85fgfbgb4000032fic0ig981hc936?...'
|
<plaintext>x0</plaintext>
|
||||||
alt='x = 0'
|
|
||||||
title='x = 0'
|
|
||||||
width='36'
|
|
||||||
height='18' />
|
|
||||||
<plaintext>x = 0</plaintext>
|
|
||||||
</subpod>
|
</subpod>
|
||||||
<states count='1'>
|
<states count='1'>
|
||||||
<state name='Step-by-step solution'
|
<state name='Step-by-step solution'
|
||||||
@ -292,11 +281,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
error='false'
|
error='false'
|
||||||
numsubpods='1'>
|
numsubpods='1'>
|
||||||
<subpod title=''>
|
<subpod title=''>
|
||||||
<img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP352225h1ea85fgfbgb40000464054c665hc5dee?...'
|
|
||||||
alt=''
|
|
||||||
title=''
|
|
||||||
width='300'
|
|
||||||
height='181' />
|
|
||||||
<plaintext></plaintext>
|
<plaintext></plaintext>
|
||||||
</subpod>
|
</subpod>
|
||||||
</pod>
|
</pod>
|
||||||
@ -307,11 +291,6 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
error='false'
|
error='false'
|
||||||
numsubpods='1'>
|
numsubpods='1'>
|
||||||
<subpod title=''>
|
<subpod title=''>
|
||||||
<img src='http://www5a.wolframalpha.com/Calculate/MSP/MSP353225h1ea85fgfbgb400005ab1c8aai366fe46?...'
|
|
||||||
alt=''
|
|
||||||
title=''
|
|
||||||
width='310'
|
|
||||||
height='36' />
|
|
||||||
<plaintext></plaintext>
|
<plaintext></plaintext>
|
||||||
</subpod>
|
</subpod>
|
||||||
</pod>
|
</pod>
|
||||||
@ -321,6 +300,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
|||||||
response = mock.Mock(content=xml)
|
response = mock.Mock(content=xml)
|
||||||
results = wolframalpha_api.response(response)
|
results = wolframalpha_api.response(response)
|
||||||
self.assertEqual(type(results), list)
|
self.assertEqual(type(results), list)
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 3)
|
||||||
self.assertIn('x = -1', results[0]['answer'])
|
self.assertIn('x=-1', results[0]['answer'])
|
||||||
self.assertIn('x = 0', results[1]['answer'])
|
self.assertIn('x=0', results[1]['answer'])
|
||||||
|
self.assertIn('solve x^2+x0 - Wolfram|Alpha'.decode('utf-8'), results[2]['title'])
|
||||||
|
self.assertEquals('http://www.wolframalpha.com/input/?i=solve+x%5E2%2Bx%EF%9F%990', results[2]['url'])
|
||||||
|
Loading…
Reference in New Issue
Block a user