| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | # lint: pylint | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | """This engine uses the Qwant API (https://api.qwant.com/v3) to implement Qwant
 | 
					
						
							|  |  |  | -Web, -News, -Images and -Videos.  The API is undocumented but can be reverse | 
					
						
							|  |  |  | engineered by reading the network log of https://www.qwant.com/ queries. | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | For Qwant's *web-search* two alternatives are implemented: | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | - ``web``: uses the :py:obj:`api_url` which returns a JSON structure | 
					
						
							|  |  |  | - ``web-lite``: uses the :py:obj:`web_lite_url` which returns a HTML page | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Configuration | 
					
						
							|  |  |  | ============= | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | The engine has the following additional settings: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - :py:obj:`qwant_categ` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This implementation is used by different qwant engines in the :ref:`settings.yml | 
					
						
							|  |  |  | <settings engine>`: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | .. code:: yaml | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   - name: qwant | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |     qwant_categ: web-lite  # alternatively use 'web' | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     ... | 
					
						
							|  |  |  |   - name: qwant news | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |     qwant_categ: news | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     ... | 
					
						
							|  |  |  |   - name: qwant images | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |     qwant_categ: images | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     ... | 
					
						
							|  |  |  |   - name: qwant videos | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |     qwant_categ: videos | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     ... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | Implementations | 
					
						
							|  |  |  | =============== | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | from datetime import ( | 
					
						
							|  |  |  |     datetime, | 
					
						
							|  |  |  |     timedelta, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-12-10 21:27:47 +01:00
										 |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2020-08-06 17:42:46 +02:00
										 |  |  | from urllib.parse import urlencode | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  | from flask_babel import gettext | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  | import babel | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | import lxml | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-17 15:45:07 +02:00
										 |  |  | from searx.exceptions import SearxEngineAPIException, SearxEngineTooManyRequestsException | 
					
						
							| 
									
										
										
											
												[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
   * can contains network definition
   * propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
     keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
   * retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
   * local_addresses can be "192.168.0.1/24" (it supports IPv6)
   * support_ipv4 & support_ipv6: both True by default
     see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
   * either a full network description
   * either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
											
										 
											2021-04-05 10:43:33 +02:00
										 |  |  | from searx.network import raise_for_httperror | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  | from searx.enginelib.traits import EngineTraits | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | from searx.utils import ( | 
					
						
							|  |  |  |     eval_xpath, | 
					
						
							|  |  |  |     eval_xpath_list, | 
					
						
							|  |  |  |     extract_text, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  | traits: EngineTraits | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 11:31:25 +01:00
										 |  |  | # about | 
					
						
							|  |  |  | about = { | 
					
						
							|  |  |  |     "website": 'https://www.qwant.com/', | 
					
						
							|  |  |  |     "wikidata_id": 'Q14657870', | 
					
						
							|  |  |  |     "official_api_documentation": None, | 
					
						
							|  |  |  |     "use_official_api": True, | 
					
						
							|  |  |  |     "require_api_key": False, | 
					
						
							|  |  |  |     "results": 'JSON', | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							| 
									
										
										
										
											2020-11-03 11:35:53 +01:00
										 |  |  | categories = [] | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2023-11-14 08:25:06 +01:00
										 |  |  | max_page = 5 | 
					
						
							|  |  |  | """5 pages maximum (``&p=5``): Trying to do more just results in an improper
 | 
					
						
							|  |  |  | redirect"""
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | qwant_categ = None | 
					
						
							| 
									
										
										
										
											2023-09-15 09:53:03 +02:00
										 |  |  | """One of ``web-lite`` (or ``web``), ``news``, ``images`` or ``videos``""" | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 18:37:46 +02:00
										 |  |  | safesearch = True | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | # safe_search_map = {0: '&safesearch=0', 1: '&safesearch=1', 2: '&safesearch=2'} | 
					
						
							| 
									
										
										
										
											2022-08-12 18:37:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  | # fmt: off | 
					
						
							|  |  |  | qwant_news_locales = [ | 
					
						
							|  |  |  |     'ca_ad', 'ca_es', 'ca_fr', 'co_fr', 'de_at', 'de_ch', 'de_de', 'en_au', | 
					
						
							|  |  |  |     'en_ca', 'en_gb', 'en_ie', 'en_my', 'en_nz', 'en_us', 'es_ad', 'es_ar', | 
					
						
							|  |  |  |     'es_cl', 'es_co', 'es_es', 'es_mx', 'es_pe', 'eu_es', 'eu_fr', 'fc_ca', | 
					
						
							|  |  |  |     'fr_ad', 'fr_be', 'fr_ca', 'fr_ch', 'fr_fr', 'it_ch', 'it_it', 'nl_be', | 
					
						
							|  |  |  |     'nl_nl', 'pt_ad', 'pt_pt', | 
					
						
							|  |  |  | ] | 
					
						
							|  |  |  | # fmt: on | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | # search-url | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | api_url = 'https://api.qwant.com/v3/search/' | 
					
						
							|  |  |  | """URL of Qwant's API (JSON)""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | web_lite_url = 'https://lite.qwant.com/' | 
					
						
							|  |  |  | """URL of Qwant-Lite (HTML)""" | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     """Qwant search request""" | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if not query: | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |     q_locale = traits.get_region(params["searxng_locale"], default='en_US') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     url = api_url + f'{qwant_categ}?' | 
					
						
							|  |  |  |     args = {'q': query} | 
					
						
							|  |  |  |     params['raise_for_httperror'] = False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-18 00:15:57 +02:00
										 |  |  |     if qwant_categ == 'web-lite': | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         url = web_lite_url + '?' | 
					
						
							|  |  |  |         args['locale'] = q_locale.lower() | 
					
						
							|  |  |  |         args['l'] = q_locale.split('_')[0] | 
					
						
							|  |  |  |         args['s'] = params['safesearch'] | 
					
						
							|  |  |  |         args['p'] = params['pageno'] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         params['raise_for_httperror'] = True | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |     elif qwant_categ == 'images': | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         args['locale'] = q_locale | 
					
						
							|  |  |  |         args['safesearch'] = params['safesearch'] | 
					
						
							|  |  |  |         args['count'] = 50 | 
					
						
							| 
									
										
										
										
											2023-09-18 00:15:57 +02:00
										 |  |  |         args['offset'] = (params['pageno'] - 1) * args['count'] | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |     else:  # web, news, videos | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |         args['locale'] = q_locale | 
					
						
							|  |  |  |         args['safesearch'] = params['safesearch'] | 
					
						
							|  |  |  |         args['count'] = 10 | 
					
						
							| 
									
										
										
										
											2023-09-18 00:15:57 +02:00
										 |  |  |         args['offset'] = (params['pageno'] - 1) * args['count'] | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = url + urlencode(args) | 
					
						
							| 
									
										
										
										
											2022-08-12 18:37:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if qwant_categ == 'web-lite': | 
					
						
							|  |  |  |         return parse_web_lite(resp) | 
					
						
							| 
									
										
										
										
											2023-09-17 17:52:56 +02:00
										 |  |  |     return parse_web_api(resp) | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def parse_web_lite(resp): | 
					
						
							|  |  |  |     """Parse results from Qwant-Lite""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  |     dom = lxml.html.fromstring(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for item in eval_xpath_list(dom, '//section/article'): | 
					
						
							| 
									
										
										
										
											2023-09-19 12:55:21 +02:00
										 |  |  |         if eval_xpath(item, "./span[contains(@class, 'tooltip')]"): | 
					
						
							|  |  |  |             # ignore randomly interspersed advertising adds | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |         results.append( | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-09-19 12:55:21 +02:00
										 |  |  |                 'url': extract_text(eval_xpath(item, "./span[contains(@class, 'url partner')]")), | 
					
						
							| 
									
										
										
										
											2023-09-15 19:14:07 +02:00
										 |  |  |                 'title': extract_text(eval_xpath(item, './h2/a')), | 
					
						
							|  |  |  |                 'content': extract_text(eval_xpath(item, './p')), | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def parse_web_api(resp): | 
					
						
							|  |  |  |     """Parse results from Qwant's API""" | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  |     # pylint: disable=too-many-locals, too-many-branches, too-many-statements | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2020-12-09 21:23:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # load JSON result | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  |     search_results = loads(resp.text) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     data = search_results.get('data', {}) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 21:23:20 +01:00
										 |  |  |     # check for an API error | 
					
						
							|  |  |  |     if search_results.get('status') != 'success': | 
					
						
							| 
									
										
										
										
											2023-09-17 15:45:07 +02:00
										 |  |  |         error_code = data.get('error_code') | 
					
						
							|  |  |  |         if error_code == 24: | 
					
						
							|  |  |  |             raise SearxEngineTooManyRequestsException() | 
					
						
							|  |  |  |         msg = ",".join(data.get('message', ['unknown'])) | 
					
						
							|  |  |  |         raise SearxEngineAPIException(f"{msg} ({error_code})") | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # raise for other errors | 
					
						
							|  |  |  |     raise_for_httperror(resp) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |     if qwant_categ == 'web': | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |         # The WEB query contains a list named 'mainline'.  This list can contain | 
					
						
							|  |  |  |         # different result types (e.g. mainline[0]['type'] returns type of the | 
					
						
							|  |  |  |         # result items in mainline[0]['items'] | 
					
						
							|  |  |  |         mainline = data.get('result', {}).get('items', {}).get('mainline', {}) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         # Queries on News, Images and Videos do not have a list named 'mainline' | 
					
						
							|  |  |  |         # in the response.  The result items are directly in the list | 
					
						
							|  |  |  |         # result['items']. | 
					
						
							|  |  |  |         mainline = data.get('result', {}).get('items', []) | 
					
						
							|  |  |  |         mainline = [ | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |             {'type': qwant_categ, 'items': mainline}, | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |         ] | 
					
						
							| 
									
										
										
										
											2020-12-09 21:23:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  |     # return empty array if there are no results | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     if not mainline: | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     for row in mainline: | 
					
						
							|  |  |  |         mainline_type = row.get('type', 'web') | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |         if mainline_type != qwant_categ: | 
					
						
							| 
									
										
										
										
											2021-10-12 20:06:37 +02:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |         if mainline_type == 'ads': | 
					
						
							|  |  |  |             # ignore adds | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mainline_items = row.get('items', []) | 
					
						
							|  |  |  |         for item in mainline_items: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[fix] qwant engine - prevent API locale exception on lang 'all'
Has been reported in [1], error message::
    Error
        Error: searx.exceptions.SearxEngineAPIException
        Percentage: 0
        Parameters: ('API error::locale must be a string,locale must be one of
        the following values: en_gb, en_ie, en_us, en_ca, en_in, en_my, en_au,
        en_nz, cy_gb, gd_gb, de_de, de_ch, de_at, fr_fr, br_fr, fr_be, fr_ch,
        fr_ca, fr_ad, fc_ca, ec_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx,
        es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_br,
        pt_pt, pt_ad, nl_be, nl_nl, pl_pl, zh_hk, zh_cn, fi_fi, bg_bg, et_ee,
        hu_hu, da_dk, nb_no, sv_se, ko_kr, th_th, cs_cz, ro_ro, el_gr',)
        File name: searx/engines/qwant.py:114
        Function: response
        Code: raise SearxEngineAPIException('API error::' + msg)
[1] https://github.com/searxng/searxng/issues/222
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2021-07-24 14:45:32 +02:00
										 |  |  |             title = item.get('title', None) | 
					
						
							|  |  |  |             res_url = item.get('url', None) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if mainline_type == 'web': | 
					
						
							|  |  |  |                 content = item['desc'] | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 results.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         'title': title, | 
					
						
							|  |  |  |                         'url': res_url, | 
					
						
							|  |  |  |                         'content': content, | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |             elif mainline_type == 'news': | 
					
						
							| 
									
										
										
										
											2021-07-24 13:52:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 pub_date = item['date'] | 
					
						
							|  |  |  |                 if pub_date is not None: | 
					
						
							|  |  |  |                     pub_date = datetime.fromtimestamp(pub_date) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |                 news_media = item.get('media', []) | 
					
						
							| 
									
										
										
										
											2017-02-12 14:58:49 +01:00
										 |  |  |                 img_src = None | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |                 if news_media: | 
					
						
							|  |  |  |                     img_src = news_media[0].get('pict', {}).get('url', None) | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 results.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         'title': title, | 
					
						
							|  |  |  |                         'url': res_url, | 
					
						
							|  |  |  |                         'publishedDate': pub_date, | 
					
						
							|  |  |  |                         'img_src': img_src, | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             elif mainline_type == 'images': | 
					
						
							|  |  |  |                 thumbnail = item['thumbnail'] | 
					
						
							|  |  |  |                 img_src = item['media'] | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 results.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         'title': title, | 
					
						
							|  |  |  |                         'url': res_url, | 
					
						
							|  |  |  |                         'template': 'images.html', | 
					
						
							|  |  |  |                         'thumbnail_src': thumbnail, | 
					
						
							|  |  |  |                         'img_src': img_src, | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             elif mainline_type == 'videos': | 
					
						
							| 
									
										
										
										
											2021-07-16 15:32:12 +02:00
										 |  |  |                 # some videos do not have a description: while qwant-video | 
					
						
							|  |  |  |                 # returns an empty string, such video from a qwant-web query | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  |                 # miss the 'desc' key. | 
					
						
							| 
									
										
										
										
											2021-07-16 15:32:12 +02:00
										 |  |  |                 d, s, c = item.get('desc'), item.get('source'), item.get('channel') | 
					
						
							|  |  |  |                 content_parts = [] | 
					
						
							|  |  |  |                 if d: | 
					
						
							|  |  |  |                     content_parts.append(d) | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  |                 if s: | 
					
						
							| 
									
										
										
										
											2021-07-16 15:32:12 +02:00
										 |  |  |                     content_parts.append("%s: %s " % (gettext("Source"), s)) | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  |                 if c: | 
					
						
							| 
									
										
										
										
											2021-07-16 15:32:12 +02:00
										 |  |  |                     content_parts.append("%s: %s " % (gettext("Channel"), c)) | 
					
						
							|  |  |  |                 content = ' // '.join(content_parts) | 
					
						
							| 
									
										
										
										
											2021-07-24 13:52:36 +02:00
										 |  |  |                 length = item['duration'] | 
					
						
							|  |  |  |                 if length is not None: | 
					
						
							|  |  |  |                     length = timedelta(milliseconds=length) | 
					
						
							|  |  |  |                 pub_date = item['date'] | 
					
						
							|  |  |  |                 if pub_date is not None: | 
					
						
							|  |  |  |                     pub_date = datetime.fromtimestamp(pub_date) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |                 thumbnail = item['thumbnail'] | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  |                 # from some locations (DE and others?) the s2 link do | 
					
						
							|  |  |  |                 # response a 'Please wait ..' but does not deliver the thumbnail | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |                 thumbnail = thumbnail.replace('https://s2.qwant.com', 'https://s1.qwant.com', 1) | 
					
						
							|  |  |  |                 results.append( | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                         'title': title, | 
					
						
							|  |  |  |                         'url': res_url, | 
					
						
							|  |  |  |                         'content': content, | 
					
						
							|  |  |  |                         'publishedDate': pub_date, | 
					
						
							|  |  |  |                         'thumbnail': thumbnail, | 
					
						
							|  |  |  |                         'template': 'videos.html', | 
					
						
							|  |  |  |                         'length': length, | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-07-15 20:10:37 +02:00
										 |  |  |                 ) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return results | 
					
						
							| 
									
										
										
										
											2017-02-25 03:21:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  | def fetch_traits(engine_traits: EngineTraits): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # pylint: disable=import-outside-toplevel | 
					
						
							|  |  |  |     from searx import network | 
					
						
							|  |  |  |     from searx.locales import region_tag | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  |     resp = network.get(about['website']) | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  |     text = resp.text | 
					
						
							|  |  |  |     text = text[text.find('INITIAL_PROPS') :] | 
					
						
							|  |  |  |     text = text[text.find('{') : text.find('</script>')] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     q_initial_props = loads(text) | 
					
						
							|  |  |  |     q_locales = q_initial_props.get('locales') | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  |     eng_tag_list = set() | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for country, v in q_locales.items(): | 
					
						
							|  |  |  |         for lang in v['langs']: | 
					
						
							|  |  |  |             _locale = "{lang}_{country}".format(lang=lang, country=country) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if qwant_categ == 'news' and _locale.lower() not in qwant_news_locales: | 
					
						
							|  |  |  |                 # qwant-news does not support all locales from qwant-web: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  |             eng_tag_list.add(_locale) | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  |     for eng_tag in eng_tag_list: | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  |         try: | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  |             sxng_tag = region_tag(babel.Locale.parse(eng_tag, sep='_')) | 
					
						
							|  |  |  |         except babel.UnknownLocaleError: | 
					
						
							|  |  |  |             print("ERROR: can't determine babel locale of quant's locale %s" % eng_tag) | 
					
						
							| 
									
										
										
											
												[fix] qwant - API error::locale must be one ..
The request function should not request a language (aka locale) that is not
supported by qwant. Select a locale like zh-TW ends in qwant's API error:
  ERROR searx.engines.qwant news: exception : \
  API error::locale must be one of the following values: \
    en_gb, en_ie, en_us, en_ca, en_my, en_au, en_nz, de_de, de_ch, de_at, fr_fr, \
    fr_be, fr_ch, fr_ca, fr_ad, fc_ca, co_fr, es_es, es_ar, es_cl, es_co, es_mx, \
    es_pe, es_ad, ca_es, ca_ad, ca_fr, eu_es, eu_fr, it_it, it_ch, pt_pt, pt_ad, \
    nl_be, nl_nl
The existing searx.utils.match_language function is unsuitable for this purpose,
it is replaced by function searx.locales.get_engine_locale that is based on the
methods from the babel package.
The quant's _fetch_supported_languages function has been revised to filter out
languages 8aka locales) not supported by qwant.
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
											
										 
											2022-08-12 18:37:09 +02:00
										 |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 22:42:58 +02:00
										 |  |  |         conflict = engine_traits.regions.get(sxng_tag) | 
					
						
							|  |  |  |         if conflict: | 
					
						
							|  |  |  |             if conflict != eng_tag: | 
					
						
							|  |  |  |                 print("CONFLICT: babel %s --> %s, %s" % (sxng_tag, conflict, eng_tag)) | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         engine_traits.regions[sxng_tag] = eng_tag |