.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 searxng_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
25
26def request(query, params):
27 params['url'] = search_url
28 params['method'] = "POST"
29 params['headers'] = {
30 'Accept': 'application/json',
31 'Content-Type': 'application/json',
32 'User-Agent': searxng_useragent(),
33 }
34 region = traits.get_region(params["searxng_locale"], default=traits.all_locale)
35 params['data'] = dumps(
36 {
37 'query': query,
38 'page': params['pageno'] - 1,
39 'selectedRegion': region,
40 }
41 )
42
43 return params
44
45
46def response(resp):
47 results = []
48
49 for result in resp.json()["webpages"]:
50 results.append(
51 {
52 'url': result['url'],
53 'title': result['title'],
54 'content': ''.join(fragment['text'] for fragment in result['snippet']['text']['fragments']),
55 }
56 )
57
58 return results
59
60
61def fetch_traits(engine_traits: EngineTraits):
62 # pylint: disable=import-outside-toplevel
63 from searx import network
64 from babel import Locale, languages
65 from searx.locales import region_tag
66
67 territories = Locale("en").territories
68
69 json = network.get(base_url + "/docs/openapi.json").json()
70 regions = json['components']['schemas']['Region']['enum']
71
72 engine_traits.all_locale = regions[0]
73
74 for region in regions[1:]:
75 for code, name in territories.items():
76 if region not in (code, name):
77 continue
78 for lang in languages.get_official_languages(code, de_facto=True):
79 engine_traits.regions[region_tag(Locale(lang, code))] = region
fetch_traits(EngineTraits engine_traits)
Definition stract.py:61
request(query, params)
Definition stract.py:26