.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.json_engine Namespace Reference

Functions

 iterate (iterable)
 
 is_iterable (obj)
 
 parse (query)
 
 do_query (data, q)
 
 query (data, query_string)
 
 request (query, params)
 
 identity (arg)
 
 extract_response_info (result)
 
 response (resp)
 

Variables

 search_url = None
 
str lang_all = 'en'
 
list no_result_for_http_status = []
 
int soft_max_redirects = 0
 
str method = 'GET'
 
str request_body = ''
 
dict cookies = {}
 
dict headers = {}
 
bool paging = False
 
int page_size = 1
 
int first_page_num = 1
 
str results_query = ''
 
 url_query = None
 
str url_prefix = ""
 
 title_query = None
 
 content_query = None
 
bool thumbnail_query = False
 
str thumbnail_prefix = ''
 
str suggestion_query = ''
 
bool title_html_to_text = False
 
bool content_html_to_text = False
 
bool time_range_support = False
 
str time_range_url = '&hours={time_range_val}'
 
dict time_range_map
 
bool safe_search_support = False
 
dict safe_search_map = {0: '&filter=none', 1: '&filter=moderate', 2: '&filter=strict'}
 

Detailed Description

The JSON engine is a *generic* engine with which it is possible to configure
engines in the settings.

Configuration
=============

Request:

- :py:obj:`search_url`
- :py:obj:`lang_all`
- :py:obj:`soft_max_redirects`
- :py:obj:`method`
- :py:obj:`request_body`
- :py:obj:`cookies`
- :py:obj:`headers`

Paging:

- :py:obj:`paging`
- :py:obj:`page_size`
- :py:obj:`first_page_num`

Time Range:

- :py:obj:`time_range_support`
- :py:obj:`time_range_url`
- :py:obj:`time_range_map`

Safe-Search:

- :py:obj:`safe_search_support`
- :py:obj:`safe_search_map`

Response:

- :py:obj:`title_html_to_text`
- :py:obj:`content_html_to_text`
- :py:obj:`no_result_for_http_status`

JSON query:

- :py:obj:`results_query`
- :py:obj:`url_query`
- :py:obj:`url_prefix`
- :py:obj:`title_query`
- :py:obj:`content_query`
- :py:obj:`thumbnail_query`
- :py:obj:`thumbnail_prefix`
- :py:obj:`suggestion_query`


Example
=======

Here is a simple example of a JSON engine configure in the :ref:`settings
engine` section, further read :ref:`engines-dev`.

.. code:: yaml

  - name : mdn
    engine : json_engine
    paging : True
    search_url : https://developer.mozilla.org/api/v1/search?q={query}&page={pageno}
    results_query : documents
    url_query : mdn_url
    url_prefix : https://developer.mozilla.org
    title_query : title
    content_query : summary

Implementations
===============

Function Documentation

◆ do_query()

searx.engines.json_engine.do_query ( data,
q )

Definition at line 281 of file json_engine.py.

281def do_query(data, q): # pylint: disable=invalid-name
282 ret = []
283 if not q:
284 return ret
285
286 qkey = q[0]
287
288 for key, value in iterate(data):
289
290 if len(q) == 1:
291 if key == qkey:
292 ret.append(value)
293 elif is_iterable(value):
294 ret.extend(do_query(value, q))
295 else:
296 if not is_iterable(value):
297 continue
298 if key == qkey:
299 ret.extend(do_query(value, q[1:]))
300 else:
301 ret.extend(do_query(value, q))
302 return ret
303
304

References searx.engines.json_engine.do_query(), searx.engines.json_engine.is_iterable(), and searx.engines.json_engine.iterate().

Referenced by searx.engines.json_engine.do_query(), and searx.engines.json_engine.query().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ extract_response_info()

searx.engines.json_engine.extract_response_info ( result)

Definition at line 355 of file json_engine.py.

