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>
		
	
			
		
			
				
	
	
		
			62 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# SPDX-License-Identifier: AGPL-3.0-or-later
 | 
						|
"""Mozhi (alternative frontend for popular translation engines)"""
 | 
						|
 | 
						|
import random
 | 
						|
import re
 | 
						|
import urllib.parse
 | 
						|
 | 
						|
from searx.result_types import EngineResults
 | 
						|
 | 
						|
about = {
 | 
						|
    "website": 'https://codeberg.org/aryak/mozhi',
 | 
						|
    "wikidata_id": None,
 | 
						|
    "official_api_documentation": 'https://mozhi.aryak.me/api/swagger/index.html',
 | 
						|
    "use_official_api": True,
 | 
						|
    "require_api_key": False,
 | 
						|
    "results": 'JSON',
 | 
						|
}
 | 
						|
 | 
						|
engine_type = 'online_dictionary'
 | 
						|
categories = ['general', 'translate']
 | 
						|
 | 
						|
base_url = "https://mozhi.aryak.me"
 | 
						|
mozhi_engine = "google"
 | 
						|
 | 
						|
re_transliteration_unsupported = "Direction '.*' is not supported"
 | 
						|
 | 
						|
 | 
						|
def request(_query, params):
 | 
						|
    request_url = random.choice(base_url) if isinstance(base_url, list) else base_url
 | 
						|
 | 
						|
    args = {'from': params['from_lang'][1], 'to': params['to_lang'][1], 'text': params['query'], 'engine': mozhi_engine}
 | 
						|
    params['url'] = f"{request_url}/api/translate?{urllib.parse.urlencode(args)}"
 | 
						|
    return params
 | 
						|
 | 
						|
 | 
						|
def response(resp) -> EngineResults:
 | 
						|
    res = EngineResults()
 | 
						|
    translation = resp.json()
 | 
						|
 | 
						|
    item = res.types.Translations.Item(text=translation['translated-text'])
 | 
						|
 | 
						|
    if translation['target_transliteration'] and not re.match(
 | 
						|
        re_transliteration_unsupported, translation['target_transliteration']
 | 
						|
    ):
 | 
						|
        item.transliteration = translation['target_transliteration']
 | 
						|
 | 
						|
    if translation['word_choices']:
 | 
						|
        for word in translation['word_choices']:
 | 
						|
            if word.get('definition'):
 | 
						|
                item.definitions.append(word['definition'])
 | 
						|
 | 
						|
            for example in word.get('examples_target', []):
 | 
						|
                item.examples.append(re.sub(r"<|>", "", example).lstrip('- '))
 | 
						|
 | 
						|
    item.synonyms = translation.get('source_synonyms', [])
 | 
						|
 | 
						|
    url = urllib.parse.urlparse(resp.search_params["url"])
 | 
						|
    # remove the api path
 | 
						|
    url = url._replace(path="", fragment="").geturl()
 | 
						|
    res.add(res.types.Translations(translations=[item], url=url))
 | 
						|
    return res
 |