logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git
commit: e7de9674b14c2ff06fac009274742819d6468138
parent dd4686a3886458f600427aba0ed7b9666b3644db
Author: Thomas Pointhuber <thomas.pointhuber@gmx.at>
Date:   Sun,  8 Feb 2015 21:53:37 +0100

[enh] add safesearch functionality

2 = strict
1 = moderate
0 = none

Diffstat:

Msearx/engines/bing_images.py8+++++++-
Msearx/engines/blekko_images.py11+++++++++++
Msearx/search.py5+++++
Msearx/templates/courgette/preferences.html10++++++++++
Msearx/templates/default/preferences.html10++++++++++
Msearx/templates/oscar/preferences.html11+++++++++++
Msearx/webapp.py12++++++++++++
7 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/searx/engines/bing_images.py b/searx/engines/bing_images.py @@ -21,12 +21,17 @@ import re # engine dependent config categories = ['images'] paging = True +safesearch = True # search-url base_url = 'https://www.bing.com/' search_string = 'images/search?{query}&count=10&first={offset}' thumb_url = "http://ts1.mm.bing.net/th?id={ihk}" +# safesearch definitions +safesearch_types = {2: 'STRICT', + 1: 'DEMOTE', + 0: 'OFF'} # do search-request def request(query, params): @@ -43,7 +48,8 @@ def request(query, params): offset=offset) params['cookies']['SRCHHPGUSR'] = \ - 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0] + 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0] +\ + '&ADLT=' + safesearch_types.get(params['safesearch'], 'DEMOTE') params['url'] = base_url + search_path diff --git a/searx/engines/blekko_images.py b/searx/engines/blekko_images.py @@ -14,11 +14,16 @@ from urllib import urlencode # engine dependent config categories = ['images'] paging = True +safesearch = True # search-url base_url = 'https://blekko.com' search_url = '/api/images?{query}&c={c}' +# safesearch definitions +safesearch_types = {2: '1', + 1: '', + 0: '0'} # do search-request def request(query, params): @@ -31,6 +36,12 @@ def request(query, params): if params['pageno'] != 1: params['url'] += '&page={pageno}'.format(pageno=(params['pageno']-1)) + # let Blekko know we wan't have profiling + params['cookies']['tag_lesslogging'] = '1' + + # parse safesearch argument + params['cookies']['safesearch'] = safesearch_types.get(params['safesearch'], '') + return params diff --git a/searx/search.py b/searx/search.py @@ -457,6 +457,11 @@ class Search(object): request_params['started'] = time() request_params['pageno'] = self.pageno request_params['language'] = self.lang + try: + # 0 = None, 1 = Moderate, 2 = Strict + request_params['safesearch'] = int(request.cookies.get('safesearch', 1)) + except ValueError: + request_params['safesearch'] = 1 # update request parameters dependent on # search-engine (contained in engines folder) diff --git a/searx/templates/courgette/preferences.html b/searx/templates/courgette/preferences.html @@ -60,6 +60,16 @@ </p> </fieldset> <fieldset> + <legend>{{ _('SafeSearch') }}</legend> + <p> + <select name='safesearch'> + <option value="2" {% if safesearch == '2' %}selected="selected"{% endif %}>{{ _('Strict') }}</option> + <option value="1" {% if safesearch == '1' %}selected="selected"{% endif %}>{{ _('Moderate') }}</option> + <option value="0" {% if safesearch == '0' %}selected="selected"{% endif %}>{{ _('None') }}</option> + </select> + </p> + </fieldset> + <fieldset> <legend>{{ _('Themes') }}</legend> <p> <select name="theme"> diff --git a/searx/templates/default/preferences.html b/searx/templates/default/preferences.html @@ -61,6 +61,16 @@ </p> </fieldset> <fieldset> + <legend>{{ _('SafeSearch') }}</legend> + <p> + <select name='safesearch'> + <option value="2" {% if safesearch == '2' %}selected="selected"{% endif %}>{{ _('Strict') }}</option> + <option value="1" {% if safesearch == '1' %}selected="selected"{% endif %}>{{ _('Moderate') }}</option> + <option value="0" {% if safesearch == '0' %}selected="selected"{% endif %}>{{ _('None') }}</option> + </select> + </p> + </fieldset> + <fieldset> <legend>{{ _('Themes') }}</legend> <p> <select name="theme"> diff --git a/searx/templates/oscar/preferences.html b/searx/templates/oscar/preferences.html @@ -88,6 +88,17 @@ <span class="col-sm-5 col-md-6 help-block">{{ _('Change how forms are submited, <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods" rel="external">learn more about request methods</a>') }}</span> </div> <div class="row form-group"> + <label class="col-sm-3 col-md-2">{{ _('SafeSearch') }}</label> + <div class="col-sm-4 col-md-4"> + <select class="form-control" name='safesearch'> + <option value="2" {% if safesearch == '2' %}selected="selected"{% endif %}>{{ _('Strict') }}</option> + <option value="1" {% if safesearch == '1' %}selected="selected"{% endif %}>{{ _('Moderate') }}</option> + <option value="0" {% if safesearch == '0' %}selected="selected"{% endif %}>{{ _('None') }}</option> + </select> + </div> + <span class="col-sm-5 col-md-6 help-block">{{ _('Filter explicite content') }}</span> + </div> + <div class="row form-group"> <label class="col-sm-3 col-md-2">{{ _('Themes') }}</label> <div class="col-sm-4 col-md-4"> <select class="form-control" name="theme"> diff --git a/searx/webapp.py b/searx/webapp.py @@ -267,6 +267,8 @@ def render(template_name, override_theme=None, **kwargs): kwargs['method'] = request.cookies.get('method', 'POST') + kwargs['safesearch'] = request.cookies.get('safesearch', '1') + # override url_for function in templates kwargs['url_for'] = url_for_theme @@ -455,6 +457,10 @@ def preferences(): Settings that are going to be saved as cookies.""" lang = None image_proxy = request.cookies.get('image_proxy', settings['server'].get('image_proxy')) + try: + savesearch = int(request.cookies.get('savesearch', 1)) + except ValueError: + savesearch = 1 if request.cookies.get('language')\ and request.cookies['language'] in (x[0] for x in language_codes): @@ -471,6 +477,8 @@ def preferences(): locale = None autocomplete = '' method = 'POST' + safesearch = '1' + for pd_name, pd in request.form.items(): if pd_name.startswith('category_'): category = pd_name[9:] @@ -489,6 +497,8 @@ def preferences(): lang = pd elif pd_name == 'method': method = pd + elif pd_name == 'safesearch': + safesearch = pd elif pd_name.startswith('engine_'): if pd_name.find('__') > -1: engine_name, category = pd_name.replace('engine_', '', 1).split('__', 1) @@ -529,6 +539,8 @@ def preferences(): ) resp.set_cookie('method', method, max_age=cookie_max_age) + + resp.set_cookie('safesearch', safesearch, max_age=cookie_max_age) resp.set_cookie('image_proxy', image_proxy, max_age=cookie_max_age)