.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.autocomplete Namespace Reference

Functions

 update_kwargs (**kwargs)
 
SXNG_Response get (*args, **kwargs)
 
SXNG_Response post (*args, **kwargs)
 
 baidu (query, _lang)
 
 brave (query, _lang)
 
 dbpedia (query, _lang)
 
 duckduckgo (query, sxng_locale)
 
 google_complete (query, sxng_locale)
 
 mwmbl (query, _lang)
 
 seznam (query, _lang)
 
 stract (query, _lang)
 
 swisscows (query, _lang)
 
 qwant (query, sxng_locale)
 
 wikipedia (query, sxng_locale)
 
 yandex (query, _lang)
 
 search_autocomplete (backend_name, query, sxng_locale)
 

Variables

dict backends
 

Detailed Description

This module implements functions needed for the autocompleter.

Function Documentation

◆ baidu()

searx.autocomplete.baidu ( query,
_lang )

Definition at line 41 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ brave()

searx.autocomplete.brave ( query,
_lang )

Definition at line 56 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ dbpedia()

searx.autocomplete.dbpedia ( query,
_lang )

Definition at line 75 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ duckduckgo()

searx.autocomplete.duckduckgo ( query,
sxng_locale )
Autocomplete from DuckDuckGo. Supports DuckDuckGo's languages

Definition at line 90 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ get()

SXNG_Response searx.autocomplete.get ( * args,
** kwargs )

Definition at line 31 of file autocomplete.py.

31def get(*args, **kwargs) -> SXNG_Response:
32 update_kwargs(**kwargs)
33 return http_get(*args, **kwargs)
34
35

References update_kwargs().

Referenced by baidu(), brave(), dbpedia(), duckduckgo(), google_complete(), mwmbl(), qwant(), seznam(), swisscows(), wikipedia(), and yandex().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ google_complete()

searx.autocomplete.google_complete ( query,
sxng_locale )
Autocomplete from Google.  Supports Google's languages and subdomains
(:py:obj:`searx.engines.google.get_google_info`) by using the async REST
API::

    https://{subdomain}/complete/search?{args}

Definition at line 110 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ mwmbl()

searx.autocomplete.mwmbl ( query,
_lang )
Autocomplete from Mwmbl_.

Definition at line 139 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ post()

SXNG_Response searx.autocomplete.post ( * args,
** kwargs )

Definition at line 36 of file autocomplete.py.

36def post(*args, **kwargs) -> SXNG_Response:
37 update_kwargs(**kwargs)
38 return http_post(*args, **kwargs)
39
40

References update_kwargs().

Referenced by stract().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ qwant()

searx.autocomplete.qwant ( query,
sxng_locale )
Autocomplete from Qwant. Supports Qwant's regions.

Definition at line 194 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ search_autocomplete()

searx.autocomplete.search_autocomplete ( backend_name,
query,
sxng_locale )

Definition at line 264 of file autocomplete.py.

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 []

◆ seznam()

searx.autocomplete.seznam ( query,
_lang )

Definition at line 151 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ stract()

searx.autocomplete.stract ( query,
_lang )

Definition at line 174 of file autocomplete.py.

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

References post().

+ Here is the call graph for this function:

◆ swisscows()

searx.autocomplete.swisscows ( query,
_lang )

Definition at line 186 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ update_kwargs()

searx.autocomplete.update_kwargs ( ** kwargs)

Definition at line 25 of file autocomplete.py.

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

Referenced by get(), and post().

+ Here is the caller graph for this function:

◆ wikipedia()

searx.autocomplete.wikipedia ( query,
sxng_locale )
Autocomplete from Wikipedia. Supports Wikipedia's languages (aka netloc).

Definition at line 211 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

◆ yandex()

searx.autocomplete.yandex ( query,
_lang )

Definition at line 238 of file autocomplete.py.

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

References get().

+ Here is the call graph for this function:

Variable Documentation

◆ backends

dict searx.autocomplete.backends
Initial value:
1= {
2 'baidu': baidu,
3 'brave': brave,
4 'dbpedia': dbpedia,
5 'duckduckgo': duckduckgo,
6 'google': google_complete,
7 'mwmbl': mwmbl,
8 'qwant': qwant,
9 'seznam': seznam,
10 'stract': stract,
11 'swisscows': swisscows,
12 'wikipedia': wikipedia,
13 'yandex': yandex,
14}

Definition at line 248 of file autocomplete.py.