.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
stract.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Stract is an independent open source search engine. At this state, it's
3still in beta and hence this implementation will need to be updated once beta
4ends.
5
6"""
7
8from json import dumps
9from searx.utils import searx_useragent
10from searx.enginelib.traits import EngineTraits
11
12about = {
13 "website": "https://stract.com/",
14 "use_official_api": True,
15 "official_api_documentation": "https://stract.com/beta/api/docs/#/search/api",
16 "require_api_key": False,
17 "results": "JSON",
18}
19categories = ['general']
20paging = True
21
22base_url = "https://stract.com/beta/api"
23search_url = base_url + "/search"
24
25traits: EngineTraits
26
27
28def request(query, params):
29 params['url'] = search_url
30 params['method'] = "POST"
31 params['headers'] = {
32 'Accept': 'application/json',
33 'Content-Type': 'application/json',
34 'User-Agent': searx_useragent(),
35 }
36 region = traits.get_region(params["searxng_locale"], default=traits.all_locale)
37 params['data'] = dumps(
38 {
39 'query': query,
40 'page': params['pageno'] - 1,
41 'selectedRegion': region,
42 }
43 )
44
45 return params
46
47
48def response(resp):
49 results = []
50
51 for result in resp.json()["webpages"]:
52 results.append(
53 {
54 'url': result['url'],
55 'title': result['title'],
56 'content': ''.join(fragment['text'] for fragment in result['snippet']['text']['fragments']),
57 }
58 )
59
60 return results
61
62
63def fetch_traits(engine_traits: EngineTraits):
64 # pylint: disable=import-outside-toplevel
65 from searx import network
66 from babel import Locale, languages
67 from searx.locales import region_tag
68
69 territories = Locale("en").territories
70
71 json = network.get(base_url + "/docs/openapi.json").json()
72 regions = json['components']['schemas']['Region']['enum']
73
74 engine_traits.all_locale = regions[0]
75
76 for region in regions[1:]:
77 for code, name in territories.items():
78 if region not in (code, name):
79 continue
80 for lang in languages.get_official_languages(code, de_facto=True):
81 engine_traits.regions[region_tag(Locale(lang, code))] = region
fetch_traits(EngineTraits engine_traits)
Definition stract.py:63
request(query, params)
Definition stract.py:28