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

Functions

 update_kwargs (**kwargs)
 
 get (*args, **kwargs)
 
 post (*args, **kwargs)
 
 brave (query, _lang)
 
 dbpedia (query, _lang)
 
 duckduckgo (query, sxng_locale)
 
 google_complete (query, sxng_locale)
 
 mwmbl (query, _lang)
 
 seznam (query, _lang)
 
 stract (query, _lang)
 
 startpage (query, sxng_locale)
 
 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

◆ brave()

searx.autocomplete.brave ( query,
_lang )

Definition at line 39 of file autocomplete.py.

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

◆ dbpedia()

searx.autocomplete.dbpedia ( query,
_lang )

Definition at line 58 of file autocomplete.py.

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

◆ duckduckgo()

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

Definition at line 73 of file autocomplete.py.

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

◆ get()

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

Definition at line 29 of file autocomplete.py.

29def get(*args, **kwargs):
30 update_kwargs(**kwargs)
31 return http_get(*args, **kwargs)
32
33

References searx.autocomplete.update_kwargs().

+ Here is the call 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 93 of file autocomplete.py.

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

◆ mwmbl()

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

Definition at line 122 of file autocomplete.py.

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

◆ post()

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

Definition at line 34 of file autocomplete.py.

34def post(*args, **kwargs):
35 update_kwargs(**kwargs)
36 return http_post(*args, **kwargs)
37
38

References searx.autocomplete.update_kwargs().

+ Here is the call graph for this function:

◆ qwant()

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

Definition at line 186 of file autocomplete.py.

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

◆ search_autocomplete()

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

Definition at line 256 of file autocomplete.py.

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

◆ seznam()

searx.autocomplete.seznam ( query,
_lang )

Definition at line 134 of file autocomplete.py.

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

◆ startpage()

searx.autocomplete.startpage ( query,
sxng_locale )
Autocomplete from Startpage. Supports Startpage's languages

Definition at line 169 of file autocomplete.py.

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

◆ stract()

searx.autocomplete.stract ( query,
_lang )

Definition at line 157 of file autocomplete.py.

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

◆ swisscows()

searx.autocomplete.swisscows ( query,
_lang )

Definition at line 178 of file autocomplete.py.

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

◆ update_kwargs()

searx.autocomplete.update_kwargs ( ** kwargs)

Definition at line 23 of file autocomplete.py.

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

Referenced by searx.autocomplete.get(), and searx.autocomplete.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 203 of file autocomplete.py.

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

◆ yandex()

searx.autocomplete.yandex ( query,
_lang )

Definition at line 230 of file autocomplete.py.

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

Variable Documentation

◆ backends

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

Definition at line 240 of file autocomplete.py.