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