forked from zaclys/searxng
[fix] pass wolframalpha_noapi tests
This commit is contained in:
parent
e9d35c1309
commit
19d025f0e7
|
@ -8,60 +8,71 @@
|
||||||
# @stable no
|
# @stable no
|
||||||
# @parse answer
|
# @parse answer
|
||||||
|
|
||||||
from re import search
|
from re import search, sub
|
||||||
from json import loads
|
from json import loads
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
|
from lxml import html
|
||||||
|
|
||||||
# search-url
|
# search-url
|
||||||
url = 'http://www.wolframalpha.com/'
|
url = 'http://www.wolframalpha.com/'
|
||||||
search_url = url+'input/?{query}'
|
search_url = url+'input/?{query}'
|
||||||
search_query = ''
|
|
||||||
|
# xpath variables
|
||||||
|
scripts_xpath = '//script'
|
||||||
|
title_xpath = '//title'
|
||||||
|
failure_xpath = '//p[attribute::class="pfail"]'
|
||||||
|
|
||||||
|
|
||||||
# do search-request
|
# do search-request
|
||||||
def request(query, params):
|
def request(query, params):
|
||||||
params['url'] = search_url.format(query=urlencode({'i': query}))
|
params['url'] = search_url.format(query=urlencode({'i': query}))
|
||||||
|
|
||||||
# used in response
|
|
||||||
global search_query
|
|
||||||
search_query = query
|
|
||||||
|
|
||||||
return params
|
return params
|
||||||
|
|
||||||
|
|
||||||
# get response from search-request
|
# get response from search-request
|
||||||
def response(resp):
|
def response(resp):
|
||||||
results = []
|
results = []
|
||||||
webpage = resp.text
|
|
||||||
line = None
|
line = None
|
||||||
|
|
||||||
|
dom = html.fromstring(resp.text)
|
||||||
|
scripts = dom.xpath(scripts_xpath)
|
||||||
|
|
||||||
# the answer is inside a js function
|
# the answer is inside a js function
|
||||||
# answer can be located in different 'pods', although by default it should be in pod_0200
|
# answer can be located in different 'pods', although by default it should be in pod_0200
|
||||||
possible_locations = ['pod_0200\.push(.*)\n',
|
possible_locations = ['pod_0200\.push(.*)\n',
|
||||||
'pod_0100\.push(.*)\n']
|
'pod_0100\.push(.*)\n']
|
||||||
|
|
||||||
|
# failed result
|
||||||
|
if dom.xpath(failure_xpath):
|
||||||
|
return results
|
||||||
|
|
||||||
# get line that matches the pattern
|
# get line that matches the pattern
|
||||||
for pattern in possible_locations:
|
for pattern in possible_locations:
|
||||||
|
for script in scripts:
|
||||||
try:
|
try:
|
||||||
line = search(pattern, webpage).group(1)
|
line = search(pattern, script.text_content()).group(1)
|
||||||
break
|
break
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
continue
|
continue
|
||||||
|
if line:
|
||||||
|
break
|
||||||
|
|
||||||
if line:
|
if line:
|
||||||
# extract answer from json
|
# extract answer from json
|
||||||
answer = line[line.find('{'):line.rfind('}')+1]
|
answer = line[line.find('{'):line.rfind('}')+1]
|
||||||
answer = loads(answer.encode('unicode-escape'))
|
answer = loads(answer.encode('unicode-escape'))
|
||||||
answer = answer['stringified'].decode('unicode-escape')
|
answer = answer['stringified'].decode('unicode-escape')
|
||||||
|
answer = sub(r'\\', '', answer)
|
||||||
|
|
||||||
results.append({'answer': answer})
|
results.append({'answer': answer})
|
||||||
|
|
||||||
# failed result
|
# user input is in first part of title
|
||||||
elif search('pfail', webpage):
|
title = dom.xpath(title_xpath)[0].text
|
||||||
return results
|
result_url = request(title[:-16], {})['url']
|
||||||
|
|
||||||
# append result
|
# append result
|
||||||
results.append({'url': request(search_query, {})['url'],
|
results.append({'url': result_url,
|
||||||
'title': search_query + ' - Wolfram|Alpha'})
|
'title': title})
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
|
@ -148,7 +148,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), 2)
|
||||||
|
self.assertEqual(len(results), 1)
|
||||||
self.assertIn("i", results[0]['answer'])
|
self.assertIn("i", results[0]['answer'])
|
||||||
# self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
|
# self.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
|
||||||
# self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
|
# self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
|
||||||
|
@ -248,7 +249,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), 2)
|
||||||
|
self.assertEqual(len(results), 1)
|
||||||
self.assertIn("log(x)+c", results[0]['answer'])
|
self.assertIn("log(x)+c", results[0]['answer'])
|
||||||
# self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
|
# self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
|
||||||
# self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])
|
# self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])
|
||||||
|
|
|
@ -138,7 +138,7 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
|
||||||
self.assertEqual(len(results), 2)
|
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.assertIn("sqrt(-1) - Wolfram|Alpha", results[1]['title'])
|
||||||
self.assertIn("http://www.wolframalpha.com/input/?i=sqrt%28-1%29", results[1]['url'])
|
self.assertIn("http://www.wolframalpha.com/input/?i=+sqrt%28-1%29", results[1]['url'])
|
||||||
|
|
||||||
html = """
|
html = """
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
|
@ -233,4 +233,4 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase):
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 2)
|
||||||
self.assertIn("log(x)+c", results[0]['answer'])
|
self.assertIn("log(x)+c", results[0]['answer'])
|
||||||
self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
|
self.assertIn("integral 1/x - Wolfram|Alpha", results[1]['title'])
|
||||||
self.assertIn("http://www.wolframalpha.com/input/?i=integral+1%2Fx", results[1]['url'])
|
self.assertIn("http://www.wolframalpha.com/input/?i=+integral+1%2Fx", results[1]['url'])
|
||||||
|
|
Loading…
Reference in New Issue