mirror of
				https://github.com/searxng/searxng.git
				synced 2025-11-04 03:27:06 -05:00 
			
		
		
		
	In [1] and [2] we discussed the need of a Result.results property and how we can
avoid unclear code.  This patch implements a class for the reslut-lists of
engines::
    searx.result_types.EngineResults
A simple example for the usage in engine development::
    from searx.result_types import EngineResults
    ...
    def response(resp) -> EngineResults:
        res = EngineResults()
        ...
        res.add( res.types.Answer(answer="lorem ipsum ..", url="https://example.org") )
        ...
        return res
[1] https://github.com/searxng/searxng/pull/4183#pullrequestreview-257400034
[2] https://github.com/searxng/searxng/pull/4183#issuecomment-2614301580
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
		
	
			
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# SPDX-License-Identifier: AGPL-3.0-or-later
 | 
						|
"""Deepl translation engine"""
 | 
						|
 | 
						|
from searx.result_types import EngineResults
 | 
						|
 | 
						|
about = {
 | 
						|
    "website": 'https://deepl.com',
 | 
						|
    "wikidata_id": 'Q43968444',
 | 
						|
    "official_api_documentation": 'https://www.deepl.com/docs-api',
 | 
						|
    "use_official_api": True,
 | 
						|
    "require_api_key": True,
 | 
						|
    "results": 'JSON',
 | 
						|
}
 | 
						|
 | 
						|
engine_type = 'online_dictionary'
 | 
						|
categories = ['general', 'translate']
 | 
						|
 | 
						|
url = 'https://api-free.deepl.com/v2/translate'
 | 
						|
api_key = None
 | 
						|
 | 
						|
 | 
						|
def request(_query, params):
 | 
						|
    '''pre-request callback
 | 
						|
 | 
						|
    params<dict>:
 | 
						|
 | 
						|
    - ``method`` : POST/GET
 | 
						|
    - ``headers``: {}
 | 
						|
    - ``data``: {}  # if method == POST
 | 
						|
    - ``url``: ''
 | 
						|
    - ``category``: 'search category'
 | 
						|
    - ``pageno``: 1  # number of the requested page
 | 
						|
    '''
 | 
						|
 | 
						|
    params['url'] = url
 | 
						|
    params['method'] = 'POST'
 | 
						|
    params['data'] = {'auth_key': api_key, 'text': params['query'], 'target_lang': params['to_lang'][1]}
 | 
						|
 | 
						|
    return params
 | 
						|
 | 
						|
 | 
						|
def response(resp) -> EngineResults:
 | 
						|
 | 
						|
    res = EngineResults()
 | 
						|
    data = resp.json()
 | 
						|
    if not data.get('translations'):
 | 
						|
        return res
 | 
						|
 | 
						|
    translations = [res.types.Translations.Item(text=t['text']) for t in data['translations']]
 | 
						|
    res.add(res.types.Translations(translations=translations))
 | 
						|
 | 
						|
    return res
 |