Browse Source

Merge pull request #2354 from dalf/fix-wikipedia

[fix] wikipedia engine: don't raise an error when the query is not found
Alexandre Flament 4 years ago
parent
commit
cdceec1cbb
3 changed files with 11 additions and 6 deletions
  1. 4 3
      docs/dev/engine_overview.rst
  2. 3 1
      searx/engines/wikipedia.py
  3. 4 2
      searx/search.py

+ 4 - 3
docs/dev/engine_overview.rst

@@ -134,9 +134,9 @@ The function ``def request(query, params):`` always returns the ``params``
 variable.  Inside searx, the following paramters can be used to specify a search
 request:
 
-================== =========== ========================================================================
+================== =========== ==========================================================================
 argument           type        information
-================== =========== ========================================================================
+================== =========== ==========================================================================
 url                string      requested url
 method             string      HTTP request method
 headers            set         HTTP header information
@@ -145,7 +145,8 @@ cookies            set         HTTP cookies
 verify             boolean     Performing SSL-Validity check
 max_redirects      int         maximum redirects, hard limit
 soft_max_redirects int         maximum redirects, soft limit. Record an error but don't stop the engine
-================== =========== ========================================================================
+raise_for_status   bool        True by default: raise an exception if the HTTP code of response is >= 300
+================== =========== ==========================================================================
 
 
 example code

+ 3 - 1
searx/engines/wikipedia.py

@@ -37,13 +37,15 @@ def request(query, params):
                                       language=url_lang(params['language']))
 
     params['headers']['User-Agent'] = searx_useragent()
+    params['raise_for_status'] = False
+    params['soft_max_redirects'] = 2
 
     return params
 
 
 # get response from search-request
 def response(resp):
-    if not resp.ok:
+    if resp.status_code == 404:
         return []
 
     results = []

+ 4 - 2
searx/search.py

@@ -143,7 +143,8 @@ def send_http_request(engine, request_params):
     response = req(request_params['url'], **request_args)
 
     # check HTTP status
-    response.raise_for_status()
+    if request_params.get('raise_for_status'):
+        response.raise_for_status()
 
     # check soft limit of the redirect count
     if len(response.history) > soft_max_redirects:
@@ -340,7 +341,8 @@ def default_request_params():
         'url': '',
         'cookies': {},
         'verify': True,
-        'auth': None
+        'auth': None,
+        'raise_for_status': True
     }