355def extract_response_info(result):
356 title_filter = html_to_text if title_html_to_text else identity
357 content_filter = html_to_text if content_html_to_text else identity
358
359 tmp_result = {}
360
361 try:
362 url = query(result, url_query)[0]
363 tmp_result['url'] = url_prefix + to_string(url)
364
365 title = query(result, title_query)[0]
366 tmp_result['title'] = title_filter(to_string(title))
367 except: # pylint: disable=bare-except
368 return None
369
370 try:
371 content = query(result, content_query)[0]
372 tmp_result['content'] = content_filter(to_string(content))
373 except: # pylint: disable=bare-except
374 tmp_result['content'] = ""
375
376 try:
377 if thumbnail_query:
378 thumbnail_query_result = query(result, thumbnail_query)[0]
379 tmp_result['thumbnail'] = thumbnail_prefix + to_string(thumbnail_query_result)
380 except: # pylint: disable=bare-except
381 pass
382
383 return tmp_result
384
385

Referenced by searx.engines.json_engine.response().

+ Here is the caller graph for this function:

◆ identity()

searx.engines.json_engine.identity ( arg)

Definition at line 351 of file json_engine.py.

351def identity(arg):
352 return arg
353
354

◆ is_iterable()

searx.engines.json_engine.is_iterable ( obj)

Definition at line 266 of file json_engine.py.

266def is_iterable(obj):
267 if isinstance(obj, str):
268 return False
269 return isinstance(obj, Iterable)
270
271

Referenced by searx.engines.json_engine.do_query().

+ Here is the caller graph for this function:

◆ iterate()

searx.engines.json_engine.iterate ( iterable)

Definition at line 256 of file json_engine.py.

256def iterate(iterable):
257 if isinstance(iterable, dict):
258 items = iterable.items()
259
260 else:
261 items = enumerate(iterable)
262 for index, value in items:
263 yield str(index), value
264
265

Referenced by searx.engines.json_engine.do_query().

+ Here is the caller graph for this function:

◆ parse()

searx.engines.json_engine.parse ( query)

Definition at line 272 of file json_engine.py.

272def parse(query): # pylint: disable=redefined-outer-name
273 q = [] # pylint: disable=invalid-name
274 for part in query.split('/'):
275 if part == '':
276 continue
277 q.append(part)
278 return q
279
280

◆ query()

searx.engines.json_engine.query ( data,
query_string )

Definition at line 305 of file json_engine.py.

305def query(data, query_string):
306 q = parse(query_string)
307
308 return do_query(data, q)
309
310

References searx.engines.json_engine.do_query().

+ Here is the call graph for this function:

◆ request()

searx.engines.json_engine.request ( query,
params )
Build request parameters (see :ref:`engine request`).

Definition at line 311 of file json_engine.py.

311def request(query, params): # pylint: disable=redefined-outer-name
312 '''Build request parameters (see :ref:`engine request`).'''
313 lang = lang_all
314 if params['language'] != 'all':
315 lang = params['language'][:2]
316
317 time_range = ''
318 if params.get('time_range'):
319 time_range_val = time_range_map.get(params.get('time_range'))
320 time_range = time_range_url.format(time_range_val=time_range_val)
321
322 safe_search = ''
323 if params['safesearch']:
324 safe_search = safe_search_map[params['safesearch']]
325
326 fp = { # pylint: disable=invalid-name
327 'query': urlencode({'q': query})[2:],
328 'lang': lang,
329 'pageno': (params['pageno'] - 1) * page_size + first_page_num,
330 'time_range': time_range,
331 'safe_search': safe_search,
332 }
333
334 params['cookies'].update(cookies)
335 params['headers'].update(headers)
336
337 params['url'] = search_url.format(**fp)
338 params['method'] = method
339
340 if request_body:
341 # don't url-encode the query if it's in the request body
342 fp['query'] = query
343 params['data'] = request_body.format(**fp)
344
345 params['soft_max_redirects'] = soft_max_redirects
346 params['raise_for_httperror'] = False
347
348 return params
349
350

◆ response()

searx.engines.json_engine.response ( resp)
Scrap *results* from the response (see :ref:`engine results`).

Definition at line 386 of file json_engine.py.

