From 9bc24080bf4c24a182cf2b5616095c2f6bea5821 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Fri, 13 Mar 2020 00:43:05 +0100 Subject: [fix] add answers, suggestions, corrections to rss output fixes #1888 --- searx/webapp.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index b661e39d1..da2bf34a9 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -637,9 +637,13 @@ def index(): response.headers.add('Content-Disposition', cont_disp) return response elif output_format == 'rss': + print(results) response_rss = render( 'opensearch_response_rss.xml', results=results, + answers=result_container.answers, + corrections=result_container.corrections, + suggestions=result_container.suggestions, q=request.form['q'], number_of_results=number_of_results, base_url=get_base_url(), -- cgit v1.2.3 From 018b6818419a1c3044b7d7244b55a62779063071 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Fri, 13 Mar 2020 00:50:19 +0100 Subject: [fix] add answers, suggestions, corrections to csv output fixes #1888 --- searx/webapp.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index da2bf34a9..49129d14e 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -626,10 +626,20 @@ def index(): mimetype='application/json') elif output_format == 'csv': csv = UnicodeWriter(StringIO()) - keys = ('title', 'url', 'content', 'host', 'engine', 'score') + keys = ('title', 'url', 'content', 'host', 'engine', 'score', 'type') csv.writerow(keys) for row in results: row['host'] = row['parsed_url'].netloc + row['type'] = 'result' + csv.writerow([row.get(key, '') for key in keys]) + for a in result_container.answers: + row = {'title': a, 'type': 'answer'} + csv.writerow([row.get(key, '') for key in keys]) + for a in result_container.suggestions: + row = {'title': a, 'type': 'suggestion'} + csv.writerow([row.get(key, '') for key in keys]) + for a in result_container.corrections: + row = {'title': a, 'type': 'correction'} csv.writerow([row.get(key, '') for key in keys]) csv.stream.seek(0) response = Response(csv.stream.read(), mimetype='application/csv') -- cgit v1.2.3 From 58a630308a95f886db9de19d562e11890ac35e07 Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Fri, 13 Mar 2020 00:57:01 +0100 Subject: [fix] convert query to string to produce valid filename for csv output --- searx/webapp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index 49129d14e..0c5ed4570 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -643,7 +643,7 @@ def index(): csv.writerow([row.get(key, '') for key in keys]) csv.stream.seek(0) response = Response(csv.stream.read(), mimetype='application/csv') - cont_disp = 'attachment;Filename=searx_-_{0}.csv'.format(search_query.query) + cont_disp = 'attachment;Filename=searx_-_{0}.csv'.format(search_query.query.decode('utf-8')) response.headers.add('Content-Disposition', cont_disp) return response elif output_format == 'rss': -- cgit v1.2.3 From baca55c94e002abbdeb428b31ab92f558195adda Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Mon, 16 Mar 2020 00:22:15 +0100 Subject: [fix] handle weights in accept language parsing - fixes w3ms en;q=1.0 --- searx/webapp.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index 0c5ed4570..cfb552514 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -178,9 +178,12 @@ flask_babel.get_translations = _get_translations def _get_browser_language(request, lang_list): for lang in request.headers.get("Accept-Language", "en").split(","): + if ';' in lang: + lang = lang.split(';')[0] locale = match_language(lang, lang_list, fallback=None) if locale is not None: return locale + return settings['search']['default_lang'] or 'en' @babel.localeselector -- cgit v1.2.3 From 822aee94a2e50923252faf7ae11b4b03017c1a1a Mon Sep 17 00:00:00 2001 From: Adam Tauber Date: Mon, 16 Mar 2020 00:22:38 +0100 Subject: [fix] remove debug print --- searx/webapp.py | 1 - 1 file changed, 1 deletion(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index cfb552514..b3928921e 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -650,7 +650,6 @@ def index(): response.headers.add('Content-Disposition', cont_disp) return response elif output_format == 'rss': - print(results) response_rss = render( 'opensearch_response_rss.xml', results=results, -- cgit v1.2.3 From 04c687403e21f883f9614e6a24df9ec450cfc111 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Wed, 25 Mar 2020 11:49:33 +0100 Subject: [fix] brands: add variables from build env to jinja templating We have some variables in the build environment which are also needed in the templating process. Theses variables are relavant if one creates a fork with its own branding. We treat these variables under the term 'brands'. Signed-off-by: Markus Heiser --- searx/webapp.py | 3 +++ 1 file changed, 3 insertions(+) mode change 100644 => 100755 searx/webapp.py (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py old mode 100644 new mode 100755 index b3928921e..c6b52d6ab --- a/searx/webapp.py +++ b/searx/webapp.py @@ -57,6 +57,7 @@ from babel.support import Translations import flask_babel from flask_babel import Babel, gettext, format_date, format_decimal from flask.json import jsonify +from searx import brand from searx import settings, searx_dir, searx_debug from searx.exceptions import SearxParameterException from searx.engines import ( @@ -427,6 +428,8 @@ def render(template_name, override_theme=None, **kwargs): kwargs['preferences'] = request.preferences + kwargs['brand'] = brand + kwargs['scripts'] = set() for plugin in request.user_plugins: for script in plugin.js_dependencies: -- cgit v1.2.3 From ace7d30aed0bdff07e97cdb3900c38633877cdcd Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Wed, 25 Mar 2020 17:12:02 +0100 Subject: webapp.py: partial code review (no functional change) Signed-off-by: Markus Heiser --- searx/webapp.py | 69 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 28 deletions(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index c6b52d6ab..3af5c57b4 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -958,34 +958,47 @@ def clear_cookies(): @app.route('/config') def config(): - return jsonify({'categories': list(categories.keys()), - 'engines': [{'name': name, - 'categories': engine.categories, - 'shortcut': engine.shortcut, - 'enabled': not engine.disabled, - 'paging': engine.paging, - 'language_support': engine.language_support, - 'supported_languages': - list(engine.supported_languages.keys()) - if isinstance(engine.supported_languages, dict) - else engine.supported_languages, - 'safesearch': engine.safesearch, - 'time_range_support': engine.time_range_support, - 'timeout': engine.timeout} - for name, engine in engines.items() if request.preferences.validate_token(engine)], - 'plugins': [{'name': plugin.name, - 'enabled': plugin.default_on} - for plugin in plugins], - 'instance_name': settings['general']['instance_name'], - 'locales': settings['locales'], - 'default_locale': settings['ui']['default_locale'], - 'autocomplete': settings['search']['autocomplete'], - 'safe_search': settings['search']['safe_search'], - 'default_theme': settings['ui']['default_theme'], - 'version': VERSION_STRING, - 'doi_resolvers': [r for r in settings['doi_resolvers']], - 'default_doi_resolver': settings['default_doi_resolver'], - }) + """Return configuration in JSON format.""" + _engines = [] + for name, engine in engines.items(): + if not request.preferences.validate_token(engine): + continue + + supported_languages = engine.supported_languages + if isinstance(engine.supported_languages, dict): + supported_languages = list(engine.supported_languages.keys()) + + _engines.append({ + 'name': name, + 'categories': engine.categories, + 'shortcut': engine.shortcut, + 'enabled': not engine.disabled, + 'paging': engine.paging, + 'language_support': engine.language_support, + 'supported_languages': supported_languages, + 'safesearch': engine.safesearch, + 'time_range_support': engine.time_range_support, + 'timeout': engine.timeout + }) + + _plugins = [] + for _ in plugins: + _plugins.append({'name': _.name, 'enabled': _.default_on}) + + return jsonify({ + 'categories': list(categories.keys()), + 'engines': _engines, + 'plugins': _plugins, + 'instance_name': settings['general']['instance_name'], + 'locales': settings['locales'], + 'default_locale': settings['ui']['default_locale'], + 'autocomplete': settings['search']['autocomplete'], + 'safe_search': settings['search']['safe_search'], + 'default_theme': settings['ui']['default_theme'], + 'version': VERSION_STRING, + 'doi_resolvers': [r for r in settings['doi_resolvers']], + 'default_doi_resolver': settings['default_doi_resolver'], + }) @app.errorhandler(404) -- cgit v1.2.3 From 4d6482823764c840415a6023d945b5699a53ba2a Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Wed, 25 Mar 2020 17:28:32 +0100 Subject: webapp.py: expose the brand variable in the /config URL. E.g. helpful for searx-stats2 Signed-off-by: Markus Heiser --- searx/webapp.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index 3af5c57b4..c910230ab 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -996,6 +996,10 @@ def config(): 'safe_search': settings['search']['safe_search'], 'default_theme': settings['ui']['default_theme'], 'version': VERSION_STRING, + 'brand': { + 'GIT_URL': brand.GIT_URL, + 'DOCS_URL': brand.DOCS_URL + }, 'doi_resolvers': [r for r in settings['doi_resolvers']], 'default_doi_resolver': settings['default_doi_resolver'], }) -- cgit v1.2.3 From ba7c8d7b960c3a3f288db162a51b76a2a935a605 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Wed, 15 Apr 2020 23:24:12 +0200 Subject: [fix] remove usage of request context where not available --- searx/webapp.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index c910230ab..9d76d8441 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -56,6 +56,7 @@ from flask import ( from babel.support import Translations import flask_babel from flask_babel import Babel, gettext, format_date, format_decimal +from flask.ctx import has_request_context from flask.json import jsonify from searx import brand from searx import settings, searx_dir, searx_debug @@ -165,13 +166,11 @@ _flask_babel_get_translations = flask_babel.get_translations # monkey patch for flask_babel.get_translations def _get_translations(): - translation_locale = request.form.get('use-translation') - if translation_locale: + if has_request_context() and request.form.get('use-translation') == 'oc': babel_ext = flask_babel.current_app.extensions['babel'] - translation = Translations.load(next(babel_ext.translation_directories), 'oc') - else: - translation = _flask_babel_get_translations() - return translation + return Translations.load(next(babel_ext.translation_directories), 'oc') + + return _flask_babel_get_translations() flask_babel.get_translations = _get_translations -- cgit v1.2.3 From bce3830b8dd9322e4e93d0003aa86e03c03e2dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Fri, 17 Apr 2020 16:31:02 +0200 Subject: [fix] translate engine errors to Occitan when configured --- searx/webapp.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index 9d76d8441..8c3531069 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -626,7 +626,7 @@ def index(): 'corrections': list(result_container.corrections), 'infoboxes': result_container.infoboxes, 'suggestions': list(result_container.suggestions), - 'unresponsive_engines': list(result_container.unresponsive_engines)}, + 'unresponsive_engines': __get_translated_errors(result_container.unresponsive_engines)}, # noqa default=lambda item: list(item) if isinstance(item, set) else item), mimetype='application/json') elif output_format == 'csv': @@ -694,7 +694,7 @@ def index(): corrections=correction_urls, infoboxes=result_container.infoboxes, paging=result_container.paging, - unresponsive_engines=result_container.unresponsive_engines, + unresponsive_engines=__get_translated_errors(result_container.unresponsive_engines), current_language=match_language(search_query.lang, LANGUAGE_CODES, fallback=request.preferences.get_value("language")), @@ -705,6 +705,16 @@ def index(): ) +def __get_translated_errors(unresponsive_engines): + translated_errors = [] + for unresponsive_engine in unresponsive_engines: + error_msg = gettext(unresponsive_engine[1]) + if unresponsive_engine[2]: + error_msg = "{} {}".format(error_msg, unresponsive_engine[2]) + translated_errors.append((unresponsive_engine[0], error_msg)) + return translated_errors + + @app.route('/about', methods=['GET']) def about(): """Render about page""" -- cgit v1.2.3