From 78d3f3d6b116e0aca8af8b939d5a98ea74bb8de3 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sat, 27 Feb 2016 19:06:44 -0600 Subject: [PATCH 1/5] [enh] infobox for wolframalpha TODO: - infobox styles - unit tests ISSUES: - no_api version needs to re-call server for additional pods, such as plots. therefore, it's even slower than before. comment out the part that calls get_async_pod if requests reach timeout or increase timeout in settings.yml. --- searx/engines/wolframalpha_api.py | 84 ++++++++++++++++------ searx/engines/wolframalpha_noapi.py | 106 ++++++++++++++++++++++------ searx/settings.yml | 6 +- 3 files changed, 150 insertions(+), 46 deletions(-) diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 303c6c165..ad80fb0f4 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -1,40 +1,56 @@ -# 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' +pods_xpath = '//pod' +subpods_xpath = './subpod' +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 = {'Visual representation', + 'Manipulatives 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', + 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': '='} @@ -55,23 +71,45 @@ 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) + infobox_title = search_results.xpath(input_xpath) + if infobox_title: + infobox_title = replace_pua_chars(infobox_title[0].text) - results.append({'answer': answer}) + pods = search_results.xpath(pods_xpath) + result_chunks = [] + for pod in pods: + pod_title = replace_pua_chars(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 + + 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 not infobox_title: + infobox_title = 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 [] + + results.append({'infobox': infobox_title, + 'attributes': result_chunks, + 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]}) # 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'], + 'title': 'Wolfram|Alpha', + 'content': infobox_title}) return results diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py index 639dccf24..a5fbdeae7 100644 --- a/searx/engines/wolframalpha_noapi.py +++ b/searx/engines/wolframalpha_noapi.py @@ -1,23 +1,23 @@ -# 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'\ @@ -33,13 +33,25 @@ 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} +# 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 +# this pods do return a plaintext, but they look better and are more useful as images +image_pods = {'Visual representation', + 'Manipulatives illustration', + 'Symbol'} + # seems, wolframalpha resets its token in every hour def obtain_token(): @@ -62,13 +74,42 @@ 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 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 pod + + # get response from search-request def response(resp): + results = [] + resp_json = loads(resp.text) if not resp_json['queryresult']['success']: @@ -76,20 +117,45 @@ def response(resp): # TODO handle resp_json['queryresult']['assumptions'] result_chunks = [] + infobox_title = None for pod in resp_json['queryresult']['pods']: pod_title = pod.get('title', '') + if 'subpods' not in pod: - continue + # comment this section if your requests always reach timeout + if pod['async']: + result = get_async_pod(pod['async']) + if result: + pod = result + 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 + for subpod in pod['subpods']: - if 'img' in subpod: - result_chunks.append(u'

{0}
{2}

