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

Functions

List[EngineRefdeduplicate_engineref_list (List[EngineRef] engineref_list)
 
Tuple[List[EngineRef], List[EngineRef], List[EngineRef]] validate_engineref_list (List[EngineRef] engineref_list, Preferences preferences)
 
int parse_pageno (Dict[str, str] form)
 
str parse_lang (Preferences preferences, Dict[str, str] form, RawTextQuery raw_text_query)
 
int parse_safesearch (Preferences preferences, Dict[str, str] form)
 
Optional[str] parse_time_range (Dict[str, str] form)
 
Optional[float] parse_timeout (Dict[str, str] form, RawTextQuery raw_text_query)
 
None parse_category_form (List[str] query_categories, str name, str value)
 
List[str] get_selected_categories (Preferences preferences, Optional[Dict[str, str]] form)
 
List[EngineRefget_engineref_from_category_list (List[str] category_list, List[str] disabled_engines)
 
List[EngineRefparse_generic (Preferences preferences, Dict[str, str] form, List[str] disabled_engines)
 
 parse_engine_data (form)
 
Tuple[SearchQuery, RawTextQuery, List[EngineRef], List[EngineRef], str] get_search_query_from_webapp (Preferences preferences, Dict[str, str] form)
 

Function Documentation

◆ deduplicate_engineref_list()

List[EngineRef] searx.webadapter.deduplicate_engineref_list ( List[EngineRef] engineref_list)

Definition at line 17 of file webadapter.py.

17def deduplicate_engineref_list(engineref_list: List[EngineRef]) -> List[EngineRef]:
18 engineref_dict = {q.category + '|' + q.name: q for q in engineref_list}
19 return list(engineref_dict.values())
20
21

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function:

◆ get_engineref_from_category_list()

List[EngineRef] searx.webadapter.get_engineref_from_category_list ( List[str] category_list,
List[str] disabled_engines )

Definition at line 159 of file webadapter.py.

162) -> List[EngineRef]:
163 result = []
164 for categ in category_list:
165 result.extend(
166 EngineRef(engine.name, categ)
167 for engine in categories[categ]
168 if (engine.name, categ) not in disabled_engines
169 )
170 return result
171
172

Referenced by searx.webadapter.parse_generic().

+ Here is the caller graph for this function:

◆ get_search_query_from_webapp()

Tuple[SearchQuery, RawTextQuery, List[EngineRef], List[EngineRef], str] searx.webadapter.get_search_query_from_webapp ( Preferences preferences,
Dict[str, str] form )
Assemble data from preferences and request.form (from the HTML form) needed
in a search query.

The returned tuple consits of:

1. instance of :py:obj:`searx.search.SearchQuery`
2. instance of :py:obj:`searx.query.RawTextQuery`
3. list of :py:obj:`searx.search.EngineRef` instances
4. string with the *selected locale* of the query

About language/locale: if the client selects the alias ``auto`` the
``SearchQuery`` object is build up by the :py:obj:`detected language
<searx.utils.detect_language>`.  If language recognition does not have a
match the language preferred by the :py:obj:`Preferences.client` is used.
If client does not have a preference, the default ``all`` is used.

The *selected locale* in the tuple always represents the selected
language/locale and might differ from the language recognition.

Definition at line 222 of file webadapter.py.

224) -> Tuple[SearchQuery, RawTextQuery, List[EngineRef], List[EngineRef], str]:
225 """Assemble data from preferences and request.form (from the HTML form) needed
226 in a search query.
227
228 The returned tuple consits of:
229
230 1. instance of :py:obj:`searx.search.SearchQuery`
231 2. instance of :py:obj:`searx.query.RawTextQuery`
232 3. list of :py:obj:`searx.search.EngineRef` instances
233 4. string with the *selected locale* of the query
234
235 About language/locale: if the client selects the alias ``auto`` the
236 ``SearchQuery`` object is build up by the :py:obj:`detected language
237 <searx.utils.detect_language>`. If language recognition does not have a
238 match the language preferred by the :py:obj:`Preferences.client` is used.
239 If client does not have a preference, the default ``all`` is used.
240
241 The *selected locale* in the tuple always represents the selected
242 language/locale and might differ from the language recognition.
243
244 """
245 # no text for the query ?
246 if not form.get('q'):
247 raise SearxParameterException('q', '')
248
249 # set blocked engines
250 disabled_engines = preferences.engines.get_disabled()
251
252 # parse query, if tags are set, which change
253 # the search engine or search-language
254 raw_text_query = RawTextQuery(form['q'], disabled_engines)
255
256 # set query
257 query = raw_text_query.getQuery()
258 query_pageno = parse_pageno(form)
259 query_safesearch = parse_safesearch(preferences, form)
260 query_time_range = parse_time_range(form)
261 query_timeout = parse_timeout(form, raw_text_query)
262 external_bang = raw_text_query.external_bang
263 redirect_to_first_result = raw_text_query.redirect_to_first_result
264 engine_data = parse_engine_data(form)
265
266 query_lang = parse_lang(preferences, form, raw_text_query)
267 selected_locale = query_lang
268
269 if query_lang == 'auto':
270 query_lang = detect_language(query, threshold=0.8, only_search_languages=True)
271 query_lang = query_lang or preferences.client.locale_tag or 'all'
272
273 if not is_locked('categories') and raw_text_query.specific:
274 # if engines are calculated from query,
275 # set categories by using that information
276 query_engineref_list = raw_text_query.enginerefs
277 else:
278 # otherwise, using defined categories to
279 # calculate which engines should be used
280 query_engineref_list = parse_generic(preferences, form, disabled_engines)
281
282 query_engineref_list = deduplicate_engineref_list(query_engineref_list)
283 query_engineref_list, query_engineref_list_unknown, query_engineref_list_notoken = validate_engineref_list(
284 query_engineref_list, preferences
285 )
286
287 return (
288 SearchQuery(
289 query,
290 query_engineref_list,
291 query_lang,
292 query_safesearch,
293 query_pageno,
294 query_time_range,
295 query_timeout,
296 external_bang=external_bang,
297 engine_data=engine_data,
298 redirect_to_first_result=redirect_to_first_result,
299 ),
300 raw_text_query,
301 query_engineref_list_unknown,
302 query_engineref_list_notoken,
303 selected_locale,
304 )

References searx.webadapter.deduplicate_engineref_list(), searx.webadapter.parse_engine_data(), searx.webadapter.parse_generic(), searx.webadapter.parse_pageno(), searx.webadapter.parse_safesearch(), searx.webadapter.parse_time_range(), searx.webadapter.parse_timeout(), and searx.webadapter.validate_engineref_list().

+ Here is the call graph for this function:

◆ get_selected_categories()

List[str] searx.webadapter.get_selected_categories ( Preferences preferences,
Optional[Dict[str, str]] form )

Definition at line 136 of file webadapter.py.

136def get_selected_categories(preferences: Preferences, form: Optional[Dict[str, str]]) -> List[str]:
137 selected_categories = []
138
139 if not is_locked('categories') and form is not None:
140 for name, value in form.items():
141 parse_category_form(selected_categories, name, value)
142
143 # if no category is specified for this search,
144 # using user-defined default-configuration which
145 # (is stored in cookie)
146 if not selected_categories:
147 cookie_categories = preferences.get_value('categories')
148 for ccateg in cookie_categories:
149 selected_categories.append(ccateg)
150
151 # if still no category is specified, using general
152 # as default-category
153 if not selected_categories:
154 selected_categories = ['general']
155
156 return selected_categories
157
158

References searx.webadapter.parse_category_form().

+ Here is the call graph for this function:

◆ parse_category_form()

None searx.webadapter.parse_category_form ( List[str] query_categories,
str name,
str value )

Definition at line 118 of file webadapter.py.

118def parse_category_form(query_categories: List[str], name: str, value: str) -> None:
119 if name == 'categories':
120 query_categories.extend(categ for categ in map(str.strip, value.split(',')) if categ in categories)
121 elif name.startswith('category_'):
122 category = name[9:]
123
124 # if category is not found in list, skip
125 if category not in categories:
126 return
127
128 if value != 'off':
129 # add category to list
130 query_categories.append(category)
131 elif category in query_categories:
132 # remove category from list if property is set to 'off'
133 query_categories.remove(category)
134
135

Referenced by searx.webadapter.get_selected_categories(), and searx.webadapter.parse_generic().

+ Here is the caller graph for this function:

◆ parse_engine_data()

searx.webadapter.parse_engine_data ( form)

Definition at line 213 of file webadapter.py.

213def parse_engine_data(form):
214 engine_data = defaultdict(dict)
215 for k, v in form.items():
216 if k.startswith("engine_data"):
217 _, engine, key = k.split('-')
218 engine_data[engine][key] = v
219 return engine_data
220
221

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function:

◆ parse_generic()

List[EngineRef] searx.webadapter.parse_generic ( Preferences preferences,
Dict[str, str] form,
List[str] disabled_engines )

Definition at line 173 of file webadapter.py.

173def parse_generic(preferences: Preferences, form: Dict[str, str], disabled_engines: List[str]) -> List[EngineRef]:
174 query_engineref_list = []
175 query_categories = []
176
177 # set categories/engines
178 explicit_engine_list = False
179 if not is_locked('categories'):
180 # parse the form only if the categories are not locked
181 for pd_name, pd in form.items(): # pylint: disable=invalid-name
182 if pd_name == 'engines':
183 pd_engines = [
184 EngineRef(engine_name, engines[engine_name].categories[0])
185 for engine_name in map(str.strip, pd.split(','))
186 if engine_name in engines
187 ]
188 if pd_engines:
189 query_engineref_list.extend(pd_engines)
190 explicit_engine_list = True
191 else:
192 parse_category_form(query_categories, pd_name, pd)
193
194 if explicit_engine_list:
195 # explicit list of engines with the "engines" parameter in the form
196 if query_categories:
197 # add engines from referenced by the "categories" parameter and the "category_*"" parameters
198 query_engineref_list.extend(get_engineref_from_category_list(query_categories, disabled_engines))
199 else:
200 # no "engines" parameters in the form
201 if not query_categories:
202 # and neither "categories" parameter nor "category_*"" parameters in the form
203 # -> get the categories from the preferences (the cookies or the settings)
204 query_categories = get_selected_categories(preferences, None)
205
206 # using all engines for that search, which are
207 # declared under the specific categories
208 query_engineref_list.extend(get_engineref_from_category_list(query_categories, disabled_engines))
209
210 return query_engineref_list
211
212

References searx.webadapter.get_engineref_from_category_list(), and searx.webadapter.parse_category_form().

Referenced by searx.webadapter.get_search_query_from_webapp().

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

◆ parse_lang()

str searx.webadapter.parse_lang ( Preferences preferences,
Dict[str, str] form,
RawTextQuery raw_text_query )

Definition at line 56 of file webadapter.py.

56def parse_lang(preferences: Preferences, form: Dict[str, str], raw_text_query: RawTextQuery) -> str:
57 if is_locked('language'):
58 return preferences.get_value('language')
59 # get language
60 # set specific language if set on request, query or preferences
61 # search with multiple languages is not supported (by most engines)
62 if len(raw_text_query.languages):
63 query_lang = raw_text_query.languages[-1]
64 elif 'language' in form:
65 query_lang = form.get('language')
66 else:
67 query_lang = preferences.get_value('language')
68
69 # check language
70 if not VALID_LANGUAGE_CODE.match(query_lang) and query_lang != 'auto':
71 raise SearxParameterException('language', query_lang)
72
73 return query_lang
74
75

◆ parse_pageno()

int searx.webadapter.parse_pageno ( Dict[str, str] form)

Definition at line 49 of file webadapter.py.

49def parse_pageno(form: Dict[str, str]) -> int:
50 pageno_param = form.get('pageno', '1')
51 if not pageno_param.isdigit() or int(pageno_param) < 1:
52 raise SearxParameterException('pageno', pageno_param)
53 return int(pageno_param)
54
55

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function:

◆ parse_safesearch()

int searx.webadapter.parse_safesearch ( Preferences preferences,
Dict[str, str] form )

Definition at line 76 of file webadapter.py.

76def parse_safesearch(preferences: Preferences, form: Dict[str, str]) -> int:
77 if is_locked('safesearch'):
78 return preferences.get_value('safesearch')
79
80 if 'safesearch' in form:
81 query_safesearch = form.get('safesearch')
82 # first check safesearch
83 if not query_safesearch.isdigit():
84 raise SearxParameterException('safesearch', query_safesearch)
85 query_safesearch = int(query_safesearch)
86 else:
87 query_safesearch = preferences.get_value('safesearch')
88
89 # safesearch : second check
90 if query_safesearch < 0 or query_safesearch > 2:
91 raise SearxParameterException('safesearch', query_safesearch)
92
93 return query_safesearch
94
95

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function:

◆ parse_time_range()

Optional[str] searx.webadapter.parse_time_range ( Dict[str, str] form)

Definition at line 96 of file webadapter.py.

96def parse_time_range(form: Dict[str, str]) -> Optional[str]:
97 query_time_range = form.get('time_range')
98 # check time_range
99 query_time_range = None if query_time_range in ('', 'None') else query_time_range
100 if query_time_range not in (None, 'day', 'week', 'month', 'year'):
101 raise SearxParameterException('time_range', query_time_range)
102 return query_time_range
103
104

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function:

◆ parse_timeout()

Optional[float] searx.webadapter.parse_timeout ( Dict[str, str] form,
RawTextQuery raw_text_query )

Definition at line 105 of file webadapter.py.

105def parse_timeout(form: Dict[str, str], raw_text_query: RawTextQuery) -> Optional[float]:
106 timeout_limit = raw_text_query.timeout_limit
107 if timeout_limit is None:
108 timeout_limit = form.get('timeout_limit')
109
110 if timeout_limit is None or timeout_limit in ['None', '']:
111 return None
112 try:
113 return float(timeout_limit)
114 except ValueError as e:
115 raise SearxParameterException('timeout_limit', timeout_limit) from e
116
117

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function:

◆ validate_engineref_list()

Tuple[List[EngineRef], List[EngineRef], List[EngineRef]] searx.webadapter.validate_engineref_list ( List[EngineRef] engineref_list,
Preferences preferences )
Validate query_engines according to the preferences

Returns:
    List[EngineRef]: list of existing engines with a validated token
    List[EngineRef]: list of unknown engine
    List[EngineRef]: list of engine with invalid token according to the preferences

Definition at line 22 of file webadapter.py.

24) -> Tuple[List[EngineRef], List[EngineRef], List[EngineRef]]:
25 """Validate query_engines according to the preferences
26
27 Returns:
28 List[EngineRef]: list of existing engines with a validated token
29 List[EngineRef]: list of unknown engine
30 List[EngineRef]: list of engine with invalid token according to the preferences
31 """
32 valid = []
33 unknown = []
34 no_token = []
35 for engineref in engineref_list:
36 if engineref.name not in engines:
37 unknown.append(engineref)
38 continue
39
40 engine = engines[engineref.name]
41 if not preferences.validate_token(engine):
42 no_token.append(engineref)
43 continue
44
45 valid.append(engineref)
46 return valid, unknown, no_token
47
48

Referenced by searx.webadapter.get_search_query_from_webapp().

+ Here is the caller graph for this function: