Merge pull request #220 from pointhi/safesearch

Safesearch
This commit is contained in:
Adam Tauber 2015-02-09 10:47:40 +01:00
commit 67df3c516f
10 changed files with 75 additions and 3 deletions

View File

@ -21,12 +21,18 @@ import re
# engine dependent config # engine dependent config
categories = ['images'] categories = ['images']
paging = True paging = True
safesearch = True
# search-url # search-url
base_url = 'https://www.bing.com/' base_url = 'https://www.bing.com/'
search_string = 'images/search?{query}&count=10&first={offset}' search_string = 'images/search?{query}&count=10&first={offset}'
thumb_url = "http://ts1.mm.bing.net/th?id={ihk}" thumb_url = "http://ts1.mm.bing.net/th?id={ihk}"
# safesearch definitions
safesearch_types = {2: 'STRICT',
1: 'DEMOTE',
0: 'OFF'}
# do search-request # do search-request
def request(query, params): def request(query, params):
@ -43,7 +49,8 @@ def request(query, params):
offset=offset) offset=offset)
params['cookies']['SRCHHPGUSR'] = \ 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 params['url'] = base_url + search_path

View File

@ -14,11 +14,17 @@ from urllib import urlencode
# engine dependent config # engine dependent config
categories = ['images'] categories = ['images']
paging = True paging = True
safesearch = True
# search-url # search-url
base_url = 'https://blekko.com' base_url = 'https://blekko.com'
search_url = '/api/images?{query}&c={c}' search_url = '/api/images?{query}&c={c}'
# safesearch definitions
safesearch_types = {2: '1',
1: '',
0: '0'}
# do search-request # do search-request
def request(query, params): def request(query, params):
@ -31,6 +37,12 @@ def request(query, params):
if params['pageno'] != 1: if params['pageno'] != 1:
params['url'] += '&page={pageno}'.format(pageno=(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 return params

View File

@ -15,18 +15,25 @@ from json import loads
# engine dependent config # engine dependent config
categories = ['images'] categories = ['images']
paging = True paging = True
safesearch = True
# search-url # search-url
url = 'https://ajax.googleapis.com/' url = 'https://ajax.googleapis.com/'
search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe=off&filter=off&{query}' search_url = url + 'ajax/services/search/images?v=1.0&start={offset}&rsz=large&safe={safesearch}&filter=off&{query}'
# do search-request # do search-request
def request(query, params): def request(query, params):
offset = (params['pageno'] - 1) * 8 offset = (params['pageno'] - 1) * 8
if params['safesearch'] == 0:
safesearch = 'off'
else:
safesearch = 'on'
params['url'] = search_url.format(query=urlencode({'q': query}), params['url'] = search_url.format(query=urlencode({'q': query}),
offset=offset) offset=offset,
safesearch=safesearch)
return params return params

View File

@ -457,6 +457,11 @@ class Search(object):
request_params['started'] = time() request_params['started'] = time()
request_params['pageno'] = self.pageno request_params['pageno'] = self.pageno
request_params['language'] = self.lang 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 # update request parameters dependent on
# search-engine (contained in engines folder) # search-engine (contained in engines folder)

View File

@ -59,6 +59,16 @@
</select> </select>
</p> </p>
</fieldset> </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> <fieldset>
<legend>{{ _('Themes') }}</legend> <legend>{{ _('Themes') }}</legend>
<p> <p>

View File

@ -60,6 +60,16 @@
</select> </select>
</p> </p>
</fieldset> </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> <fieldset>
<legend>{{ _('Themes') }}</legend> <legend>{{ _('Themes') }}</legend>
<p> <p>

View File

@ -87,6 +87,17 @@
</div> </div>
<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> <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>
<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"> <div class="row form-group">
<label class="col-sm-3 col-md-2">{{ _('Themes') }}</label> <label class="col-sm-3 col-md-2">{{ _('Themes') }}</label>
<div class="col-sm-4 col-md-4"> <div class="col-sm-4 col-md-4">

View File

@ -12,6 +12,7 @@ class TestBingImagesEngine(SearxTestCase):
dicto = defaultdict(dict) dicto = defaultdict(dict)
dicto['pageno'] = 1 dicto['pageno'] = 1
dicto['language'] = 'fr_FR' dicto['language'] = 'fr_FR'
dicto['safesearch'] = 1
params = bing_images.request(query, dicto) params = bing_images.request(query, dicto)
self.assertTrue('url' in params) self.assertTrue('url' in params)
self.assertTrue(query in params['url']) self.assertTrue(query in params['url'])

View File

@ -10,6 +10,7 @@ class TestBlekkoImagesEngine(SearxTestCase):
query = 'test_query' query = 'test_query'
dicto = defaultdict(dict) dicto = defaultdict(dict)
dicto['pageno'] = 0 dicto['pageno'] = 0
dicto['safesearch'] = 1
params = blekko_images.request(query, dicto) params = blekko_images.request(query, dicto)
self.assertTrue('url' in params) self.assertTrue('url' in params)
self.assertTrue(query in params['url']) self.assertTrue(query in params['url'])

View File

@ -267,6 +267,8 @@ def render(template_name, override_theme=None, **kwargs):
kwargs['method'] = request.cookies.get('method', 'POST') kwargs['method'] = request.cookies.get('method', 'POST')
kwargs['safesearch'] = request.cookies.get('safesearch', '1')
# override url_for function in templates # override url_for function in templates
kwargs['url_for'] = url_for_theme kwargs['url_for'] = url_for_theme
@ -471,6 +473,8 @@ def preferences():
locale = None locale = None
autocomplete = '' autocomplete = ''
method = 'POST' method = 'POST'
safesearch = '1'
for pd_name, pd in request.form.items(): for pd_name, pd in request.form.items():
if pd_name.startswith('category_'): if pd_name.startswith('category_'):
category = pd_name[9:] category = pd_name[9:]
@ -489,6 +493,8 @@ def preferences():
lang = pd lang = pd
elif pd_name == 'method': elif pd_name == 'method':
method = pd method = pd
elif pd_name == 'safesearch':
safesearch = pd
elif pd_name.startswith('engine_'): elif pd_name.startswith('engine_'):
if pd_name.find('__') > -1: if pd_name.find('__') > -1:
engine_name, category = pd_name.replace('engine_', '', 1).split('__', 1) engine_name, category = pd_name.replace('engine_', '', 1).split('__', 1)
@ -530,6 +536,8 @@ def preferences():
resp.set_cookie('method', method, max_age=cookie_max_age) 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) resp.set_cookie('image_proxy', image_proxy, max_age=cookie_max_age)
resp.set_cookie('theme', theme, max_age=cookie_max_age) resp.set_cookie('theme', theme, max_age=cookie_max_age)