| 
									
										
										
										
											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 | 
					
						
							|  |  |  | """Qwant (Web, News, Images, Videos)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This engine uses the Qwant API (https://api.qwant.com/v3). The API is | 
					
						
							|  |  |  | undocumented but can be reverse engineered by reading the network log of | 
					
						
							|  |  |  | https://www.qwant.com/ queries. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | This implementation is used by different qwant engines in the settings.yml:: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - name: qwant | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |     qwant_categ: 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
										 |  |  |     ... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | from searx.exceptions import SearxEngineAPIException | 
					
						
							| 
									
										
										
											
												[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 | 
					
						
							| 
									
										
										
											
												[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
										 |  |  | from searx.locales import get_engine_locale | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-05-03 02:24:28 +02:00
										 |  |  | supported_languages_url = about['website'] | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  | qwant_categ = None  # web|news|inages|videos | 
					
						
							| 
									
										
										
										
											2015-06-02 20:36:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 18:37:46 +02:00
										 |  |  | safesearch = True | 
					
						
							|  |  |  | safe_search_map = {0: '&safesearch=0', 1: '&safesearch=1', 2: '&safesearch=2'} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												[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 | 
					
						
							| 
									
										
										
										
											2021-11-17 18:13:54 +01:00
										 |  |  | url = 'https://api.qwant.com/v3/search/{keyword}?{query}&count={count}&offset={offset}' | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     count = 10  # web: count must be equal to 10 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |     if qwant_categ == 'images': | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |         count = 50 | 
					
						
							|  |  |  |         offset = (params['pageno'] - 1) * count | 
					
						
							|  |  |  |         # count + offset must be lower than 250 | 
					
						
							|  |  |  |         offset = min(offset, 199) | 
					
						
							| 
									
										
										
										
											2015-06-02 22:11:47 +02:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |         offset = (params['pageno'] - 1) * count | 
					
						
							|  |  |  |         # count + offset must be lower than 50 | 
					
						
							|  |  |  |         offset = min(offset, 40) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     params['url'] = url.format( | 
					
						
							| 
									
										
										
										
											2022-08-10 09:45:48 +02:00
										 |  |  |         keyword=qwant_categ, | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         query=urlencode({'q': query}), | 
					
						
							|  |  |  |         offset=offset, | 
					
						
							|  |  |  |         count=count, | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +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
										 |  |  |     # add quant's locale | 
					
						
							|  |  |  |     q_locale = get_engine_locale(params['language'], supported_languages, default='en_US') | 
					
						
							|  |  |  |     params['url'] += '&locale=' + q_locale | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-12 18:37:46 +02:00
										 |  |  |     # add safesearch option | 
					
						
							|  |  |  |     params['url'] += safe_search_map.get(params['safesearch'], '') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 21:23:20 +01:00
										 |  |  |     params['raise_for_httperror'] = False | 
					
						
							| 
									
										
										
										
											2015-06-01 00:00:32 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |     """Get response from Qwant's search request""" | 
					
						
							| 
									
										
										
										
											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': | 
					
						
							| 
									
										
										
										
											2021-12-27 09:26:22 +01:00
										 |  |  |         msg = ",".join( | 
					
						
							|  |  |  |             data.get( | 
					
						
							|  |  |  |                 'message', | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'unknown', | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2021-07-13 18:16:09 +02:00
										 |  |  |         raise SearxEngineAPIException('API error::' + msg) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # 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
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _fetch_supported_languages(resp): | 
					
						
							| 
									
										
										
											
												[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') | 
					
						
							|  |  |  |     q_valid_locales = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             q_valid_locales.append(_locale) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     supported_languages = {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for q_locale in q_valid_locales: | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             locale = babel.Locale.parse(q_locale, sep='_') | 
					
						
							|  |  |  |         except babel.core.UnknownLocaleError: | 
					
						
							|  |  |  |             print("ERROR: can't determine babel locale of quant's locale %s" % q_locale) | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # note: supported_languages (dict) | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         #   dict's key is a string build up from a babel.Locale object / the | 
					
						
							|  |  |  |         #   notation 'xx-XX' (and 'xx') conforms to SearXNG's locale (and | 
					
						
							|  |  |  |         #   language) notation and dict's values are the locale strings used by | 
					
						
							|  |  |  |         #   the engine. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         searxng_locale = locale.language + '-' + locale.territory  # --> params['language'] | 
					
						
							|  |  |  |         supported_languages[searxng_locale] = q_locale | 
					
						
							| 
									
										
										
										
											2017-02-25 03:21:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return supported_languages |