forked from zaclys/searxng
[fix] remove unnecesary async calls in wolframalpha_noapi
setting async to false in the request did the job, lol.
This commit is contained in:
parent
4cea71e3bb
commit
8f3b33de23
|
@ -20,7 +20,7 @@ from searx.poolrequests import get as http_get
|
||||||
url = 'https://www.wolframalpha.com/'
|
url = 'https://www.wolframalpha.com/'
|
||||||
|
|
||||||
search_url = url + 'input/json.jsp'\
|
search_url = url + 'input/json.jsp'\
|
||||||
'?async=true'\
|
'?async=false'\
|
||||||
'&banners=raw'\
|
'&banners=raw'\
|
||||||
'&debuggingdata=false'\
|
'&debuggingdata=false'\
|
||||||
'&format=image,plaintext,imagemap,minput,moutput'\
|
'&format=image,plaintext,imagemap,minput,moutput'\
|
||||||
|
@ -38,14 +38,6 @@ referer_url = url + 'input/?{query}'
|
||||||
token = {'value': '',
|
token = {'value': '',
|
||||||
'last_updated': None}
|
'last_updated': None}
|
||||||
|
|
||||||
# xpath variables
|
|
||||||
success_xpath = '/pod[attribute::error="false"]'
|
|
||||||
plaintext_xpath = './plaintext'
|
|
||||||
title_xpath = './@title'
|
|
||||||
image_xpath = './img'
|
|
||||||
img_src_xpath = './img/@src'
|
|
||||||
img_alt_xpath = './img/@alt'
|
|
||||||
|
|
||||||
# pods to display as image in infobox
|
# pods to display as image in infobox
|
||||||
# this pods do return a plaintext, but they look better and are more useful as images
|
# this pods do return a plaintext, but they look better and are more useful as images
|
||||||
image_pods = {'VisualRepresentation',
|
image_pods = {'VisualRepresentation',
|
||||||
|
@ -79,42 +71,6 @@ def request(query, params):
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
# get additional pod
|
|
||||||
# NOTE: this makes an additional requests to server, so the response will take longer and might reach timeout
|
|
||||||
def get_async_pod(url):
|
|
||||||
try:
|
|
||||||
resp = http_get(url, timeout=2.0)
|
|
||||||
except:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if resp:
|
|
||||||
return parse_async_pod(resp)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_async_pod(resp):
|
|
||||||
pod = {'subpods': []}
|
|
||||||
|
|
||||||
resp_pod = XML(resp.content)
|
|
||||||
|
|
||||||
if resp_pod.xpath(success_xpath):
|
|
||||||
for subpod in resp_pod:
|
|
||||||
new_subpod = {'title': subpod.xpath(title_xpath)[0]}
|
|
||||||
|
|
||||||
plaintext = subpod.xpath(plaintext_xpath)[0].text
|
|
||||||
if plaintext:
|
|
||||||
new_subpod['plaintext'] = plaintext
|
|
||||||
else:
|
|
||||||
new_subpod['plaintext'] = ''
|
|
||||||
|
|
||||||
if subpod.xpath(image_xpath):
|
|
||||||
new_subpod['img'] = {'src': subpod.xpath(img_src_xpath)[0],
|
|
||||||
'alt': subpod.xpath(img_alt_xpath)[0]}
|
|
||||||
|
|
||||||
pod['subpods'].append(new_subpod)
|
|
||||||
|
|
||||||
return pod
|
|
||||||
|
|
||||||
|
|
||||||
# get response from search-request
|
# get response from search-request
|
||||||
def response(resp):
|
def response(resp):
|
||||||
results = []
|
results = []
|
||||||
|
@ -132,15 +88,7 @@ def response(resp):
|
||||||
pod_title = pod.get('title', '')
|
pod_title = pod.get('title', '')
|
||||||
|
|
||||||
if 'subpods' not in pod:
|
if 'subpods' not in pod:
|
||||||
# comment this section if your requests always reach timeout
|
continue
|
||||||
if pod['async']:
|
|
||||||
result = get_async_pod(pod['async'])
|
|
||||||
if result:
|
|
||||||
pod = result
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if pod_id == 'Input' or not infobox_title:
|
if pod_id == 'Input' or not infobox_title:
|
||||||
infobox_title = pod['subpods'][0]['plaintext']
|
infobox_title = pod['subpods'][0]['plaintext']
|
||||||
|
|
|
@ -87,16 +87,16 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
||||||
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), 2)
|
||||||
self.assertIn('input_plaintext', results[0]['infobox'])
|
self.assertEqual('input_plaintext', results[0]['infobox'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['attributes']), 3)
|
self.assertEqual(len(results[0]['attributes']), 3)
|
||||||
self.assertIn('Input', results[0]['attributes'][0]['label'])
|
self.assertEqual('Input', results[0]['attributes'][0]['label'])
|
||||||
self.assertIn('input_plaintext', results[0]['attributes'][0]['value'])
|
self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
|
||||||
self.assertIn('Result', results[0]['attributes'][1]['label'])
|
self.assertEqual('Result', results[0]['attributes'][1]['label'])
|
||||||
self.assertIn('result_plaintext', results[0]['attributes'][1]['value'])
|
self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
|
||||||
self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label'])
|
self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
|
||||||
self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
|
self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
|
||||||
self.assertIn('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
|
self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['urls']), 1)
|
self.assertEqual(len(results[0]['urls']), 1)
|
||||||
|
|
||||||
|
@ -129,8 +129,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
||||||
<plaintext>integral_plaintext</plaintext>
|
<plaintext>integral_plaintext</plaintext>
|
||||||
</subpod>
|
</subpod>
|
||||||
</pod>
|
</pod>
|
||||||
<pod title='Plot'
|
<pod title='Plot of the integral'
|
||||||
scanner='Plotter'
|
scanner='Integral'
|
||||||
id='Plot'
|
id='Plot'
|
||||||
error='false'
|
error='false'
|
||||||
numsubpods='1'>
|
numsubpods='1'>
|
||||||
|
@ -147,14 +147,14 @@ class TestWolframAlphaAPIEngine(SearxTestCase):
|
||||||
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), 2)
|
||||||
self.assertIn('integral_plaintext', results[0]['infobox'])
|
self.assertEqual('integral_plaintext', results[0]['infobox'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['attributes']), 2)
|
self.assertEqual(len(results[0]['attributes']), 2)
|
||||||
self.assertIn('Indefinite integral', results[0]['attributes'][0]['label'])
|
self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
|
||||||
self.assertIn('integral_plaintext', results[0]['attributes'][0]['value'])
|
self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
|
||||||
self.assertIn('Plot', results[0]['attributes'][1]['label'])
|
self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
|
||||||
self.assertIn('plot.gif', results[0]['attributes'][1]['image']['src'])
|
self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
|
||||||
self.assertIn('plot_alt', results[0]['attributes'][1]['image']['alt'])
|
self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['urls']), 1)
|
self.assertEqual(len(results[0]['urls']), 1)
|
||||||
|
|
||||||
|
|
|
@ -124,16 +124,16 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
|
||||||
results = wolframalpha_noapi.response(response)
|
results = wolframalpha_noapi.response(response)
|
||||||
self.assertEqual(type(results), list)
|
self.assertEqual(type(results), list)
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn('input_plaintext', results[0]['infobox'])
|
self.assertEqual('input_plaintext', results[0]['infobox'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['attributes']), 3)
|
self.assertEqual(len(results[0]['attributes']), 3)
|
||||||
self.assertIn('Input', results[0]['attributes'][0]['label'])
|
self.assertEqual('Input', results[0]['attributes'][0]['label'])
|
||||||
self.assertIn('input_plaintext', results[0]['attributes'][0]['value'])
|
self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
|
||||||
self.assertIn('Result', results[0]['attributes'][1]['label'])
|
self.assertEqual('Result', results[0]['attributes'][1]['label'])
|
||||||
self.assertIn('result_plaintext', results[0]['attributes'][1]['value'])
|
self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
|
||||||
self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label'])
|
self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
|
||||||
self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
|
self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
|
||||||
self.assertIn('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
|
self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['urls']), 1)
|
self.assertEqual(len(results[0]['urls']), 1)
|
||||||
|
|
||||||
|
@ -184,8 +184,19 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
|
||||||
],
|
],
|
||||||
"id" : "Plot",
|
"id" : "Plot",
|
||||||
"error" : false,
|
"error" : false,
|
||||||
"numsubpods" : 0,
|
"numsubpods" : 1,
|
||||||
"async" : "invalid_async_url"
|
"subpods" : [
|
||||||
|
{
|
||||||
|
"title" : "",
|
||||||
|
"img" : {
|
||||||
|
"src" : "plot.gif",
|
||||||
|
"alt" : "plot_alt",
|
||||||
|
"title" : "plot_title"
|
||||||
|
},
|
||||||
|
"plaintext" : "",
|
||||||
|
"minput" : "plot_minput"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}}
|
}}
|
||||||
|
@ -194,11 +205,14 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
|
||||||
results = wolframalpha_noapi.response(response)
|
results = wolframalpha_noapi.response(response)
|
||||||
self.assertEqual(type(results), list)
|
self.assertEqual(type(results), list)
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn('integral_plaintext', results[0]['infobox'])
|
self.assertEqual('integral_plaintext', results[0]['infobox'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['attributes']), 1)
|
self.assertEqual(len(results[0]['attributes']), 2)
|
||||||
self.assertIn('Indefinite integral', results[0]['attributes'][0]['label'])
|
self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
|
||||||
self.assertIn('integral_plaintext', results[0]['attributes'][0]['value'])
|
self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
|
||||||
|
self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
|
||||||
|
self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
|
||||||
|
self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])
|
||||||
|
|
||||||
self.assertEqual(len(results[0]['urls']), 1)
|
self.assertEqual(len(results[0]['urls']), 1)
|
||||||
|
|
||||||
|
@ -206,33 +220,3 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
|
||||||
self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
|
self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
|
||||||
self.assertEqual(referer_url, results[1]['url'])
|
self.assertEqual(referer_url, results[1]['url'])
|
||||||
self.assertEqual('Wolfram|Alpha', results[1]['title'])
|
self.assertEqual('Wolfram|Alpha', results[1]['title'])
|
||||||
|
|
||||||
def test_parse_async_pod(self):
|
|
||||||
self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, None)
|
|
||||||
self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, [])
|
|
||||||
self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, '')
|
|
||||||
self.assertRaises(AttributeError, wolframalpha_noapi.parse_async_pod, '[]')
|
|
||||||
|
|
||||||
# test plot
|
|
||||||
xml = '''<?xml version='1.0' encoding='UTF-8'?>
|
|
||||||
<pod title='Plot'
|
|
||||||
scanner='Plot'
|
|
||||||
id='Plot'
|
|
||||||
error='false'
|
|
||||||
numsubpods='1'>
|
|
||||||
<subpod title=''>
|
|
||||||
<img src='plot_img_src.gif'
|
|
||||||
alt='plot_img_alt'
|
|
||||||
title='plot_img_title' />
|
|
||||||
<plaintext>plot_plaintext</plaintext>
|
|
||||||
<minput>plot_minput</minput>
|
|
||||||
</subpod>
|
|
||||||
</pod>
|
|
||||||
'''
|
|
||||||
response = mock.Mock(content=xml)
|
|
||||||
pod = wolframalpha_noapi.parse_async_pod(response)
|
|
||||||
self.assertEqual(len(pod['subpods']), 1)
|
|
||||||
self.assertEqual('', pod['subpods'][0]['title'])
|
|
||||||
self.assertEqual('plot_plaintext', pod['subpods'][0]['plaintext'])
|
|
||||||
self.assertEqual('plot_img_src.gif', pod['subpods'][0]['img']['src'])
|
|
||||||
self.assertEqual('plot_img_alt', pod['subpods'][0]['img']['alt'])
|
|
||||||
|
|
Loading…
Reference in New Issue