2"""This module implements functions needed for the autocompleter.
9from urllib.parse
import urlencode, quote_plus
12from httpx
import HTTPError
14from searx
import settings
24 if 'timeout' not in kwargs:
25 kwargs[
'timeout'] = settings[
'outgoing'][
'request_timeout']
26 kwargs[
'raise_for_httperror'] =
True
29def get(*args, **kwargs):
31 return http_get(*args, **kwargs)
36 return http_post(*args, **kwargs)
41 url =
'https://search.brave.com/api/suggest?'
42 url += urlencode({
'q': query})
46 kwargs = {
'cookies': {
'country': country}}
47 resp = get(url, **kwargs)
60 autocomplete_url =
'https://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'
62 response = get(autocomplete_url + urlencode(dict(QueryString=query)))
67 dom = lxml.etree.fromstring(response.content)
68 results = dom.xpath(
'//Result/Label//text()')
74 """Autocomplete from DuckDuckGo. Supports DuckDuckGo's languages"""
76 traits = engines[
'duckduckgo'].traits
79 'kl': traits.get_region(sxng_locale, traits.all_locale),
82 url =
'https://duckduckgo.com/ac/?type=list&' + urlencode(args)
94 """Autocomplete from Google. Supports Google's languages and subdomains
95 (:py:obj:`searx.engines.google.get_google_info`) by using the async REST
98 https://{subdomain}/complete/search?{args}
102 google_info = google.get_google_info({
'searxng_locale': sxng_locale}, engines[
'google'].traits)
104 url =
'https://{subdomain}/complete/search?{args}'
109 'hl': google_info[
'params'][
'hl'],
113 resp = get(url.format(subdomain=google_info[
'subdomain'], args=args))
115 json_txt = resp.text[resp.text.find(
'[') : resp.text.find(
']', -3) + 1]
116 data = json.loads(json_txt)
118 results.append(lxml.html.fromstring(item[0]).text_content())
123 """Autocomplete from Mwmbl_."""
126 url =
'https://api.mwmbl.org/search/complete?{query}'
128 results = get(url.format(query=urlencode({
'q': query}))).json()[1]
131 return [result
for result
in results
if not result.startswith(
"go: ")
and not result.startswith(
"search: ")]
136 url =
'https://suggest.seznam.cz/fulltext/cs?{query}'
141 {
'phrase': query,
'cursorPosition': len(query),
'format':
'json-2',
'highlight':
'1',
'count':
'6'}
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'
159 url = f
"https://stract.com/beta/api/autosuggest?q={quote_plus(query)}"
166 return [html.unescape(suggestion[
'raw'])
for suggestion
in resp.json()]
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})))
175 return [e[
'text']
for e
in data.get(
'suggestions', [])
if 'text' in e]
180 url =
'https://swisscows.ch/api/suggest?{query}&itemsCount=5'
182 resp = json.loads(get(url.format(query=urlencode({
'query': query}))).text)
187 """Autocomplete from Qwant. Supports Qwant's regions."""
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'})))
196 if data[
'status'] ==
'success':
197 for item
in data[
'data'][
'items']:
198 results.append(item[
'value'])
204 """Autocomplete from Wikipedia. Supports Wikipedia's languages (aka netloc)."""
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')
210 url =
'https://{wiki_netloc}/w/api.php?{args}'
213 'action':
'opensearch',
215 'formatversion':
'2',
221 resp = get(url.format(args=args, wiki_netloc=wiki_netloc))
232 url =
"https://suggest.yandex.com/suggest-ff.cgi?{0}"
234 resp = json.loads(get(url.format(urlencode(dict(part=query)))).text)
242 'duckduckgo': duckduckgo,
243 'google': google_complete,
246 'startpage': startpage,
248 'swisscows': swisscows,
250 'wikipedia': wikipedia,
257 backend = backends.get(backend_name)
261 return backend(query, sxng_locale)
262 except (HTTPError, SearxEngineResponseException):
search_autocomplete(backend_name, query, sxng_locale)
qwant(query, sxng_locale)
wikipedia(query, sxng_locale)
google_complete(query, sxng_locale)
duckduckgo(query, sxng_locale)
startpage(query, sxng_locale)