From 84a943f867f6a17e760e849d840e329142fff2e5 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sun, 23 May 2021 22:26:18 +0200 Subject: [PATCH] [enh] XPath engine - add time safe-search support Signed-off-by: Markus Heiser --- searx/engines/xpath.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/searx/engines/xpath.py b/searx/engines/xpath.py index 41d3e17ff..e14f982b9 100644 --- a/searx/engines/xpath.py +++ b/searx/engines/xpath.py @@ -29,7 +29,11 @@ logger = logger.getChild('XPath engine') search_url = None """ -Search URL of the engine, replacements are: +Search URL of the engine. Example:: + + https://example.org/?search={query}&page={pageno}{time_range}{safe_search} + +Replacements are: ``{query}``: Search terms from user. @@ -45,6 +49,15 @@ Search URL of the engine, replacements are: range `. The value for the parameter is taken from :py:obj:`time_range_map`. +``{safe_search}``: + Safe-search :py:obj:`URL parameter ` if engine + :py:obj:`supports safe-search `. The ``{safe_search}`` + replacement is taken from the :py:obj:`safes_search_map`. Filter results:: + + 0: none, 1: moderate, 2:strict + + If not supported, the URL paramter is an empty string. + """ lang_all='en' @@ -117,6 +130,26 @@ time_range_map = { year: 365 ''' +safe_search_support = False +'''Engine supports safe-search.''' + +safe_search_map = { + 0: '&filter=none', + 1: '&filter=moderate', + 2: '&filter=strict' +} +'''Maps safe-search value to ``{safe_search}`` in :py:obj:`search_url`. + +.. code:: yaml + + safesearch: true + safes_search_map: + 0: '&filter=none' + 1: '&filter=moderate' + 2: '&filter=strict' + +''' + def request(query, params): '''Build request parameters (see :ref:`engine request`). @@ -130,11 +163,16 @@ def request(query, params): time_range_val = time_range_map.get(params.get('time_range')) time_range = time_range_url.format(time_range_val=time_range_val) + safe_search = '' + if params['safesearch']: + safe_search = safe_search_map[params['safesearch']] + fargs = { 'query': urlencode({'q': query})[2:], 'lang': lang, 'pageno': (params['pageno'] - 1) * page_size + first_page_num, 'time_range' : time_range, + 'safe_search' : safe_search, } params['url'] = search_url.format(**fargs)