summaryrefslogtreecommitdiff
path: root/searx/engines
diff options
context:
space:
mode:
authora01200356 <a01200356@itesm.mx>2016-02-28 00:47:36 -0600
committera01200356 <a01200356@itesm.mx>2016-02-28 00:47:36 -0600
commit4d8996eb4d54a0938ad9dc6ad105de2b850fa033 (patch)
treec523d6ea02d94c9ba35554b83c625ba06d49147b /searx/engines
parent4267b11a45b7427bcba91259fcda202bd049f004 (diff)
downloadsearxng-4d8996eb4d54a0938ad9dc6ad105de2b850fa033.tar.gz
searxng-4d8996eb4d54a0938ad9dc6ad105de2b850fa033.zip
[enh] unit tests for wolframalpha
Diffstat (limited to 'searx/engines')
-rw-r--r--searx/engines/wolframalpha_api.py47
-rw-r--r--searx/engines/wolframalpha_noapi.py61
2 files changed, 61 insertions, 47 deletions
diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py
index ad80fb0f4..9a13d7422 100644
--- a/searx/engines/wolframalpha_api.py
+++ b/searx/engines/wolframalpha_api.py
@@ -19,9 +19,10 @@ api_key = '' # defined in settings.yml
# xpath variables
failure_xpath = '/queryresult[attribute::success="false"]'
answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
-input_xpath = '//pod[starts-with(attribute::title, "Input")]/subpod/plaintext'
+input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
pods_xpath = '//pod'
subpods_xpath = './subpod'
+pod_id_xpath = './@id'
pod_title_xpath = './@title'
plaintext_xpath = './plaintext'
image_xpath = './img'
@@ -30,8 +31,8 @@ img_alt_xpath = './@alt'
# pods to display as image in infobox
# this pods do return a plaintext, but they look better and are more useful as images
-image_pods = {'Visual representation',
- 'Manipulatives illustration'}
+image_pods = {'VisualRepresentation',
+ 'Illustration'}
# do search-request
@@ -45,15 +46,15 @@ def request(query, params):
# replace private user area characters to make text legible
def replace_pua_chars(text):
- pua_chars = {u'\uf522': u'\u2192',
- u'\uf7b1': u'\u2115',
- u'\uf7b4': u'\u211a',
- u'\uf7b5': u'\u211d',
- u'\uf7bd': u'\u2124',
- u'\uf74c': 'd',
- u'\uf74d': u'\u212f',
- u'\uf74e': 'i',
- u'\uf7d9': '='}
+ pua_chars = {u'\uf522': u'\u2192', # rigth arrow
+ u'\uf7b1': u'\u2115', # set of natural numbers
+ u'\uf7b4': u'\u211a', # set of rational numbers
+ u'\uf7b5': u'\u211d', # set of real numbers
+ u'\uf7bd': u'\u2124', # set of integer numbers
+ u'\uf74c': 'd', # differential
+ u'\uf74d': u'\u212f', # euler's number
+ u'\uf74e': 'i', # imaginary number
+ u'\uf7d9': '='} # equals sign
for k, v in pua_chars.iteritems():
text = text.replace(k, v)
@@ -71,30 +72,35 @@ def response(resp):
if search_results.xpath(failure_xpath):
return []
- infobox_title = search_results.xpath(input_xpath)
- if infobox_title:
- infobox_title = replace_pua_chars(infobox_title[0].text)
+ try:
+ infobox_title = search_results.xpath(input_xpath)[0].text
+ except:
+ infobox_title = None
pods = search_results.xpath(pods_xpath)
result_chunks = []
for pod in pods:
- pod_title = replace_pua_chars(pod.xpath(pod_title_xpath)[0])
+ pod_id = pod.xpath(pod_id_xpath)[0]
+ pod_title = pod.xpath(pod_title_xpath)[0]
subpods = pod.xpath(subpods_xpath)
if not subpods:
continue
+ # Appends either a text or an image, depending on which one is more suitable
for subpod in subpods:
content = subpod.xpath(plaintext_xpath)[0].text
image = subpod.xpath(image_xpath)
- if content and pod_title not in image_pods:
- content = replace_pua_chars(content)
- result_chunks.append({'label': pod_title, 'value': content})
- # if there's no input pod, infobox_title is content of first pod
+ if content and pod_id not in image_pods:
+
+ # if no input pod was found, title is first plaintext pod
if not infobox_title:
infobox_title = content
+ content = replace_pua_chars(content)
+ result_chunks.append({'label': pod_title, 'value': content})
+
elif image:
result_chunks.append({'label': pod_title,
'image': {'src': image[0].xpath(img_src_xpath)[0],
@@ -103,6 +109,7 @@ def response(resp):
if not result_chunks:
return []
+ # append infobox
results.append({'infobox': infobox_title,
'attributes': result_chunks,
'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py
index a5fbdeae7..d10716a2a 100644
--- a/searx/engines/wolframalpha_noapi.py
+++ b/searx/engines/wolframalpha_noapi.py
@@ -48,8 +48,8 @@ img_alt_xpath = './img/@alt'
# pods to display as image in infobox
# this pods do return a plaintext, but they look better and are more useful as images
-image_pods = {'Visual representation',
- 'Manipulatives illustration',
+image_pods = {'VisualRepresentation',
+ 'Illustration',
'Symbol'}
@@ -82,26 +82,35 @@ def request(query, 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):
- pod = {'subpods': []}
-
try:
resp = http_get(url, timeout=2.0)
-
- resp_pod = XML(resp.content)
- if resp_pod.xpath(success_xpath):
-
- for subpod in resp_pod:
- plaintext = subpod.xpath(plaintext_xpath)[0].text
- if plaintext:
- pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
- 'plaintext': plaintext})
- elif subpod.xpath(image_xpath):
- pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
- 'plaintext': '',
- 'img': {'src': subpod.xpath(img_src_xpath)[0],
- 'alt': subpod.xpath(img_alt_xpath)[0]}})
except:
- pass
+ 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
@@ -119,6 +128,7 @@ def response(resp):
result_chunks = []
infobox_title = None
for pod in resp_json['queryresult']['pods']:
+ pod_id = pod.get('id', '')
pod_title = pod.get('title', '')
if 'subpods' not in pod:
@@ -127,19 +137,16 @@ def response(resp):
result = get_async_pod(pod['async'])
if result:
pod = result
+ else:
+ continue
else:
continue
- # infobox title is input or text content on first pod
- if pod_title.startswith('Input') or not infobox_title:
- try:
- infobox_title = pod['subpods'][0]['plaintext']
- except:
- infobox_title = ''
- pass
+ if pod_id == 'Input' or not infobox_title:
+ infobox_title = pod['subpods'][0]['plaintext']
for subpod in pod['subpods']:
- if subpod['plaintext'] != '' and pod_title not in image_pods:
+ if subpod['plaintext'] != '' and pod_id not in image_pods:
# append unless it's not an actual answer
if subpod['plaintext'] != '(requires interactivity)':
result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})