From 78d3f3d6b116e0aca8af8b939d5a98ea74bb8de3 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sat, 27 Feb 2016 19:06:44 -0600 Subject: [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(-) (limited to 'searx') 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) + + pods = search_results.xpath(pods_xpath) + result_chunks = [] + for pod in pods: + pod_title = replace_pua_chars(pod.xpath(pod_title_xpath)[0]) + + 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}) - results.append({'answer': answer}) + # 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 [] - # 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) + 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/ -- cgit v1.2.3 From 4267b11a45b7427bcba91259fcda202bd049f004 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sat, 27 Feb 2016 19:19:04 -0600 Subject: [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(-) (limited to 'searx') 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 %} -- cgit v1.2.3 From 4d8996eb4d54a0938ad9dc6ad105de2b850fa033 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sun, 28 Feb 2016 00:47:36 -0600 Subject: [enh] unit tests for wolframalpha --- searx/engines/wolframalpha_api.py | 47 ++++++++++++++++------------ searx/engines/wolframalpha_noapi.py | 61 +++++++++++++++++++++---------------- searx/settings.yml | 4 +-- 3 files changed, 63 insertions(+), 49 deletions(-) (limited to 'searx') 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/ -- cgit v1.2.3 From 4cea71e3bb34020dff0f3e28f5c1398c0b0d8278 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sun, 28 Feb 2016 00:52:47 -0600 Subject: [fix] merge with 79705450dfdf321c19839bce23c56d9d4a86ba68 --- searx/engines/wolframalpha_api.py | 4 ++-- searx/engines/wolframalpha_noapi.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'searx') 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}) -- cgit v1.2.3 From 8f3b33de23090e6d11a5a29a239719bb6915d2ec Mon Sep 17 00:00:00 2001 From: a01200356 Date: Sun, 28 Feb 2016 02:05:52 -0600 Subject: [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 ++----------------------------------- 1 file changed, 2 insertions(+), 54 deletions(-) (limited to 'searx') 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'] -- cgit v1.2.3