.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
autocomplete.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""This module implements functions needed for the autocompleter.
3
4"""
5# pylint: disable=use-dict-literal
6
7import json
8import html
9from urllib.parse import urlencode, quote_plus
10
11import lxml.etree
12import lxml.html
13from httpx import HTTPError
14
15from searx.extended_types import SXNG_Response
16from searx import settings
17from searx.engines import (
18 engines,
19 google,
20)
21from searx.network import get as http_get, post as http_post
22from searx.exceptions import SearxEngineResponseException
23
24
25def update_kwargs(**kwargs):
26 if 'timeout' not in kwargs:
27 kwargs['timeout'] = settings['outgoing']['request_timeout']
28 kwargs['raise_for_httperror'] = True
29
30
31def get(*args, **kwargs) -> SXNG_Response:
32 update_kwargs(**kwargs)
33 return http_get(*args, **kwargs)
34
35
36def post(*args, **kwargs) -> SXNG_Response:
37 update_kwargs(**kwargs)
38 return http_post(*args, **kwargs)
39
40
41def baidu(query, _lang):
42 # baidu search autocompleter
43 base_url = "https://www.baidu.com/sugrec?"
44 response = get(base_url + urlencode({'ie': 'utf-8', 'json': 1, 'prod': 'pc', 'wd': query}))
45
46 results = []
47
48 if response.ok:
49 data = response.json()
50 if 'g' in data:
51 for item in data['g']:
52 results.append(item['q'])
53 return results
54
55
56def brave(query, _lang):
57 # brave search autocompleter
58 url = 'https://search.brave.com/api/suggest?'
59 url += urlencode({'q': query})
60 country = 'all'
61 # if lang in _brave:
62 # country = lang
63 kwargs = {'cookies': {'country': country}}
64 resp = get(url, **kwargs)
65
66 results = []
67
68 if resp.ok:
69 data = resp.json()
70 for item in data[1]:
71 results.append(item)
72 return results
73
74
75def dbpedia(query, _lang):
76 # dbpedia autocompleter, no HTTPS
77 autocomplete_url = 'https://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'
78
79 response = get(autocomplete_url + urlencode(dict(QueryString=query)))
80
81 results = []
82
83 if response.ok:
84 dom = lxml.etree.fromstring(response.content)
85 results = dom.xpath('//Result/Label//text()')
86
87 return results
88
89
90def duckduckgo(query, sxng_locale):
91 """Autocomplete from DuckDuckGo. Supports DuckDuckGo's languages"""
92
93 traits = engines['duckduckgo'].traits
94 args = {
95 'q': query,
96 'kl': traits.get_region(sxng_locale, traits.all_locale),
97 }
98
99 url = 'https://duckduckgo.com/ac/?type=list&' + urlencode(args)
100 resp = get(url)
101
102 ret_val = []
103 if resp.ok:
104 j = resp.json()
105 if len(j) > 1:
106 ret_val = j[1]
107 return ret_val
108
109
110def google_complete(query, sxng_locale):
111 """Autocomplete from Google. Supports Google's languages and subdomains
112 (:py:obj:`searx.engines.google.get_google_info`) by using the async REST
113 API::
114
115 https://{subdomain}/complete/search?{args}
116
117 """
118
119 google_info = google.get_google_info({'searxng_locale': sxng_locale}, engines['google'].traits)
120
121 url = 'https://{subdomain}/complete/search?{args}'
122 args = urlencode(
123 {
124 'q': query,
125 'client': 'gws-wiz',
126 'hl': google_info['params']['hl'],
127 }
128 )
129 results = []
130 resp = get(url.format(subdomain=google_info['subdomain'], args=args))
131 if resp and resp.ok:
132 json_txt = resp.text[resp.text.find('[') : resp.text.find(']', -3) + 1]
133 data = json.loads(json_txt)
134 for item in data[0]:
135 results.append(lxml.html.fromstring(item[0]).text_content())
136 return results
137
138
139def mwmbl(query, _lang):
140 """Autocomplete from Mwmbl_."""
141
142 # mwmbl autocompleter
143 url = 'https://api.mwmbl.org/search/complete?{query}'
144
145 results = get(url.format(query=urlencode({'q': query}))).json()[1]
146
147 # results starting with `go:` are direct urls and not useful for auto completion
148 return [result for result in results if not result.startswith("go: ") and not result.startswith("search: ")]
149
150
151def seznam(query, _lang):
152 # seznam search autocompleter
153 url = 'https://suggest.seznam.cz/fulltext/cs?{query}'
154
155 resp = get(
156 url.format(
157 query=urlencode(
158 {'phrase': query, 'cursorPosition': len(query), 'format': 'json-2', 'highlight': '1', 'count': '6'}
159 )
160 )
161 )
162
163 if not resp.ok:
164 return []
165
166 data = resp.json()
167 return [
168 ''.join([part.get('text', '') for part in item.get('text', [])])
169 for item in data.get('result', [])
170 if item.get('itemType', None) == 'ItemType.TEXT'
171 ]
172
173
174def stract(query, _lang):
175 # stract autocompleter (beta)
176 url = f"https://stract.com/beta/api/autosuggest?q={quote_plus(query)}"
177
178 resp = post(url)
179
180 if not resp.ok:
181 return []
182
183 return [html.unescape(suggestion['raw']) for suggestion in resp.json()]
184
185
186def swisscows(query, _lang):
187 # swisscows autocompleter
188 url = 'https://swisscows.ch/api/suggest?{query}&itemsCount=5'
189
190 resp = json.loads(get(url.format(query=urlencode({'query': query}))).text)
191 return resp
192
193
194def qwant(query, sxng_locale):
195 """Autocomplete from Qwant. Supports Qwant's regions."""
196 results = []
197
198 locale = engines['qwant'].traits.get_region(sxng_locale, 'en_US')
199 url = 'https://api.qwant.com/v3/suggest?{query}'
200 resp = get(url.format(query=urlencode({'q': query, 'locale': locale, 'version': '2'})))
201
202 if resp.ok:
203 data = resp.json()
204 if data['status'] == 'success':
205 for item in data['data']['items']:
206 results.append(item['value'])
207
208 return results
209
210
211def wikipedia(query, sxng_locale):
212 """Autocomplete from Wikipedia. Supports Wikipedia's languages (aka netloc)."""
213 results = []
214 eng_traits = engines['wikipedia'].traits
215 wiki_lang = eng_traits.get_language(sxng_locale, 'en')
216 wiki_netloc = eng_traits.custom['wiki_netloc'].get(wiki_lang, 'en.wikipedia.org') # type: ignore
217
218 url = 'https://{wiki_netloc}/w/api.php?{args}'
219 args = urlencode(
220 {
221 'action': 'opensearch',
222 'format': 'json',
223 'formatversion': '2',
224 'search': query,
225 'namespace': '0',
226 'limit': '10',
227 }
228 )
229 resp = get(url.format(args=args, wiki_netloc=wiki_netloc))
230 if resp.ok:
231 data = resp.json()
232 if len(data) > 1:
233 results = data[1]
234
235 return results
236
237
238def yandex(query, _lang):
239 # yandex autocompleter
240 url = "https://suggest.yandex.com/suggest-ff.cgi?{0}"
241
242 resp = json.loads(get(url.format(urlencode(dict(part=query)))).text)
243 if len(resp) > 1:
244 return resp[1]
245 return []
246
247
248backends = {
249 'baidu': baidu,
250 'brave': brave,
251 'dbpedia': dbpedia,
252 'duckduckgo': duckduckgo,
253 'google': google_complete,
254 'mwmbl': mwmbl,
255 'qwant': qwant,
256 'seznam': seznam,
257 'stract': stract,
258 'swisscows': swisscows,
259 'wikipedia': wikipedia,
260 'yandex': yandex,
261}
262
263
264def search_autocomplete(backend_name, query, sxng_locale):
265 backend = backends.get(backend_name)
266 if backend is None:
267 return []
268 try:
269 return backend(query, sxng_locale)
270 except (HTTPError, SearxEngineResponseException):
271 return []
search_autocomplete(backend_name, query, sxng_locale)
qwant(query, sxng_locale)
seznam(query, _lang)
yandex(query, _lang)
wikipedia(query, sxng_locale)
google_complete(query, sxng_locale)
dbpedia(query, _lang)
stract(query, _lang)
SXNG_Response get(*args, **kwargs)
brave(query, _lang)
update_kwargs(**kwargs)
swisscows(query, _lang)
duckduckgo(query, sxng_locale)
mwmbl(query, _lang)
baidu(query, _lang)
SXNG_Response post(*args, **kwargs)
::1337x
Definition 1337x.py:1