| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | """The JSON engine is a *generic* engine with which it is possible to configure
 | 
					
						
							|  |  |  | engines in the settings. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | Configuration | 
					
						
							|  |  |  | ============= | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | Request: | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | - :py:obj:`search_url` | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | - :py:obj:`lang_all` | 
					
						
							|  |  |  | - :py:obj:`soft_max_redirects` | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | - :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` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | 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` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | Response: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - :py:obj:`title_html_to_text` | 
					
						
							|  |  |  | - :py:obj:`content_html_to_text` | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | - :py:obj:`no_result_for_http_status` | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | JSON query: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - :py:obj:`results_query` | 
					
						
							|  |  |  | - :py:obj:`url_query` | 
					
						
							|  |  |  | - :py:obj:`url_prefix` | 
					
						
							|  |  |  | - :py:obj:`title_query` | 
					
						
							|  |  |  | - :py:obj:`content_query` | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | - :py:obj:`thumbnail_query` | 
					
						
							|  |  |  | - :py:obj:`thumbnail_prefix` | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | - :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 | 
					
						
							|  |  |  | =============== | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 12:52:08 +02:00
										 |  |  | from collections.abc import Iterable | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2021-02-10 16:40:03 +01:00
										 |  |  | from searx.utils import to_string, html_to_text | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | from searx.network import raise_for_httperror | 
					
						
							| 
									
										
										
										
											2016-11-30 18:43:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | search_url = None | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  | Search URL of the engine.  Example:: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  |     https://example.org/?search={query}&page={pageno}{time_range}{safe_search} | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Replacements are: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``{query}``: | 
					
						
							|  |  |  |   Search terms from user. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``{pageno}``: | 
					
						
							|  |  |  |   Page number if engine supports paging :py:obj:`paging` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | ``{lang}``: | 
					
						
							|  |  |  |   ISO 639-1 language code (en, de, fr ..) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``{time_range}``: | 
					
						
							|  |  |  |   :py:obj:`URL parameter <time_range_url>` if engine :py:obj:`supports time | 
					
						
							|  |  |  |   range <time_range_support>`.  The value for the parameter is taken from | 
					
						
							|  |  |  |   :py:obj:`time_range_map`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``{safe_search}``: | 
					
						
							|  |  |  |   Safe-search :py:obj:`URL parameter <safe_search_map>` if engine | 
					
						
							|  |  |  |   :py:obj:`supports safe-search <safe_search_support>`.  The ``{safe_search}`` | 
					
						
							|  |  |  |   replacement is taken from the :py:obj:`safes_search_map`.  Filter results:: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       0: none, 1: moderate, 2:strict | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   If not supported, the URL parameter is an empty string. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | lang_all = 'en' | 
					
						
							|  |  |  | '''Replacement ``{lang}`` in :py:obj:`search_url` if language ``all`` is
 | 
					
						
							|  |  |  | selected. | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | no_result_for_http_status = [] | 
					
						
							|  |  |  | '''Return empty result for these HTTP status codes instead of throwing an error.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code:: yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     no_result_for_http_status: [] | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | soft_max_redirects = 0 | 
					
						
							|  |  |  | '''Maximum redirects, soft limit. Record an error but don't stop the engine''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | method = 'GET' | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Some engines might require to do POST requests for search.''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | request_body = '' | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''The body of the request.  This can only be used if different :py:obj:`method`
 | 
					
						
							|  |  |  | is set, e.g. ``POST``. For formatting see the documentation of :py:obj:`search_url`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Note: Curly brackets which aren't encapsulating a replacement placeholder | 
					
						
							|  |  |  | must be escaped by doubling each ``{`` and ``}``. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code:: yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     request_body: >- | 
					
						
							|  |  |  |       {{ | 
					
						
							|  |  |  |         "search": "{query}", | 
					
						
							|  |  |  |         "page": {pageno}, | 
					
						
							|  |  |  |         "extra": {{ | 
					
						
							|  |  |  |           "time_range": {time_range}, | 
					
						
							|  |  |  |           "rating": "{safe_search}" | 
					
						
							|  |  |  |         }} | 
					
						
							|  |  |  |       }} | 
					
						
							|  |  |  | '''
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 23:26:55 +02:00
										 |  |  | cookies = {} | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Some engines might offer different result based on cookies.
 | 
					
						
							|  |  |  | Possible use-case: To set safesearch cookie.'''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 23:26:55 +02:00
										 |  |  | headers = {} | 
					
						
							|  |  |  | '''Some engines might offer different result based on cookies or headers.
 | 
					
						
							|  |  |  | Possible use-case: To set safesearch cookie or header to moderate.'''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | paging = False | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Engine supports paging [True or False].''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 11:26:29 +02:00
										 |  |  | page_size = 1 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Number of results on each page.  Only needed if the site requires not a page
 | 
					
						
							|  |  |  | number, but an offset.'''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 11:26:29 +02:00
										 |  |  | first_page_num = 1 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Number of the first page (usually 0 or 1).''' | 
					
						
							| 
									
										
										
										
											2016-07-16 11:26:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | results_query = '' | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''JSON query for the list of result items.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The query string is a slash `/` separated path of JSON key names. | 
					
						
							|  |  |  | Array entries can be specified using the index or can be omitted entirely, | 
					
						
							|  |  |  | in which case each entry is considered - | 
					
						
							|  |  |  | most implementations will default to the first entry in this case. | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | url_query = None | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''JSON query of result's ``url``. For the query string documentation see :py:obj:`results_query`''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | url_prefix = "" | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''String to prepend to the result's ``url``.''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | title_query = None | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''JSON query of result's ``title``. For the query string documentation see :py:obj:`results_query`''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | content_query = None | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''JSON query of result's ``content``. For the query string documentation see :py:obj:`results_query`''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | thumbnail_query = False | 
					
						
							|  |  |  | '''JSON query of result's ``thumbnail``. For the query string documentation see :py:obj:`results_query`''' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | thumbnail_prefix = '' | 
					
						
							|  |  |  | '''String to prepend to the result's ``thumbnail``.''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | suggestion_query = '' | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''JSON query of result's ``suggestion``. For the query string documentation see :py:obj:`results_query`''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | title_html_to_text = False | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Extract text from a HTML title string''' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | content_html_to_text = False | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | '''Extract text from a HTML content string''' | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  | time_range_support = False | 
					
						
							|  |  |  | '''Engine supports search time range.''' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | time_range_url = '&hours={time_range_val}' | 
					
						
							|  |  |  | '''Time range URL parameter in the in :py:obj:`search_url`.  If no time range is
 | 
					
						
							|  |  |  | requested by the user, the URL parameter is an empty string.  The | 
					
						
							|  |  |  | ``{time_range_val}`` replacement is taken from the :py:obj:`time_range_map`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code:: yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     time_range_url : '&days={time_range_val}' | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | time_range_map = { | 
					
						
							|  |  |  |     'day': 24, | 
					
						
							|  |  |  |     'week': 24 * 7, | 
					
						
							|  |  |  |     'month': 24 * 30, | 
					
						
							|  |  |  |     'year': 24 * 365, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | '''Maps time range value from user to ``{time_range_val}`` in
 | 
					
						
							|  |  |  | :py:obj:`time_range_url`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code:: yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     time_range_map: | 
					
						
							|  |  |  |       day: 1 | 
					
						
							|  |  |  |       week: 7 | 
					
						
							|  |  |  |       month: 30 | 
					
						
							|  |  |  |       year: 365 | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | safe_search_support = False | 
					
						
							|  |  |  | '''Engine supports safe-search.''' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | safe_search_map = {0: '&filter=none', 1: '&filter=moderate', 2: '&filter=strict'} | 
					
						
							|  |  |  | '''Maps safe-search value to ``{safe_search}`` in :py:obj:`search_url`.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code:: yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     safesearch: true | 
					
						
							|  |  |  |     safes_search_map: | 
					
						
							|  |  |  |       0: '&filter=none' | 
					
						
							|  |  |  |       1: '&filter=moderate' | 
					
						
							|  |  |  |       2: '&filter=strict' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | '''
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | def iterate(iterable): | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |     if isinstance(iterable, dict): | 
					
						
							|  |  |  |         items = iterable.items() | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |         items = enumerate(iterable) | 
					
						
							|  |  |  |     for index, value in items: | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |         yield str(index), value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | def is_iterable(obj): | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |     if isinstance(obj, str): | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  |         return False | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     return isinstance(obj, Iterable) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | def parse(query):  # pylint: disable=redefined-outer-name | 
					
						
							|  |  |  |     q = []  # pylint: disable=invalid-name | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     for part in query.split('/'): | 
					
						
							|  |  |  |         if part == '': | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |         q.append(part) | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     return q | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | def do_query(data, q):  # pylint: disable=invalid-name | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     ret = [] | 
					
						
							| 
									
										
										
										
											2014-02-11 13:13:51 +01:00
										 |  |  |     if not q: | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |         return ret | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     qkey = q[0] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  |     for key, value in iterate(data): | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if len(q) == 1: | 
					
						
							|  |  |  |             if key == qkey: | 
					
						
							|  |  |  |                 ret.append(value) | 
					
						
							|  |  |  |             elif is_iterable(value): | 
					
						
							|  |  |  |                 ret.extend(do_query(value, q)) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             if not is_iterable(value): | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             if key == qkey: | 
					
						
							|  |  |  |                 ret.extend(do_query(value, q[1:])) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 ret.extend(do_query(value, q)) | 
					
						
							|  |  |  |     return ret | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | def query(data, query_string): | 
					
						
							|  |  |  |     q = parse(query_string) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return do_query(data, q) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-20 02:31:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  | def request(query, params):  # pylint: disable=redefined-outer-name | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  |     '''Build request parameters (see :ref:`engine request`).''' | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  |     lang = lang_all | 
					
						
							|  |  |  |     if params['language'] != 'all': | 
					
						
							|  |  |  |         lang = params['language'][:2] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     time_range = '' | 
					
						
							|  |  |  |     if params.get('time_range'): | 
					
						
							|  |  |  |         time_range_val = time_range_map.get(params.get('time_range')) | 
					
						
							|  |  |  |         time_range = time_range_url.format(time_range_val=time_range_val) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     safe_search = '' | 
					
						
							|  |  |  |     if params['safesearch']: | 
					
						
							|  |  |  |         safe_search = safe_search_map[params['safesearch']] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     fp = {  # pylint: disable=invalid-name | 
					
						
							|  |  |  |         'query': urlencode({'q': query})[2:], | 
					
						
							|  |  |  |         'lang': lang, | 
					
						
							|  |  |  |         'pageno': (params['pageno'] - 1) * page_size + first_page_num, | 
					
						
							|  |  |  |         'time_range': time_range, | 
					
						
							|  |  |  |         'safe_search': safe_search, | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-07-16 11:26:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-10 23:26:55 +02:00
										 |  |  |     params['cookies'].update(cookies) | 
					
						
							|  |  |  |     params['headers'].update(headers) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-16 11:26:29 +02:00
										 |  |  |     params['url'] = search_url.format(**fp) | 
					
						
							| 
									
										
										
										
											2024-11-27 14:13:23 +01:00
										 |  |  |     params['method'] = method | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if request_body: | 
					
						
							|  |  |  |         # don't url-encode the query if it's in the request body | 
					
						
							|  |  |  |         fp['query'] = query | 
					
						
							|  |  |  |         params['data'] = request_body.format(**fp) | 
					
						
							| 
									
										
										
										
											2016-07-16 11:26:29 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  |     params['soft_max_redirects'] = soft_max_redirects | 
					
						
							|  |  |  |     params['raise_for_httperror'] = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 16:40:03 +01:00
										 |  |  | def identity(arg): | 
					
						
							|  |  |  |     return arg | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 16:47:50 +01:00
										 |  |  | def extract_response_info(result): | 
					
						
							|  |  |  |     title_filter = html_to_text if title_html_to_text else identity | 
					
						
							|  |  |  |     content_filter = html_to_text if content_html_to_text else identity | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     tmp_result = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         url = query(result, url_query)[0] | 
					
						
							|  |  |  |         tmp_result['url'] = url_prefix + to_string(url) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         title = query(result, title_query)[0] | 
					
						
							|  |  |  |         tmp_result['title'] = title_filter(to_string(title)) | 
					
						
							|  |  |  |     except:  # pylint: disable=bare-except | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         content = query(result, content_query)[0] | 
					
						
							|  |  |  |         tmp_result['content'] = content_filter(to_string(content)) | 
					
						
							|  |  |  |     except:  # pylint: disable=bare-except | 
					
						
							|  |  |  |         tmp_result['content'] = "" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         if thumbnail_query: | 
					
						
							|  |  |  |             thumbnail_query_result = query(result, thumbnail_query)[0] | 
					
						
							|  |  |  |             tmp_result['thumbnail'] = thumbnail_prefix + to_string(thumbnail_query_result) | 
					
						
							|  |  |  |     except:  # pylint: disable=bare-except | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return tmp_result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  |     '''Scrap *results* from the response (see :ref:`engine results`).''' | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  |     if no_result_for_http_status and resp.status_code in no_result_for_http_status: | 
					
						
							|  |  |  |         return results | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     raise_for_httperror(resp) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 01:51:33 +01:00
										 |  |  |     if not resp.text: | 
					
						
							|  |  |  |         return results | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     json = loads(resp.text) | 
					
						
							| 
									
										
										
										
											2025-01-11 02:25:40 +01:00
										 |  |  |     is_onion = 'onions' in categories | 
					
						
							| 
									
										
										
										
											2021-02-10 16:40:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 12:15:27 +02:00
										 |  |  |     if results_query: | 
					
						
							| 
									
										
										
										
											2024-03-11 07:45:08 +01:00
										 |  |  |         rs = query(json, results_query)  # pylint: disable=invalid-name | 
					
						
							|  |  |  |         if not rs: | 
					
						
							| 
									
										
										
										
											2017-11-02 00:43:29 +01:00
										 |  |  |             return results | 
					
						
							| 
									
										
										
										
											2025-01-11 16:47:50 +01:00
										 |  |  |         rs = rs[0]  # pylint: disable=invalid-name | 
					
						
							| 
									
										
										
										
											2016-08-12 12:15:27 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2025-01-11 16:47:50 +01:00
										 |  |  |         rs = json  # pylint: disable=invalid-name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for result in rs: | 
					
						
							|  |  |  |         tmp_result = extract_response_info(result) | 
					
						
							|  |  |  |         if not tmp_result: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if is_onion: | 
					
						
							|  |  |  |             tmp_result['is_onion'] = True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append(tmp_result) | 
					
						
							| 
									
										
										
										
											2016-08-12 12:15:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if not suggestion_query: | 
					
						
							|  |  |  |         return results | 
					
						
							|  |  |  |     for suggestion in query(json, suggestion_query): | 
					
						
							|  |  |  |         results.append({'suggestion': suggestion}) | 
					
						
							| 
									
										
										
										
											2013-11-19 15:49:52 +01:00
										 |  |  |     return results |