[enh] engine: stract - add language/region support

This commit is contained in:
0xhtml 2024-09-29 02:27:57 +02:00 committed by Markus Heiser
parent b5009b8610
commit 0a0fb450b5
1 changed files with 34 additions and 2 deletions

View File

@ -7,6 +7,7 @@ ends.
from json import dumps from json import dumps
from searx.utils import searx_useragent from searx.utils import searx_useragent
from searx.enginelib.traits import EngineTraits
about = { about = {
"website": "https://stract.com/", "website": "https://stract.com/",
@ -18,7 +19,10 @@ about = {
categories = ['general'] categories = ['general']
paging = True paging = True
search_url = "https://stract.com/beta/api/search" base_url = "https://stract.com/beta/api"
search_url = base_url + "/search"
traits: EngineTraits
def request(query, params): def request(query, params):
@ -29,7 +33,14 @@ def request(query, params):
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'User-Agent': searx_useragent(), 'User-Agent': searx_useragent(),
} }
params['data'] = dumps({'query': query, 'page': params['pageno'] - 1}) region = traits.get_region(params["searxng_locale"], default=traits.all_locale)
params['data'] = dumps(
{
'query': query,
'page': params['pageno'] - 1,
'selectedRegion': region,
}
)
return params return params
@ -47,3 +58,24 @@ def response(resp):
) )
return results return results
def fetch_traits(engine_traits: EngineTraits):
# pylint: disable=import-outside-toplevel
from searx import network
from babel import Locale, languages
from searx.locales import region_tag
territories = Locale("en").territories
json = network.get(base_url + "/docs/openapi.json").json()
regions = json['components']['schemas']['Region']['enum']
engine_traits.all_locale = regions[0]
for region in regions[1:]:
for code, name in territories.items():
if region not in (code, name):
continue
for lang in languages.get_official_languages(code, de_facto=True):
engine_traits.regions[region_tag(Locale(lang, code))] = region