logo

searx

Unnamed repository; edit this file 'description' to name the repository.
commit: 308613e586108fd7b713fc2a14f8200a0a781105
parent: 45b5073f3e10cad68ddda6a1a83d3273a3d734fc
Author: Adam Tauber <asciimoo@gmail.com>
Date:   Tue,  1 Mar 2016 10:41:56 +0100

Merge pull request #513 from a01200356/wolframalpha

WolframAlpha infobox

Diffstat:

searx/engines/wolframalpha_api.py | 99+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
searx/engines/wolframalpha_noapi.py | 61+++++++++++++++++++++++++++++++++++++++++--------------------
searx/settings.yml | 4++--
searx/static/themes/default/css/style.css | 3+--
searx/static/themes/default/less/style.less | 5+++--
searx/static/themes/oscar/css/oscar.min.css | 2+-
searx/static/themes/oscar/less/oscar/infobox.less | 3++-
searx/templates/default/infobox.html | 9++++++++-
searx/templates/oscar/infobox.html | 6+++++-
tests/unit/engines/test_wolframalpha_api.py | 367++++++++++++++++++++++++-------------------------------------------------------
tests/unit/engines/test_wolframalpha_noapi.py | 205+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
11 files changed, 449 insertions(+), 315 deletions(-)

diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py @@ -1,43 +1,60 @@ -# Wolfram Alpha (Maths) +# Wolfram Alpha (Science) # -# @website http://www.wolframalpha.com -# @provide-api yes (http://api.wolframalpha.com/v2/) +# @website https://www.wolframalpha.com +# @provide-api yes (https://api.wolframalpha.com/v2/) # # @using-api yes # @results XML # @stable yes -# @parse result +# @parse url, infobox from urllib import urlencode from lxml import etree -from re import search # search-url -base_url = 'http://api.wolframalpha.com/v2/query' -search_url = base_url + '?appid={api_key}&{query}&format=plaintext' -site_url = 'http://www.wolframalpha.com/input/?{query}' +search_url = 'https://api.wolframalpha.com/v2/query?appid={api_key}&{query}' +site_url = 'https://www.wolframalpha.com/input/?{query}' 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' +img_src_xpath = './@src' +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 = {'VisualRepresentation', + 'Illustration'} # do search-request def request(query, params): params['url'] = search_url.format(query=urlencode({'input': query}), api_key=api_key) + params['headers']['Referer'] = site_url.format(query=urlencode({'i': query})) return params # replace private user area characters to make text legible def replace_pua_chars(text): - pua_chars = {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) @@ -55,23 +72,51 @@ def response(resp): if search_results.xpath(failure_xpath): return [] - # parse answers - answers = search_results.xpath(answer_xpath) - if answers: - for answer in answers: - answer = replace_pua_chars(answer.text) + try: + infobox_title = search_results.xpath(input_xpath)[0].text + except: + infobox_title = None - results.append({'answer': answer}) + pods = search_results.xpath(pods_xpath) + result_chunks = [] + for pod in pods: + pod_id = pod.xpath(pod_id_xpath)[0] + pod_title = pod.xpath(pod_title_xpath)[0] - # 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) + 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_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], + 'alt': image[0].xpath(img_alt_xpath)[0]}}) + + if not result_chunks: + return [] + + # append infobox + results.append({'infobox': infobox_title, + 'attributes': result_chunks, + 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]}) # 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"}) + results.append({'url': resp.request.headers['Referer'].decode('utf8'), + 'title': 'Wolfram|Alpha', + 'content': infobox_title}) return results diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py @@ -1,26 +1,26 @@ -# WolframAlpha (Maths) +# Wolfram|Alpha (Science) # -# @website http://www.wolframalpha.com/ -# @provide-api yes (http://api.wolframalpha.com/v2/) +# @website https://www.wolframalpha.com/ +# @provide-api yes (https://api.wolframalpha.com/v2/) # # @using-api no -# @results HTML +# @results JSON # @stable no -# @parse answer +# @parse url, infobox from cgi import escape from json import loads from time import time from urllib import urlencode +from lxml.etree import XML from searx.poolrequests import get as http_get # search-url url = 'https://www.wolframalpha.com/' -search_url = url + 'input/?{query}' search_url = url + 'input/json.jsp'\ - '?async=true'\ + '?async=false'\ '&banners=raw'\ '&debuggingdata=false'\ '&format=image,plaintext,imagemap,minput,moutput'\ @@ -33,13 +33,17 @@ search_url = url + 'input/json.jsp'\ '&sponsorcategories=true'\ '&statemethod=deploybutton' -# xpath variables -scripts_xpath = '//script' -title_xpath = '//title' -failure_xpath = '//p[attribute::class="pfail"]' +referer_url = url + 'input/?{query}' + token = {'value': '', 'last_updated': None} +# 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 = {'VisualRepresentation', + 'Illustration', + 'Symbol'} + # seems, wolframalpha resets its token in every hour def obtain_token(): @@ -62,13 +66,15 @@ def request(query, params): if time() - token['last_updated'] > 3600: obtain_token() params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value']) - params['headers']['Referer'] = 'https://www.wolframalpha.com/input/?i=' + query + params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query})) return params # get response from search-request def response(resp): + results = [] + resp_json = loads(resp.text) if not resp_json['queryresult']['success']: @@ -76,20 +82,35 @@ def response(resp): # TODO handle resp_json['queryresult']['assumptions'] 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: continue + + if pod_id == 'Input' or not infobox_title: + infobox_title = pod['subpods'][0]['plaintext'] + for subpod in pod['subpods']: - if 'img' in subpod: - result_chunks.append(u'<p>{0}<br /><img src="{1}" alt="{2}" /></p>' - .format(escape(pod_title or subpod['img']['alt']), - escape(subpod['img']['src']), - escape(subpod['img']['alt']))) + 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']}) + + elif 'img' in subpod: + result_chunks.append({'label': pod_title, 'image': subpod['img']}) if not result_chunks: return [] - return [{'url': resp.request.headers['Referer'].decode('utf-8'), - 'title': 'Wolframalpha', - 'content': ''.join(result_chunks)}] + results.append({'infobox': infobox_title, + 'attributes': result_chunks, + 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]}) + + results.append({'url': resp.request.headers['Referer'].decode('utf8'), + 'title': 'Wolfram|Alpha', + 'content': infobox_title}) + + return results diff --git a/searx/settings.yml b/searx/settings.yml @@ -310,8 +310,8 @@ engines: shortcut : wa # You can use the engine using the official stable API, but you need an API key # See : http://products.wolframalpha.com/api/ - # engine : wolframalpha_api - # api_key: 'apikey' # required! + # engine : wolframalpha_api + # api_key: '' # required! engine : wolframalpha_noapi timeout: 6.0 categories : science diff --git a/searx/static/themes/default/css/style.css b/searx/static/themes/default/css/style.css @@ -1 +1 @@ -.highlight .hll{background-color:#ffc}.highlight{background:#f8f8f8}.highlight .c{color:#408080;font-style:italic}.highlight .err{border:1px solid #f00}.highlight .k{color:#008000;font-weight:bold}.highlight .o{color:#666}.highlight .cm{color:#408080;font-style:italic}.highlight .cp{color:#bc7a00}.highlight .c1{color:#408080;font-style:italic}.highlight .cs{color:#408080;font-style:italic}.highlight .gd{color:#a00000}.highlight .ge{font-style:italic}.highlight .gr{color:#f00}.highlight .gh{color:#000080;font-weight:bold}.highlight .gi{color:#00a000}.highlight .go{color:#888}.highlight .gp{color:#000080;font-weight:bold}.highlight .gs{font-weight:bold}.highlight .gu{color:#800080;font-weight:bold}.highlight .gt{color:#04d}.highlight .kc{color:#008000;font-weight:bold}.highlight .kd{color:#008000;font-weight:bold}.highlight .kn{color:#008000;font-weight:bold}.highlight .kp{color:#008000}.highlight .kr{color:#008000;font-weight:bold}.highlight .kt{color:#b00040}.highlight .m{color:#666}.highlight .s{color:#ba2121}.highlight .na{color:#7d9029}.highlight .nb{color:#008000}.highlight .nc{color:#00f;font-weight:bold}.highlight .no{color:#800}.highlight .nd{color:#a2f}.highlight .ni{color:#999;font-weight:bold}.highlight .ne{color:#d2413a;font-weight:bold}.highlight .nf{color:#00f}.highlight .nl{color:#a0a000}.highlight .nn{color:#00f;font-weight:bold}.highlight .nt{color:#008000;font-weight:bold}.highlight .nv{color:#19177c}.highlight .ow{color:#a2f;font-weight:bold}.highlight .w{color:#bbb}.highlight .mf{color:#666}.highlight .mh{color:#666}.highlight .mi{color:#666}.highlight .mo{color:#666}.highlight .sb{color:#ba2121}.highlight .sc{color:#ba2121}.highlight .sd{color:#ba2121;font-style:italic}.highlight .s2{color:#ba2121}.highlight .se{color:#b62;font-weight:bold}.highlight .sh{color:#ba2121}.highlight .si{color:#b68;font-weight:bold}.highlight .sx{color:#008000}.highlight .sr{color:#b68}.highlight .s1{color:#ba2121}.highlight .ss{color:#19177c}.highlight .bp{color:#008000}.highlight .vc{color:#19177c}.highlight .vg{color:#19177c}.highlight .vi{color:#19177c}.highlight .il{color:#666}.highlight pre{overflow:auto}.highlight .lineno{-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:default}.highlight .lineno::selection{background:transparent}.highlight .lineno::-moz-selection{background:transparent}html{font-family:sans-serif;font-size:.9em;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;-moz-text-size-adjust:100%;text-size-adjust:100%;color:#444;padding:0;margin:0}body,#container{padding:0;margin:0}#container{width:100%;position:absolute;top:0}.search{padding:0;margin:0}.search .checkbox_container label{font-size:.9em;border-bottom:2px solid #e8e7e6}.search .checkbox_container label:hover{border-bottom:2px solid #3498db}.search .checkbox_container input[type="checkbox"]:checked+label{border-bottom:2px solid #2980b9}#search_wrapper{position:relative;width:50em;padding:10px}.center #search_wrapper{margin-left:auto;margin-right:auto}.q{background:none repeat scroll 0 0 #fff;border:1px solid #3498db;color:#222;font-size:16px;height:28px;margin:0;outline:medium none;padding:2px;padding-left:8px;padding-right:0 !important;width:100%;z-index:2}#search_submit{position:absolute;top:13px;right:1px;padding:0;border:0;background:url('../img/search-icon.png') no-repeat;background-size:24px 24px;opacity:.8;width:24px;height:30px;font-size:0}@media screen and (max-width:50em){#search_wrapper{width:90%;clear:both;overflow:hidden}}ul.autocompleter-choices{position:absolute;margin:0;padding:0;list-style:none;border:1px solid #3498db;border-left-color:#3498db;border-right-color:#3498db;border-bottom-color:#3498db;text-align:left;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;z-index:50;background-color:#fff;color:#444}ul.autocompleter-choices li{position:relative;margin:-2px 0 0 0;padding:.2em 1.5em .2em 1em;display:block;float:none !important;cursor:pointer;font-weight:normal;white-space:nowrap;font-size:1em;line-height:1.5em}ul.autocompleter-choices li.autocompleter-selected{background-color:#444;color:#fff}ul.autocompleter-choices li.autocompleter-selected span.autocompleter-queried{color:#9fcfff}ul.autocompleter-choices span.autocompleter-queried{display:inline;float:none;font-weight:bold;margin:0;padding:0}.row{max-width:800px;margin:20px auto;text-align:justify}.row h1{font-size:3em;margin-top:50px}.row p{padding:0 10px;max-width:700px}.row h3,.row ul{margin:4px 8px}.hmarg{margin:0 20px;border:1px solid #3498db;padding:4px 10px}a:link.hmarg{color:#3498db}a:visited.hmarg{color:#3498db}a:active.hmarg{color:#3498db}a:hover.hmarg{color:#3498db}.top_margin{margin-top:60px}.center{text-align:center}h1{font-size:5em}div.title{background:url('../img/searx.png') no-repeat;width:100%;min-height:80px;background-position:center}div.title h1{visibility:hidden}input[type="submit"]{padding:2px 6px;margin:2px 4px;display:inline-block;background:#3498db;color:#fff;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;border:0;cursor:pointer}input[type="checkbox"]{visibility:hidden}fieldset{margin:8px;border:1px solid #3498db}#categories{margin:0 10px;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.checkbox_container{display:inline-block;position:relative;margin:0 3px;padding:0}.checkbox_container input{display:none}.checkbox_container label,.engine_checkbox label{cursor:pointer;padding:4px 10px;margin:0;display:block;text-transform:capitalize;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.checkbox_container input[type="checkbox"]:checked+label{background:#3498db;color:#fff}.engine_checkbox{padding:4px}label.allow{background:#e74c3c;padding:4px 8px;color:#fff;display:none}label.deny{background:#2ecc71;padding:4px 8px;color:#444;display:inline}.engine_checkbox input[type="checkbox"]:checked+label:nth-child(2)+label{display:none}.engine_checkbox input[type="checkbox"]:checked+label.allow{display:inline}a{text-decoration:none;color:#1a11be}a:visited{color:#8e44ad}.result{margin:19px 0 18px 0;padding:0;clear:both}.result_title{margin-bottom:0}.result_title a{color:#2980b9;font-weight:normal;font-size:1.1em}.result_title a:hover{text-decoration:underline}.result_title a:visited{color:#8e44ad}.cache_link{font-size:10px !important}.result h3{font-size:1em;word-wrap:break-word;margin:5px 0 1px 0;padding:0}.result .content{font-size:.8em;margin:0;padding:0;max-width:54em;word-wrap:break-word;line-height:1.24}.result .content img{float:left;margin-right:5px;max-width:200px;max-height:100px}.result .content br.last{clear:both}.result .url{font-size:.8em;margin:0 0 3px 0;padding:0;max-width:54em;word-wrap:break-word;color:#c0392b}.result .published_date{font-size:.8em;color:#888;Margin:5px 20px}.result .thumbnail{width:400px}.engines{color:#888}.small_font{font-size:.8em}.small p{margin:2px 0}.right{float:right}.invisible{display:none}.left{float:left}.highlight{color:#094089}.content .highlight{color:#000}.image_result{display:inline-block;margin:10px 10px;position:relative;max-height:160px}.image_result img{border:0;max-height:160px}.image_result p{margin:0;padding:0}.image_result p span a{display:none;color:#fff}.image_result p:hover span a{display:block;position:absolute;bottom:0;right:0;padding:4px;background-color:rgba(0,0,0,0.6);font-size:.7em}.torrent_result{border-left:10px solid lightgray;padding-left:3px}.torrent_result p{margin:3px;font-size:.8em}.torrent_result a{color:#2980b9}.torrent_result a:hover{text-decoration:underline}.torrent_result a:visited{color:#8e44ad}.definition_result{border-left:10px solid gray;padding-left:3px}.percentage{position:relative;width:300px}.percentage div{background:#444}table{width:100%}td{padding:0 4px}tr:hover{background:#ddd}#results{margin:auto;padding:0;width:50em;margin-bottom:20px}#sidebar{position:fixed;bottom:10px;left:10px;margin:0 2px 5px 5px;padding:0 2px 2px 2px;width:14em}#sidebar input{padding:0;margin:3px;font-size:.8em;display:inline-block;background:transparent;color:#444;cursor:pointer}#sidebar input[type="submit"]{text-decoration:underline}#suggestions form{display:inline}#suggestions,#answers{margin-top:20px;max-width:45em}#suggestions input,#answers input,#infoboxes input{padding:0;margin:3px;font-size:.8em;display:inline-block;background:transparent;color:#444;cursor:pointer}#suggestions input[type="submit"],#answers input[type="submit"],#infoboxes input[type="submit"]{text-decoration:underline}#suggestions-title{color:#888}#answers{border:2px solid #2980b9;padding:20px}#answers form,#infoboxes form{min-width:210px}#infoboxes{position:absolute;top:100px;right:20px;margin:0 2px 5px 5px;padding:0 2px 2px;max-width:21em}#infoboxes .infobox{margin:10px 0 10px;border:1px solid #ddd;padding:5px;font-size:.8em}#infoboxes .infobox img{max-width:20em;max-heigt:12em;display:block;margin:5px;padding:5px}#infoboxes .infobox h2{margin:0}#infoboxes .infobox table{width:auto}#infoboxes .infobox table td{vertical-align:top}#infoboxes .infobox input{font-size:1em}#infoboxes .infobox br{clear:both}#search_url{margin-top:8px}#search_url input{border:1px solid #888;padding:4px;color:#444;width:14em;display:block;margin:4px;font-size:.8em}#preferences{top:10px;padding:0;border:0;background:url('../img/preference-icon.png') no-repeat;background-size:28px 28px;opacity:.8;width:28px;height:30px;display:block}#preferences *{display:none}#pagination{clear:both}#pagination br{clear:both}#apis{margin-top:8px;clear:both}#categories_container{position:relative}@media screen and (max-width:50em){#results{margin:auto;padding:0;width:90%}.github{display:none}.checkbox_container{display:block;width:90%}.checkbox_container label{border-bottom:0}.preferences_container{display:none;postion:fixed !important;top:100px;right:0}}@media screen and (max-width:75em){div.title h1{font-size:1em}html.touch #categories{width:95%;height:30px;text-align:left;overflow-x:scroll;overflow-y:hidden;-webkit-overflow-scrolling:touch}html.touch #categories #categories_container{width:1000px;width:-moz-max-content;width:-webkit-max-content;width:max-content}html.touch #categories #categories_container .checkbox_container{display:inline-block;width:auto}#categories{font-size:90%;clear:both}#categories .checkbox_container{margin-top:2px;margin:auto}#suggestions,#answers{margin-top:5px}#infoboxes{position:inherit;max-width:inherit}#infoboxes .infobox{clear:both}#infoboxes .infobox img{float:left;max-width:10em}#categories{font-size:90%;clear:both}#categories .checkbox_container{margin-top:2px;margin:auto}#sidebar{position:static;max-width:50em;margin:0 0 2px 0;padding:0;float:none;border:none;width:auto}#sidebar input{border:0}#apis{display:none}#search_url{display:none}.result{border-top:1px solid #e8e7e6;margin:8px 0 8px 0}.result .thumbnail{max-width:98%}.image_result{max-width:98%}.image_result img{max-width:98%}}.favicon{float:left;margin-right:4px;margin-top:2px}.preferences_back{background:none repeat scroll 0 0 #3498db;border:0 none;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;cursor:pointer;display:inline-block;margin:2px 4px;padding:4px 6px}.preferences_back a{color:#fff}.hidden{opacity:0;overflow:hidden;font-size:.8em;position:absolute;bottom:-20px;width:100%;text-position:center;background:white;transition:opacity 1s ease}#categories_container:hover .hidden{transition:opacity 1s ease;opacity:.8}- \ No newline at end of file +.highlight .hll{background-color:#ffc}.highlight{background:#f8f8f8}.highlight .c{color:#408080;font-style:italic}.highlight .err{border:1px solid #f00}.highlight .k{color:#008000;font-weight:bold}.highlight .o{color:#666}.highlight .cm{color:#408080;font-style:italic}.highlight .cp{color:#bc7a00}.highlight .c1{color:#408080;font-style:italic}.highlight .cs{color:#408080;font-style:italic}.highlight .gd{color:#a00000}.highlight .ge{font-style:italic}.highlight .gr{color:#f00}.highlight .gh{color:#000080;font-weight:bold}.highlight .gi{color:#00a000}.highlight .go{color:#888}.highlight .gp{color:#000080;font-weight:bold}.highlight .gs{font-weight:bold}.highlight .gu{color:#800080;font-weight:bold}.highlight .gt{color:#04d}.highlight .kc{color:#008000;font-weight:bold}.highlight .kd{color:#008000;font-weight:bold}.highlight .kn{color:#008000;font-weight:bold}.highlight .kp{color:#008000}.highlight .kr{color:#008000;font-weight:bold}.highlight .kt{color:#b00040}.highlight .m{color:#666}.highlight .s{color:#ba2121}.highlight .na{color:#7d9029}.highlight .nb{color:#008000}.highlight .nc{color:#00f;font-weight:bold}.highlight .no{color:#800}.highlight .nd{color:#a2f}.highlight .ni{color:#999;font-weight:bold}.highlight .ne{color:#d2413a;font-weight:bold}.highlight .nf{color:#00f}.highlight .nl{color:#a0a000}.highlight .nn{color:#00f;font-weight:bold}.highlight .nt{color:#008000;font-weight:bold}.highlight .nv{color:#19177c}.highlight .ow{color:#a2f;font-weight:bold}.highlight .w{color:#bbb}.highlight .mf{color:#666}.highlight .mh{color:#666}.highlight .mi{color:#666}.highlight .mo{color:#666}.highlight .sb{color:#ba2121}.highlight .sc{color:#ba2121}.highlight .sd{color:#ba2121;font-style:italic}.highlight .s2{color:#ba2121}.highlight .se{color:#b62;font-weight:bold}.highlight .sh{color:#ba2121}.highlight .si{color:#b68;font-weight:bold}.highlight .sx{color:#008000}.highlight .sr{color:#b68}.highlight .s1{color:#ba2121}.highlight .ss{color:#19177c}.highlight .bp{color:#008000}.highlight .vc{color:#19177c}.highlight .vg{color:#19177c}.highlight .vi{color:#19177c}.highlight .il{color:#666}.highlight pre{overflow:auto}.highlight .lineno{-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;cursor:default}.highlight .lineno::selection{background:transparent}.highlight .lineno::-moz-selection{background:transparent}html{font-family:sans-serif;font-size:.9em;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;-moz-text-size-adjust:100%;text-size-adjust:100%;color:#444;padding:0;margin:0}body,#container{padding:0;margin:0}#container{width:100%;position:absolute;top:0}.search{padding:0;margin:0}.search .checkbox_container label{font-size:.9em;border-bottom:2px solid #e8e7e6}.search .checkbox_container label:hover{border-bottom:2px solid #3498db}.search .checkbox_container input[type="checkbox"]:checked+label{border-bottom:2px solid #2980b9}#search_wrapper{position:relative;width:50em;padding:10px}.center #search_wrapper{margin-left:auto;margin-right:auto}.q{background:none repeat scroll 0 0 #fff;border:1px solid #3498db;color:#222;font-size:16px;height:28px;margin:0;outline:medium none;padding:2px;padding-left:8px;padding-right:0 !important;width:100%;z-index:2}#search_submit{position:absolute;top:13px;right:1px;padding:0;border:0;background:url('../img/search-icon.png') no-repeat;background-size:24px 24px;opacity:.8;width:24px;height:30px;font-size:0}@media screen and (max-width:50em){#search_wrapper{width:90%;clear:both;overflow:hidden}}ul.autocompleter-choices{position:absolute;margin:0;padding:0;list-style:none;border:1px solid #3498db;border-left-color:#3498db;border-right-color:#3498db;border-bottom-color:#3498db;text-align:left;font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;z-index:50;background-color:#fff;color:#444}ul.autocompleter-choices li{position:relative;margin:-2px 0 0 0;padding:.2em 1.5em .2em 1em;display:block;float:none !important;cursor:pointer;font-weight:normal;white-space:nowrap;font-size:1em;line-height:1.5em}ul.autocompleter-choices li.autocompleter-selected{background-color:#444;color:#fff}ul.autocompleter-choices li.autocompleter-selected span.autocompleter-queried{color:#9fcfff}ul.autocompleter-choices span.autocompleter-queried{display:inline;float:none;font-weight:bold;margin:0;padding:0}.row{max-width:800px;margin:20px auto;text-align:justify}.row h1{font-size:3em;margin-top:50px}.row p{padding:0 10px;max-width:700px}.row h3,.row ul{margin:4px 8px}.hmarg{margin:0 20px;border:1px solid #3498db;padding:4px 10px}a:link.hmarg{color:#3498db}a:visited.hmarg{color:#3498db}a:active.hmarg{color:#3498db}a:hover.hmarg{color:#3498db}.top_margin{margin-top:60px}.center{text-align:center}h1{font-size:5em}div.title{background:url('../img/searx.png') no-repeat;width:100%;min-height:80px;background-position:center}div.title h1{visibility:hidden}input[type="submit"]{padding:2px 6px;margin:2px 4px;display:inline-block;background:#3498db;color:#fff;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;border:0;cursor:pointer}input[type="checkbox"]{visibility:hidden}fieldset{margin:8px;border:1px solid #3498db}#categories{margin:0 10px;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.checkbox_container{display:inline-block;position:relative;margin:0 3px;padding:0}.checkbox_container input{display:none}.checkbox_container label,.engine_checkbox label{cursor:pointer;padding:4px 10px;margin:0;display:block;text-transform:capitalize;-webkit-touch-callout:none;-webkit-user-select:none;-khtml-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none}.checkbox_container input[type="checkbox"]:checked+label{background:#3498db;color:#fff}.engine_checkbox{padding:4px}label.allow{background:#e74c3c;padding:4px 8px;color:#fff;display:none}label.deny{background:#2ecc71;padding:4px 8px;color:#444;display:inline}.engine_checkbox input[type="checkbox"]:checked+label:nth-child(2)+label{display:none}.engine_checkbox input[type="checkbox"]:checked+label.allow{display:inline}a{text-decoration:none;color:#1a11be}a:visited{color:#8e44ad}.result{margin:19px 0 18px 0;padding:0;clear:both}.result_title{margin-bottom:0}.result_title a{color:#2980b9;font-weight:normal;font-size:1.1em}.result_title a:hover{text-decoration:underline}.result_title a:visited{color:#8e44ad}.cache_link{font-size:10px !important}.result h3{font-size:1em;word-wrap:break-word;margin:5px 0 1px 0;padding:0}.result .content{font-size:.8em;margin:0;padding:0;max-width:54em;word-wrap:break-word;line-height:1.24}.result .content img{float:left;margin-right:5px;max-width:200px;max-height:100px}.result .content br.last{clear:both}.result .url{font-size:.8em;margin:0 0 3px 0;padding:0;max-width:54em;word-wrap:break-word;color:#c0392b}.result .published_date{font-size:.8em;color:#888;Margin:5px 20px}.result .thumbnail{width:400px}.engines{color:#888}.small_font{font-size:.8em}.small p{margin:2px 0}.right{float:right}.invisible{display:none}.left{float:left}.highlight{color:#094089}.content .highlight{color:#000}.image_result{display:inline-block;margin:10px 10px;position:relative;max-height:160px}.image_result img{border:0;max-height:160px}.image_result p{margin:0;padding:0}.image_result p span a{display:none;color:#fff}.image_result p:hover span a{display:block;position:absolute;bottom:0;right:0;padding:4px;background-color:rgba(0,0,0,0.6);font-size:.7em}.torrent_result{border-left:10px solid lightgray;padding-left:3px}.torrent_result p{margin:3px;font-size:.8em}.torrent_result a{color:#2980b9}.torrent_result a:hover{text-decoration:underline}.torrent_result a:visited{color:#8e44ad}.definition_result{border-left:10px solid gray;padding-left:3px}.percentage{position:relative;width:300px}.percentage div{background:#444}table{width:100%}td{padding:0 4px}tr:hover{background:#ddd}#results{margin:auto;padding:0;width:50em;margin-bottom:20px}#sidebar{position:fixed;bottom:10px;left:10px;margin:0 2px 5px 5px;padding:0 2px 2px 2px;width:14em}#sidebar input{padding:0;margin:3px;font-size:.8em;display:inline-block;background:transparent;color:#444;cursor:pointer}#sidebar input[type="submit"]{text-decoration:underline}#suggestions form{display:inline}#suggestions,#answers{margin-top:20px;max-width:45em}#suggestions input,#answers input,#infoboxes input{padding:0;margin:3px;font-size:.8em;display:inline-block;background:transparent;color:#444;cursor:pointer}#suggestions input[type="submit"],#answers input[type="submit"],#infoboxes input[type="submit"]{text-decoration:underline}#suggestions-title{color:#888}#answers{border:2px solid #2980b9;padding:20px}#answers form,#infoboxes form{min-width:210px}#infoboxes{position:absolute;top:100px;right:20px;margin:0 2px 5px 5px;padding:0 2px 2px;max-width:21em;word-wrap:break-word;}#infoboxes .infobox{margin:10px 0 10px;border:1px solid #ddd;padding:5px;font-size:.8em}#infoboxes .infobox img{max-width:90%;max-heigt:12em;display:block;margin:5px;padding:5px}#infoboxes .infobox h2{margin:0}#infoboxes .infobox table{table-layout:fixed;}#infoboxes .infobox table td{vertical-align:top}#infoboxes .infobox input{font-size:1em}#infoboxes .infobox br{clear:both}#search_url{margin-top:8px}#search_url input{border:1px solid #888;padding:4px;color:#444;width:14em;display:block;margin:4px;font-size:.8em}#preferences{top:10px;padding:0;border:0;background:url('../img/preference-icon.png') no-repeat;background-size:28px 28px;opacity:.8;width:28px;height:30px;display:block}#preferences *{display:none}#pagination{clear:both}#pagination br{clear:both}#apis{margin-top:8px;clear:both}#categories_container{position:relative}@media screen and (max-width:50em){#results{margin:auto;padding:0;width:90%}.github{display:none}.checkbox_container{display:block;width:90%}.checkbox_container label{border-bottom:0}.preferences_container{display:none;postion:fixed !important;top:100px;right:0}}@media screen and (max-width:75em){div.title h1{font-size:1em}html.touch #categories{width:95%;height:30px;text-align:left;overflow-x:scroll;overflow-y:hidden;-webkit-overflow-scrolling:touch}html.touch #categories #categories_container{width:1000px;width:-moz-max-content;width:-webkit-max-content;width:max-content}html.touch #categories #categories_container .checkbox_container{display:inline-block;width:auto}#categories{font-size:90%;clear:both}#categories .checkbox_container{margin-top:2px;margin:auto}#suggestions,#answers{margin-top:5px}#infoboxes{position:inherit;max-width:inherit}#infoboxes .infobox{clear:both}#infoboxes .infobox img{float:left;max-width:10em}#categories{font-size:90%;clear:both}#categories .checkbox_container{margin-top:2px;margin:auto}#sidebar{position:static;max-width:50em;margin:0 0 2px 0;padding:0;float:none;border:none;width:auto}#sidebar input{border:0}#apis{display:none}#search_url{display:none}.result{border-top:1px solid #e8e7e6;margin:8px 0 8px 0}.result .thumbnail{max-width:98%}.image_result{max-width:98%}.image_result img{max-width:98%}}.favicon{float:left;margin-right:4px;margin-top:2px}.preferences_back{background:none repeat scroll 0 0 #3498db;border:0 none;-webkit-border-radius:4px;-moz-border-radius:4px;border-radius:4px;cursor:pointer;display:inline-block;margin:2px 4px;padding:4px 6px}.preferences_back a{color:#fff}.hidden{opacity:0;overflow:hidden;font-size:.8em;position:absolute;bottom:-20px;width:100%;text-position:center;background:white;transition:opacity 1s ease}#categories_container:hover .hidden{transition:opacity 1s ease;opacity:.8} diff --git a/searx/static/themes/default/less/style.less b/searx/static/themes/default/less/style.less @@ -476,6 +476,7 @@ color: @color-font-light; margin: 0px 2px 5px 5px; padding: 0px 2px 2px; max-width: 21em; + word-wrap: break-word; .infobox { margin: 10px 0 10px; @@ -485,7 +486,7 @@ color: @color-font-light; /* box-shadow: 0px 0px 5px #CCC; */ img { - max-width: 20em; + max-width: 90%; max-heigt: 12em; display: block; margin: 5px; @@ -497,7 +498,7 @@ color: @color-font-light; } table { - width: auto; + table-layout: fixed; td { vertical-align: top; diff --git a/searx/static/themes/oscar/css/oscar.min.css b/searx/static/themes/oscar/css/oscar.min.css @@ -17,7 +17,7 @@ input[type=checkbox]:not(:checked)+.label_hide_if_not_checked,input[type=checkbo .result_download{margin-right:5px} #pagination{margin-top:30px;padding-bottom:50px} .label-default{color:#aaa;background:#fff} -.infobox .infobox_part{margin-bottom:20px;word-wrap:break-word} +.infobox .infobox_part{margin-bottom:20px;word-wrap:break-word;table-layout:fixed} .infobox .infobox_part:last-child{margin-bottom:0} .search_categories{margin:10px 0;text-transform:capitalize} .cursor-text{cursor:text !important} diff --git a/searx/static/themes/oscar/less/oscar/infobox.less b/searx/static/themes/oscar/less/oscar/infobox.less @@ -1,7 +1,8 @@ .infobox { .infobox_part { margin-bottom: 20px; - word-wrap: break-word; + word-wrap: break-word; + table-layout: fixed; } .infobox_part:last-child { diff --git a/searx/templates/default/infobox.html b/searx/templates/default/infobox.html @@ -7,7 +7,14 @@ <div class="attributes"> <table> {% for attribute in infobox.attributes %} - <tr><td>{{ attribute.label }}</td><td>{{ attribute.value }}</td></tr> + <tr> + <td>{{ attribute.label }}</td> + {% if attribute.image %} + <td><img src="{{ image_proxify(attribute.image.src) }}" alt="{{ attribute.image.alt }}" /></td> + {% else %} + <td>{{ attribute.value }}</td> + {% endif %} + </tr> {% endfor %} </table> </div> diff --git a/searx/templates/oscar/infobox.html b/searx/templates/oscar/infobox.html @@ -1,6 +1,6 @@ <div class="panel panel-default infobox"> <div class="panel-heading"> - <h4 class="panel-title">{{ infobox.infobox }}</h4> + <h4 class="panel-title infobox_part">{{ infobox.infobox }}</h4> </div> <div class="panel-body"> {% if infobox.img_src %}<img class="img-responsive center-block infobox_part" src="{{ image_proxify(infobox.img_src) }}" alt="{{ infobox.infobox }}" />{% endif %} @@ -11,7 +11,11 @@ {% for attribute in infobox.attributes %} <tr> <td>{{ attribute.label }}</td> + {% if attribute.image %} + <td><img class="img-responsive" src="{{ image_proxify(attribute.image.src) }}" alt="{{ attribute.image.alt }}" /></td> + {% else %} <td>{{ attribute.value }}</td> + {% endif %} </tr> {% endfor %} </table> diff --git a/tests/unit/engines/test_wolframalpha_api.py b/tests/unit/engines/test_wolframalpha_api.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from collections import defaultdict import mock +from requests import Request from searx.engines import wolframalpha_api from searx.testing import SearxTestCase @@ -9,17 +10,17 @@ class TestWolframAlphaAPIEngine(SearxTestCase): def test_request(self): query = 'test_query' - api_key = 'XXXXXX-XXXXXXXXXX' dicto = defaultdict(dict) - dicto['api_key'] = api_key params = wolframalpha_api.request(query, dicto) + # TODO: test api_key self.assertIn('url', params) + self.assertIn('https://api.wolframalpha.com/v2/query?', params['url']) self.assertIn(query, params['url']) - self.assertIn('wolframalpha.com', params['url']) + self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer']) - self.assertIn('api_key', params) - self.assertIn(api_key, params['api_key']) + def test_replace_pua_chars(self): + self.assertEqual('i', wolframalpha_api.replace_pua_chars(u'\uf74e')) def test_response(self): self.assertRaises(AttributeError, wolframalpha_api.response, None) @@ -27,281 +28,137 @@ class TestWolframAlphaAPIEngine(SearxTestCase): self.assertRaises(AttributeError, wolframalpha_api.response, '') self.assertRaises(AttributeError, wolframalpha_api.response, '[]') + referer_url = 'referer_url' + request = Request(headers={'Referer': referer_url}) + + # test failure xml = '''<?xml version='1.0' encoding='UTF-8'?> <queryresult success='false' error='false' /> ''' - # test failure response = mock.Mock(content=xml) self.assertEqual(wolframalpha_api.response(response), []) + # test basic case xml = """<?xml version='1.0' encoding='UTF-8'?> <queryresult success='true' error='false' - numpods='6' - datatypes='' - timedout='' - timedoutpods='' - timing='0.684' - parsetiming='0.138' - parsetimedout='false' - recalculate='' - id='MSPa416020a7966dachc463600000f9c66cc21444cfg' - host='http://www3.wolframalpha.com' - server='6' - related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?...' + numpods='3' + datatypes='Math' + id='queryresult_id' + host='http://www4c.wolframalpha.com' + related='related_url' version='2.6'> - <pod title='Input' - scanner='Identity' - id='Input' - position='100' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext>sqrt(-1)</plaintext> - </subpod> - </pod> - <pod title='Result' - scanner='Simplification' - id='Result' - position='200' - error='false' - numsubpods='1' - primary='true'> - <subpod title=''> - <plaintext></plaintext> - </subpod> - <states count='1'> - <state name='Step-by-step solution' - input='Result__Step-by-step solution' /> - </states> - </pod> - <pod title='Polar coordinates' - scanner='Numeric' - id='PolarCoordinates' - position='300' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext>r1 (radius), θ90° (angle)</plaintext> - </subpod> - </pod> - <pod title='Position in the complex plane' - scanner='Numeric' - id='PositionInTheComplexPlane' - position='400' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext></plaintext> - </subpod> - </pod> - <pod title='All 2nd roots of -1' - scanner='RootsOfUnity' - id='' - position='500' - error='false' - numsubpods='2'> - <subpod title=''> - <plaintext> (principal root)</plaintext> - </subpod> - <subpod title=''> - <plaintext>-</plaintext> - </subpod> - </pod> - <pod title='Plot of all roots in the complex plane' - scanner='RootsOfUnity' - id='PlotOfAllRootsInTheComplexPlane' - position='600' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext></plaintext> - </subpod> - </pod> - </queryresult> - """ - # test private user area char in response - response = mock.Mock(content=xml) - results = wolframalpha_api.response(response) - self.assertEqual(type(results), list) - self.assertEqual(len(results), 2) - 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'?> - <queryresult success='true' - error='false' - numpods='2' - datatypes='' - timedout='' - timedoutpods='' - timing='1.286' - parsetiming='0.255' - parsetimedout='false' - recalculate='' - id='MSPa195222ad740ede5214h30000480ca61h003d3gd6' - host='http://www3.wolframalpha.com' - server='20' - related='http://www3.wolframalpha.com/api/v2/relatedQueries.jsp?id=...' - version='2.6'> - <pod title='Indefinite integral' - scanner='Integral' - id='IndefiniteIntegral' - position='100' - error='false' + <pod title='Input' + scanner='Identity' + id='Input' + numsubpods='1'> + <subpod title=''> + <img src='input_img_src.gif' + alt='input_img_alt' + title='input_img_title' /> + <plaintext>input_plaintext</plaintext> + </subpod> + </pod> + <pod title='Result' + scanner='Simplification' + id='Result' numsubpods='1' primary='true'> - <subpod title=''> - <plaintext>∫1/xxlog(x)+constant</plaintext> - </subpod> - <states count='1'> - <state name='Step-by-step solution' - input='IndefiniteIntegral__Step-by-step solution' /> - </states> - <infos count='1'> - <info text='log(x) is the natural logarithm'> - <link url='http://reference.wolfram.com/mathematica/ref/Log.html' - text='Documentation' - title='Mathematica' /> - <link url='http://functions.wolfram.com/ElementaryFunctions/Log' - text='Properties' - title='Wolfram Functions Site' /> - <link url='http://mathworld.wolfram.com/NaturalLogarithm.html' - text='Definition' - title='MathWorld' /> - </info> - </infos> + <subpod title=''> + <img src='result_img_src.gif' + alt='result_img_alt' + title='result_img_title' /> + <plaintext>result_plaintext</plaintext> + </subpod> </pod> - <pod title='Plots of the integral' - scanner='Integral' - id='Plot' - position='200' - error='false' - numsubpods='2'> - <subpod title=''> - <plaintext></plaintext> - <states count='1'> - <statelist count='2' - value='Complex-valued plot' - delimiters=''> - <state name='Complex-valued plot' - input='Plot__1_Complex-valued plot' /> - <state name='Real-valued plot' - input='Plot__1_Real-valued plot' /> - </statelist> - </states> - </subpod> - <subpod title=''> - <plaintext></plaintext> - <states count='1'> - <statelist count='2' - value='Complex-valued plot' - delimiters=''> - <state name='Complex-valued plot' - input='Plot__2_Complex-valued plot' /> - <state name='Real-valued plot' - input='Plot__2_Real-valued plot' /> - </statelist> - </states> - </subpod> + <pod title='Manipulatives illustration' + scanner='Arithmetic' + id='Illustration' + numsubpods='1'> + <subpod title=''> + <img src='illustration_img_src.gif' + alt='illustration_img_alt' /> + <plaintext>illustration_plaintext</plaintext> + </subpod> </pod> - <assumptions count='1'> - <assumption type='Clash' - word='integral' - template='Assuming &quot;${word}&quot; is ${desc1}. Use as ${desc2} instead' - count='2'> - <value name='IntegralsWord' - desc='an integral' - input='*C.integral-_*IntegralsWord-' /> - <value name='MathematicalFunctionIdentityPropertyClass' - desc='a function property' - input='*C.integral-_*MathematicalFunctionIdentityPropertyClass-' /> - </assumption> - </assumptions> - </queryresult> + </queryresult> """ - # test integral - response = mock.Mock(content=xml) + response = mock.Mock(content=xml, request=request) results = wolframalpha_api.response(response) self.assertEqual(type(results), list) self.assertEqual(len(results), 2) - self.assertIn('log(x)+c', results[0]['answer']) - self.assertIn('∫1/xx - 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']) + self.assertEqual('input_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 3) + self.assertEqual('Input', results[0]['attributes'][0]['label']) + self.assertEqual('input_plaintext', results[0]['attributes'][0]['value']) + self.assertEqual('Result', results[0]['attributes'][1]['label']) + self.assertEqual('result_plaintext', results[0]['attributes'][1]['value']) + self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label']) + self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) + self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt']) + + self.assertEqual(len(results[0]['urls']), 1) + self.assertEqual(referer_url, results[0]['urls'][0]['url']) + self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) + self.assertEqual(referer_url, results[1]['url']) + self.assertEqual('Wolfram|Alpha', results[1]['title']) + + # test calc xml = """<?xml version='1.0' encoding='UTF-8'?> <queryresult success='true' error='false' - numpods='4' - datatypes='Solve' - timedout='' - timedoutpods='' - timing='0.79' - parsetiming='0.338' + numpods='2' + datatypes='' parsetimedout='false' - recalculate='' - id='MSPa7481f7i06d25h3deh2900004810i3a78d9b4fdc' + id='queryresult_id' host='http://www5b.wolframalpha.com' - server='23' - related='http://www5b.wolframalpha.com/api/v2/relatedQueries.jsp?id=...' - version='2.6'> - <pod title='Input interpretation' - scanner='Identity' - id='Input' - position='100' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext>solve x^2+x0</plaintext> - </subpod> - </pod> - <pod title='Results' - scanner='Solve' - id='Result' - position='200' - error='false' - numsubpods='2' - primary='true'> - <subpod title=''> - <plaintext>x-1</plaintext> - </subpod> - <subpod title=''> - <plaintext>x0</plaintext> - </subpod> - <states count='1'> - <state name='Step-by-step solution' - input='Result__Step-by-step solution' /> - </states> - </pod> - <pod title='Root plot' - scanner='Solve' - id='RootPlot' - position='300' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext></plaintext> - </subpod> - </pod> - <pod title='Number line' - scanner='Solve' - id='NumberLine' - position='400' - error='false' - numsubpods='1'> - <subpod title=''> - <plaintext></plaintext> - </subpod> - </pod> + related='related_url' + version='2.6' > + <pod title='Indefinite integral' + scanner='Integral' + id='IndefiniteIntegral' + error='false' + numsubpods='1' + primary='true'> + <subpod title=''> + <img src='integral_image.gif' + alt='integral_img_alt' + title='integral_img_title' /> + <plaintext>integral_plaintext</plaintext> + </subpod> + </pod> + <pod title='Plot of the integral' + scanner='Integral' + id='Plot' + error='false' + numsubpods='1'> + <subpod title=''> + <img src='plot.gif' + alt='plot_alt' + title='' /> + <plaintext></plaintext> + </subpod> + </pod> </queryresult> """ - # test ecuation with multiple answers - response = mock.Mock(content=xml) + response = mock.Mock(content=xml, request=request) results = wolframalpha_api.response(response) self.assertEqual(type(results), list) - self.assertEqual(len(results), 3) - self.assertIn('x=-1', results[0]['answer']) - self.assertIn('x=0', results[1]['answer']) - self.assertIn('solve x^2+x0 - 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']) + self.assertEqual(len(results), 2) + self.assertEqual('integral_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 2) + self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label']) + 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(referer_url, results[0]['urls'][0]['url']) + self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) + self.assertEqual(referer_url, results[1]['url']) + self.assertEqual('Wolfram|Alpha', results[1]['title']) diff --git a/tests/unit/engines/test_wolframalpha_noapi.py b/tests/unit/engines/test_wolframalpha_noapi.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- from collections import defaultdict +import mock +from requests import Request from searx.engines import wolframalpha_noapi from searx.testing import SearxTestCase @@ -9,15 +11,212 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): def test_request(self): query = 'test_query' dicto = defaultdict(dict) - dicto['pageno'] = 1 params = wolframalpha_noapi.request(query, dicto) + self.assertIn('url', params) + self.assertIn('https://www.wolframalpha.com/input/json.jsp', params['url']) self.assertIn(query, params['url']) - self.assertIn('wolframalpha.com', params['url']) + self.assertEqual('https://www.wolframalpha.com/input/?i=test_query', params['headers']['Referer']) def test_response(self): self.assertRaises(AttributeError, wolframalpha_noapi.response, None) self.assertRaises(AttributeError, wolframalpha_noapi.response, []) self.assertRaises(AttributeError, wolframalpha_noapi.response, '') self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]') - # TODO + + referer_url = 'referer_url' + request = Request(headers={'Referer': referer_url}) + + # test failure + json = ''' + {"queryresult" : { + "success" : false, + "error" : false, + "numpods" : 0, + "id" : "", + "host" : "https:\/\/www5a.wolframalpha.com", + "didyoumeans" : {} + }} + ''' + response = mock.Mock(text=json, request=request) + self.assertEqual(wolframalpha_noapi.response(response), []) + + # test basic case + json = ''' + {"queryresult" : { + "success" : true, + "error" : false, + "numpods" : 6, + "datatypes" : "Math", + "id" : "queryresult_id", + "host" : "https:\/\/www5b.wolframalpha.com", + "related" : "related_url", + "version" : "2.6", + "pods" : [ + { + "title" : "Input", + "scanners" : [ + "Identity" + ], + "id" : "Input", + "error" : false, + "numsubpods" : 1, + "subpods" : [ + { + "title" : "", + "img" : { + "src" : "input_img_src.gif", + "alt" : "input_img_alt", + "title" : "input_img_title" + }, + "plaintext" : "input_plaintext", + "minput" : "input_minput" + } + ] + }, + { + "title" : "Result", + "scanners" : [ + "Simplification" + ], + "id" : "Result", + "error" : false, + "numsubpods" : 1, + "primary" : true, + "subpods" : [ + { + "title" : "", + "img" : { + "src" : "result_img_src.gif", + "alt" : "result_img_alt", + "title" : "result_img_title" + }, + "plaintext" : "result_plaintext", + "moutput" : "result_moutput" + } + ] + }, + { + "title" : "Manipulatives illustration", + "scanners" : [ + "Arithmetic" + ], + "id" : "Illustration", + "error" : false, + "numsubpods" : 1, + "subpods" : [ + { + "title" : "", + "CDFcontent" : "Resizeable", + "img" : { + "src" : "illustration_img_src.gif", + "alt" : "illustration_img_alt", + "title" : "illustration_img_title" + }, + "plaintext" : "illustration_img_plaintext" + } + ] + } + ] + }} + ''' + response = mock.Mock(text=json, request=request) + results = wolframalpha_noapi.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertEqual('input_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 3) + self.assertEqual('Input', results[0]['attributes'][0]['label']) + self.assertEqual('input_plaintext', results[0]['attributes'][0]['value']) + self.assertEqual('Result', results[0]['attributes'][1]['label']) + self.assertEqual('result_plaintext', results[0]['attributes'][1]['value']) + self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label']) + self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) + self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt']) + + self.assertEqual(len(results[0]['urls']), 1) + + self.assertEqual(referer_url, results[0]['urls'][0]['url']) + self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) + self.assertEqual(referer_url, results[1]['url']) + self.assertEqual('Wolfram|Alpha', results[1]['title']) + + # test calc + json = """ + {"queryresult" : { + "success" : true, + "error" : false, + "numpods" : 2, + "datatypes" : "", + "id" : "queryresult_id", + "host" : "https:\/\/www4b.wolframalpha.com", + "related" : "related_url", + "version" : "2.6", + "pods" : [ + { + "title" : "Indefinite integral", + "scanners" : [ + "Integral" + ], + "id" : "IndefiniteIntegral", + "error" : false, + "numsubpods" : 1, + "primary" : true, + "subpods" : [ + { + "title" : "", + "img" : { + "src" : "integral_img_src.gif", + "alt" : "integral_img_alt", + "title" : "integral_img_title" + }, + "plaintext" : "integral_plaintext", + "minput" : "integral_minput", + "moutput" : "integral_moutput" + } + ] + }, + { + "title" : "Plot of the integral", + "scanners" : [ + "Integral" + ], + "id" : "Plot", + "error" : false, + "numsubpods" : 1, + "subpods" : [ + { + "title" : "", + "img" : { + "src" : "plot.gif", + "alt" : "plot_alt", + "title" : "plot_title" + }, + "plaintext" : "", + "minput" : "plot_minput" + } + ] + } + ] + }} + """ + response = mock.Mock(text=json, request=request) + results = wolframalpha_noapi.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertEqual('integral_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 2) + self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label']) + 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(referer_url, results[0]['urls'][0]['url']) + self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) + self.assertEqual(referer_url, results[1]['url']) + self.assertEqual('Wolfram|Alpha', results[1]['title'])