' - .format(escape(pod_title or subpod['img']['alt']), - escape(subpod['img']['src']), - escape(subpod['img']['alt']))) + if subpod['plaintext'] != '' and pod_title 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']}]}) + + results.append({'url': resp.request.headers['Referer'], + 'title': 'Wolfram|Alpha', + 'content': infobox_title}) + + return results diff --git a/searx/settings.yml b/searx/settings.yml index 7607ce94d..6e4316b08 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -310,10 +310,10 @@ 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: '5952JX-X52L3VKWT8' # required! engine : wolframalpha_noapi - timeout: 6.0 + timeout: 10.0 categories : science #The blekko technology and team have joined IBM Watson! -> https://blekko.com/ From 4267b11a45b7427bcba91259fcda202bd049f004 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sat, 27 Feb 2016 19:19:04 -0600 Subject: [PATCH 2/5] [fix] apply changes in infobox's styles changes were made for wolframalpha infobox: - wrap text inside infobox. for example, there's a hill in New Zealand called Taumatawhakatangihangakoauauotamateapokaiwhenuakitanatahu (don't blame me, blame the Kiwis) and now it doesn't break the infobox. - add an optional image field for infobox's attributes. (doesn't affect ddg infobox at all) - table is now always split in half. needed so that images stay inside infobox. (max-width doesn't work for inline elements, it's the table that has to set the width. if you don't like how the table width looks now in ddg/wiki's infobox, i can change that code so that the style only applies when using wolframalpha. --- searx/static/themes/default/css/style.css | 2 +- 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 +++++- 6 files changed, 20 insertions(+), 7 deletions(-) diff --git a/searx/static/themes/default/css/style.css b/searx/static/themes/default/css/style.css index 5be452e98..71422bc94 100644 --- 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 index 575bc222c..4374f7d68 100644 --- 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 index f7aba2bbc..60b5c3715 100644 --- 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 index d8f6f9264..41375f277 100644 --- 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 index 1733f7753..178a27e6d 100644 --- a/searx/templates/default/infobox.html +++ b/searx/templates/default/infobox.html @@ -7,7 +7,14 @@
{% for attribute in infobox.attributes %} - + + + {% if attribute.image %} + + {% else %} + + {% endif %} + {% endfor %}
{{ attribute.label }}{{ attribute.value }}
{{ attribute.label }}{{ attribute.image.alt }}{{ attribute.value }}
diff --git a/searx/templates/oscar/infobox.html b/searx/templates/oscar/infobox.html index 2abdbf0ec..d87d98453 100644 --- a/searx/templates/oscar/infobox.html +++ b/searx/templates/oscar/infobox.html @@ -1,6 +1,6 @@
-

{{ infobox.infobox }}

+

{{ infobox.infobox }}