386def response(resp):
387 '''Scrap *results* from the response (see :ref:`engine results`).'''
388 results = []
389
390 if no_result_for_http_status and resp.status_code in no_result_for_http_status:
391 return results
392
393 raise_for_httperror(resp)
394
395 if not resp.text:
396 return results
397
398 json = loads(resp.text)
399 is_onion = 'onions' in categories
400
401 if results_query:
402 rs = query(json, results_query) # pylint: disable=invalid-name
403 if not rs:
404 return results
405 rs = rs[0] # pylint: disable=invalid-name
406 else:
407 rs = json # pylint: disable=invalid-name
408
409 for result in rs:
410 tmp_result = extract_response_info(result)
411 if not tmp_result:
412 continue
413
414 if is_onion:
415 tmp_result['is_onion'] = True
416
417 results.append(tmp_result)
418
419 if not suggestion_query:
420 return results
421 for suggestion in query(json, suggestion_query):
422 results.append({'suggestion': suggestion})
423 return results

References searx.engines.json_engine.extract_response_info().

+ Here is the call graph for this function:

Variable Documentation

◆ content_html_to_text

bool searx.engines.json_engine.content_html_to_text = False

Definition at line 205 of file json_engine.py.

◆ content_query

searx.engines.json_engine.content_query = None

Definition at line 190 of file json_engine.py.

◆ cookies

dict searx.engines.json_engine.cookies = {}

Definition at line 154 of file json_engine.py.

◆ first_page_num

int searx.engines.json_engine.first_page_num = 1

Definition at line 169 of file json_engine.py.

◆ headers

dict searx.engines.json_engine.headers = {}

Definition at line 158 of file json_engine.py.

◆ lang_all

str searx.engines.json_engine.lang_all = 'en'

Definition at line 115 of file json_engine.py.

◆ method

str searx.engines.json_engine.method = 'GET'

Definition at line 131 of file json_engine.py.

◆ no_result_for_http_status

list searx.engines.json_engine.no_result_for_http_status = []

Definition at line 120 of file json_engine.py.

◆ page_size

int searx.engines.json_engine.page_size = 1

Definition at line 165 of file json_engine.py.

◆ paging

bool searx.engines.json_engine.paging = False

Definition at line 162 of file json_engine.py.

◆ request_body

str searx.engines.json_engine.request_body = ''

Definition at line 134 of file json_engine.py.

◆ results_query

str searx.engines.json_engine.results_query = ''

Definition at line 172 of file json_engine.py.

◆ safe_search_map

dict searx.engines.json_engine.safe_search_map = {0: '&filter=none', 1: '&filter=moderate', 2: '&filter=strict'}

Definition at line 242 of file json_engine.py.

◆ safe_search_support

bool searx.engines.json_engine.safe_search_support = False

Definition at line 239 of file json_engine.py.

◆ search_url

searx.engines.json_engine.search_url = None

Definition at line 82 of file json_engine.py.

◆ soft_max_redirects

int searx.engines.json_engine.soft_max_redirects = 0

Definition at line 128 of file json_engine.py.

◆ suggestion_query

str searx.engines.json_engine.suggestion_query = ''

Definition at line 199 of file json_engine.py.

◆ thumbnail_prefix

str searx.engines.json_engine.thumbnail_prefix = ''

Definition at line 196 of file json_engine.py.

◆ thumbnail_query

bool searx.engines.json_engine.thumbnail_query = False

Definition at line 193 of file json_engine.py.

◆ time_range_map

dict searx.engines.json_engine.time_range_map
Initial value:
1= {
2 'day': 24,
3 'week': 24 * 7,
4 'month': 24 * 30,
5 'year': 24 * 365,
6}

Definition at line 221 of file json_engine.py.

◆ time_range_support

bool searx.engines.json_engine.time_range_support = False

Definition at line 208 of file json_engine.py.

◆ time_range_url

str searx.engines.json_engine.time_range_url = '&hours={time_range_val}'

Definition at line 211 of file json_engine.py.

◆ title_html_to_text

bool searx.engines.json_engine.title_html_to_text = False

Definition at line 202 of file json_engine.py.

◆ title_query

searx.engines.json_engine.title_query = None

Definition at line 187 of file json_engine.py.

◆ url_prefix

str searx.engines.json_engine.url_prefix = ""

Definition at line 184 of file json_engine.py.

◆ url_query

searx.engines.json_engine.url_query = None

Definition at line 181 of file json_engine.py.