on master help sign in
Merge pull request #2354 from dalf/fix-wikipedia

[fix] wikipedia engine: don't raise an error when the query is not found

commitcdceec1cbb2ca894572396e0a68c2d09b0769231 authorAlexandre Flament <alex@al-f.net> dateFri 4 Dec 2020, 20:42 (6 years ago) parentd949428f0054d6
3 files changed+11−6
docs/dev/engine_overview.rst+4−3 searx/engines/wikipedia.py+3−1 searx/search.py+4−2
docs/dev/engine_overview.rst+4−3
@@ -134,9 +134,9 @@ The function ``def request(query, params):`` always returns the ``params``
134134variable. Inside searx, the following paramters can be used to specify a search
135135request:
136136
137−================== =========== ========================================================================
137+================== =========== ==========================================================================
138138argument type information
139−================== =========== ========================================================================
139+================== =========== ==========================================================================
140140url string requested url
141141method string HTTP request method
142142headers set HTTP header information
@@ -145,7 +145,8 @@ cookies set HTTP cookies
145145verify boolean Performing SSL-Validity check
146146max_redirects int maximum redirects, hard limit
147147soft_max_redirects int maximum redirects, soft limit. Record an error but don't stop the engine
148−================== =========== ========================================================================
148+raise_for_status bool True by default: raise an exception if the HTTP code of response is >= 300
149+================== =========== ==========================================================================
149150
150151
151152example code
searx/engines/wikipedia.py+3−1
@@ -37,13 +37,15 @@ def request(query, params):
3737 language=url_lang(params['language']))
3838
3939 params['headers']['User-Agent'] = searx_useragent()
40+ params['raise_for_status'] = False
41+ params['soft_max_redirects'] = 2
4042
4143 return params
4244
4345
4446# get response from search-request
4547def response(resp):
46− if not resp.ok:
48+ if resp.status_code == 404:
4749 return []
4850
4951 results = []
searx/search.py+4−2
@@ -143,7 +143,8 @@ def send_http_request(engine, request_params):
143143 response = req(request_params['url'], **request_args)
144144
145145 # check HTTP status
146− response.raise_for_status()
146+ if request_params.get('raise_for_status'):
147+ response.raise_for_status()
147148
148149 # check soft limit of the redirect count
149150 if len(response.history) > soft_max_redirects:
@@ -340,7 +341,8 @@ def default_request_params():
340341 'url': '',
341342 'cookies': {},
342343 'verify': True,
343− 'auth': None
344+ 'auth': None,
345+ 'raise_for_status': True
344346 }
345347
346348