{% if infobox.img_src %}{{ infobox.infobox }}{% endif %} @@ -11,7 +11,11 @@ {% for attribute in infobox.attributes %} {{ attribute.label }} + {% if attribute.image %} + {{ attribute.image.alt }} + {% else %} {{ attribute.value }} + {% endif %} {% endfor %} From 4d8996eb4d54a0938ad9dc6ad105de2b850fa033 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sun, 28 Feb 2016 00:47:36 -0600 Subject: [PATCH 3/5] [enh] unit tests for wolframalpha --- searx/engines/wolframalpha_api.py | 47 ++- searx/engines/wolframalpha_noapi.py | 61 +-- searx/settings.yml | 4 +- tests/unit/engines/test_wolframalpha_api.py | 367 ++++++------------ tests/unit/engines/test_wolframalpha_noapi.py | 221 ++++++++++- 5 files changed, 393 insertions(+), 307 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']}) diff --git a/searx/settings.yml b/searx/settings.yml index 6e4316b08..7a5937ed1 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -311,9 +311,9 @@ engines: # 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: '5952JX-X52L3VKWT8' # required! + # api_key: '' # required! engine : wolframalpha_noapi - timeout: 10.0 + timeout: 6.0 categories : science #The blekko technology and team have joined IBM Watson! -> https://blekko.com/ diff --git a/tests/unit/engines/test_wolframalpha_api.py b/tests/unit/engines/test_wolframalpha_api.py index c80775795..9dbe4d860 100644 --- 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 = ''' ''' - # test failure response = mock.Mock(content=xml) self.assertEqual(wolframalpha_api.response(response), []) + # test basic case xml = """ - - - 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.assertIn('input_plaintext', results[0]['infobox']) + self.assertEqual(len(results[0]['attributes']), 3) + self.assertIn('Input', results[0]['attributes'][0]['label']) + self.assertIn('input_plaintext', results[0]['attributes'][0]['value']) + self.assertIn('Result', results[0]['attributes'][1]['label']) + self.assertIn('result_plaintext', results[0]['attributes'][1]['value']) + self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label']) + self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) + self.assertIn('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' + scanner='Plotter' + 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.assertIn('integral_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 2) + self.assertIn('Indefinite integral', results[0]['attributes'][0]['label']) + self.assertIn('integral_plaintext', results[0]['attributes'][0]['value']) + self.assertIn('Plot', results[0]['attributes'][1]['label']) + self.assertIn('plot.gif', results[0]['attributes'][1]['image']['src']) + self.assertIn('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 index 37f3a9059..1129dc8b0 100644 --- 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,228 @@ 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.assertIn('input_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 3) + self.assertIn('Input', results[0]['attributes'][0]['label']) + self.assertIn('input_plaintext', results[0]['attributes'][0]['value']) + self.assertIn('Result', results[0]['attributes'][1]['label']) + self.assertIn('result_plaintext', results[0]['attributes'][1]['value']) + self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label']) + self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) + self.assertIn('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" : 0, + "async" : "invalid_async_url" + } + ] + }} + """ + response = mock.Mock(text=json, request=request) + results = wolframalpha_noapi.response(response) + self.assertEqual(type(results), list) + self.assertEqual(len(results), 2) + self.assertIn('integral_plaintext', results[0]['infobox']) + + self.assertEqual(len(results[0]['attributes']), 1) + self.assertIn('Indefinite integral', results[0]['attributes'][0]['label']) + self.assertIn('integral_plaintext', results[0]['attributes'][0]['value']) + + 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']) + + 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']) From 4cea71e3bb34020dff0f3e28f5c1398c0b0d8278 Mon Sep 17 00:00:00 2001 From: a01200356 <a01200356@itesm.mx> Date: Sun, 28 Feb 2016 00:52:47 -0600 Subject: [PATCH 4/5] [fix] merge with 79705450dfdf321c19839bce23c56d9d4a86ba68 --- searx/engines/wolframalpha_api.py | 4 ++-- searx/engines/wolframalpha_noapi.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/searx/engines/wolframalpha_api.py b/searx/engines/wolframalpha_api.py index 9a13d7422..4526c825f 100644 --- a/searx/engines/wolframalpha_api.py +++ b/searx/engines/wolframalpha_api.py @@ -112,10 +112,10 @@ def response(resp): # append infobox results.append({'infobox': infobox_title, 'attributes': result_chunks, - 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]}) + 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]}) # append link to site - results.append({'url': resp.request.headers['Referer'], + results.append({'url': resp.request.headers['Referer'].decode('utf8'), 'title': 'Wolfram|Alpha', 'content': infobox_title}) diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py index d10716a2a..7962d923c 100644 --- a/searx/engines/wolframalpha_noapi.py +++ b/searx/engines/wolframalpha_noapi.py @@ -159,9 +159,9 @@ def response(resp): results.append({'infobox': infobox_title, 'attributes': result_chunks, - 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]}) + 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]}) - results.append({'url': resp.request.headers['Referer'], + results.append({'url': resp.request.headers['Referer'].decode('utf8'), 'title': 'Wolfram|Alpha', 'content': infobox_title}) From 8f3b33de23090e6d11a5a29a239719bb6915d2ec Mon Sep 17 00:00:00 2001 From: a01200356 <a01200356@itesm.mx> Date: Sun, 28 Feb 2016 02:05:52 -0600 Subject: [PATCH 5/5] [fix] remove unnecesary async calls in wolframalpha_noapi setting async to false in the request did the job, lol. --- searx/engines/wolframalpha_noapi.py | 56 +-------------- tests/unit/engines/test_wolframalpha_api.py | 32 ++++----- tests/unit/engines/test_wolframalpha_noapi.py | 72 ++++++++----------- 3 files changed, 46 insertions(+), 114 deletions(-) diff --git a/searx/engines/wolframalpha_noapi.py b/searx/engines/wolframalpha_noapi.py index 7962d923c..59629b833 100644 --- a/searx/engines/wolframalpha_noapi.py +++ b/searx/engines/wolframalpha_noapi.py @@ -20,7 +20,7 @@ from searx.poolrequests import get as http_get url = 'https://www.wolframalpha.com/' search_url = url + 'input/json.jsp'\ - '?async=true'\ + '?async=false'\ '&banners=raw'\ '&debuggingdata=false'\ '&format=image,plaintext,imagemap,minput,moutput'\ @@ -38,14 +38,6 @@ referer_url = url + 'input/?{query}' token = {'value': '', '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 # this pods do return a plaintext, but they look better and are more useful as images image_pods = {'VisualRepresentation', @@ -79,42 +71,6 @@ def request(query, 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 def response(resp): results = [] @@ -132,15 +88,7 @@ def response(resp): pod_title = pod.get('title', '') if 'subpods' not in pod: - # comment this section if your requests always reach timeout - if pod['async']: - result = get_async_pod(pod['async']) - if result: - pod = result - else: - continue - else: - continue + continue if pod_id == 'Input' or not infobox_title: infobox_title = pod['subpods'][0]['plaintext'] diff --git a/tests/unit/engines/test_wolframalpha_api.py b/tests/unit/engines/test_wolframalpha_api.py index 9dbe4d860..76404e192 100644 --- a/tests/unit/engines/test_wolframalpha_api.py +++ b/tests/unit/engines/test_wolframalpha_api.py @@ -87,16 +87,16 @@ class TestWolframAlphaAPIEngine(SearxTestCase): results = wolframalpha_api.response(response) self.assertEqual(type(results), list) 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.assertIn('Input', results[0]['attributes'][0]['label']) - self.assertIn('input_plaintext', results[0]['attributes'][0]['value']) - self.assertIn('Result', results[0]['attributes'][1]['label']) - self.assertIn('result_plaintext', results[0]['attributes'][1]['value']) - self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label']) - self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) - self.assertIn('illustration_img_alt', results[0]['attributes'][2]['image']['alt']) + 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) @@ -129,8 +129,8 @@ class TestWolframAlphaAPIEngine(SearxTestCase): <plaintext>integral_plaintext</plaintext> </subpod> </pod> - <pod title='Plot' - scanner='Plotter' + <pod title='Plot of the integral' + scanner='Integral' id='Plot' error='false' numsubpods='1'> @@ -147,14 +147,14 @@ class TestWolframAlphaAPIEngine(SearxTestCase): results = wolframalpha_api.response(response) self.assertEqual(type(results), list) 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.assertIn('Indefinite integral', results[0]['attributes'][0]['label']) - self.assertIn('integral_plaintext', results[0]['attributes'][0]['value']) - self.assertIn('Plot', results[0]['attributes'][1]['label']) - self.assertIn('plot.gif', results[0]['attributes'][1]['image']['src']) - self.assertIn('plot_alt', results[0]['attributes'][1]['image']['alt']) + 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) diff --git a/tests/unit/engines/test_wolframalpha_noapi.py b/tests/unit/engines/test_wolframalpha_noapi.py index 1129dc8b0..068c1be79 100644 --- a/tests/unit/engines/test_wolframalpha_noapi.py +++ b/tests/unit/engines/test_wolframalpha_noapi.py @@ -124,16 +124,16 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): results = wolframalpha_noapi.response(response) self.assertEqual(type(results), list) 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.assertIn('Input', results[0]['attributes'][0]['label']) - self.assertIn('input_plaintext', results[0]['attributes'][0]['value']) - self.assertIn('Result', results[0]['attributes'][1]['label']) - self.assertIn('result_plaintext', results[0]['attributes'][1]['value']) - self.assertIn('Manipulatives illustration', results[0]['attributes'][2]['label']) - self.assertIn('illustration_img_src.gif', results[0]['attributes'][2]['image']['src']) - self.assertIn('illustration_img_alt', results[0]['attributes'][2]['image']['alt']) + 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) @@ -184,8 +184,19 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): ], "id" : "Plot", "error" : false, - "numsubpods" : 0, - "async" : "invalid_async_url" + "numsubpods" : 1, + "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) self.assertEqual(type(results), list) 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.assertIn('Indefinite integral', results[0]['attributes'][0]['label']) - self.assertIn('integral_plaintext', results[0]['attributes'][0]['value']) + 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) @@ -206,33 +220,3 @@ class TestWolframAlphaNoAPIEngine(SearxTestCase): self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title']) self.assertEqual(referer_url, results[1]['url']) 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'])