|
@@ -25,9 +25,10 @@ from searx.engines import (
|
25
|
25
|
categories, engines
|
26
|
26
|
)
|
27
|
27
|
from searx.utils import gen_useragent
|
28
|
|
-from searx.query import Query
|
|
28
|
+from searx.query import RawTextQuery, SearchQuery
|
29
|
29
|
from searx.results import ResultContainer
|
30
|
30
|
from searx import logger
|
|
31
|
+from searx.plugins import plugins
|
31
|
32
|
|
32
|
33
|
logger = logger.getChild('search')
|
33
|
34
|
|
|
@@ -127,135 +128,130 @@ def make_callback(engine_name, callback, params, result_container):
|
127
|
128
|
return process_callback
|
128
|
129
|
|
129
|
130
|
|
130
|
|
-class Search(object):
|
131
|
|
-
|
132
|
|
- """Search information container"""
|
133
|
|
-
|
134
|
|
- def __init__(self, request):
|
135
|
|
- # init vars
|
136
|
|
- super(Search, self).__init__()
|
137
|
|
- self.query = None
|
138
|
|
- self.engines = []
|
139
|
|
- self.categories = []
|
140
|
|
- self.paging = False
|
141
|
|
- self.pageno = 1
|
142
|
|
- self.lang = 'all'
|
143
|
|
- self.time_range = None
|
144
|
|
- self.is_advanced = None
|
145
|
|
-
|
146
|
|
- # set blocked engines
|
147
|
|
- self.disabled_engines = request.preferences.engines.get_disabled()
|
148
|
|
-
|
149
|
|
- self.result_container = ResultContainer()
|
150
|
|
- self.request_data = {}
|
151
|
|
-
|
152
|
|
- # set specific language if set
|
153
|
|
- self.lang = request.preferences.get_value('language')
|
154
|
|
-
|
155
|
|
- # set request method
|
156
|
|
- if request.method == 'POST':
|
157
|
|
- self.request_data = request.form
|
|
131
|
+def get_search_query_from_webapp(preferences, request_data):
|
|
132
|
+ query = None
|
|
133
|
+ query_engines = []
|
|
134
|
+ query_categories = []
|
|
135
|
+ query_paging = False
|
|
136
|
+ query_pageno = 1
|
|
137
|
+ query_lang = 'all'
|
|
138
|
+ query_time_range = None
|
|
139
|
+
|
|
140
|
+ # set blocked engines
|
|
141
|
+ disabled_engines = preferences.engines.get_disabled()
|
|
142
|
+
|
|
143
|
+ # set specific language if set
|
|
144
|
+ query_lang = preferences.get_value('language')
|
|
145
|
+
|
|
146
|
+ # safesearch
|
|
147
|
+ query_safesearch = preferences.get_value('safesearch')
|
|
148
|
+
|
|
149
|
+ # TODO better exceptions
|
|
150
|
+ if not request_data.get('q'):
|
|
151
|
+ raise Exception('noquery')
|
|
152
|
+
|
|
153
|
+ # set pagenumber
|
|
154
|
+ pageno_param = request_data.get('pageno', '1')
|
|
155
|
+ if not pageno_param.isdigit() or int(pageno_param) < 1:
|
|
156
|
+ pageno_param = 1
|
|
157
|
+
|
|
158
|
+ query_pageno = int(pageno_param)
|
|
159
|
+
|
|
160
|
+ # parse query, if tags are set, which change
|
|
161
|
+ # the serch engine or search-language
|
|
162
|
+ raw_text_query = RawTextQuery(request_data['q'], disabled_engines)
|
|
163
|
+ raw_text_query.parse_query()
|
|
164
|
+
|
|
165
|
+ # set query
|
|
166
|
+ query = raw_text_query.getSearchQuery()
|
|
167
|
+
|
|
168
|
+ # get last selected language in query, if possible
|
|
169
|
+ # TODO support search with multible languages
|
|
170
|
+ if len(raw_text_query.languages):
|
|
171
|
+ query_lang = raw_text_query.languages[-1]
|
|
172
|
+
|
|
173
|
+ query_time_range = request_data.get('time_range')
|
|
174
|
+
|
|
175
|
+ query_engines = raw_text_query.engines
|
|
176
|
+
|
|
177
|
+ # if engines are calculated from query,
|
|
178
|
+ # set categories by using that informations
|
|
179
|
+ if query_engines and raw_text_query.specific:
|
|
180
|
+ query_categories = list(set(engine['category']
|
|
181
|
+ for engine in query_engines))
|
|
182
|
+
|
|
183
|
+ # otherwise, using defined categories to
|
|
184
|
+ # calculate which engines should be used
|
|
185
|
+ else:
|
|
186
|
+ # set categories/engines
|
|
187
|
+ load_default_categories = True
|
|
188
|
+ for pd_name, pd in request_data.items():
|
|
189
|
+ if pd_name == 'categories':
|
|
190
|
+ query_categories.extend(categ for categ in map(unicode.strip, pd.split(',')) if categ in categories)
|
|
191
|
+ elif pd_name == 'engines':
|
|
192
|
+ pd_engines = [{'category': engines[engine].categories[0],
|
|
193
|
+ 'name': engine}
|
|
194
|
+ for engine in map(unicode.strip, pd.split(',')) if engine in engines]
|
|
195
|
+ if pd_engines:
|
|
196
|
+ query_engines.extend(pd_engines)
|
|
197
|
+ load_default_categories = False
|
|
198
|
+ elif pd_name.startswith('category_'):
|
|
199
|
+ category = pd_name[9:]
|
|
200
|
+
|
|
201
|
+ # if category is not found in list, skip
|
|
202
|
+ if category not in categories:
|
|
203
|
+ continue
|
|
204
|
+
|
|
205
|
+ if pd != 'off':
|
|
206
|
+ # add category to list
|
|
207
|
+ query_categories.append(category)
|
|
208
|
+ elif category in query_categories:
|
|
209
|
+ # remove category from list if property is set to 'off'
|
|
210
|
+ query_categories.remove(category)
|
|
211
|
+
|
|
212
|
+ if not load_default_categories:
|
|
213
|
+ if not query_categories:
|
|
214
|
+ query_categories = list(set(engine['category']
|
|
215
|
+ for engine in engines))
|
158
|
216
|
else:
|
159
|
|
- self.request_data = request.args
|
160
|
|
-
|
161
|
|
- # TODO better exceptions
|
162
|
|
- if not self.request_data.get('q'):
|
163
|
|
- raise Exception('noquery')
|
164
|
|
-
|
165
|
|
- # set pagenumber
|
166
|
|
- pageno_param = self.request_data.get('pageno', '1')
|
167
|
|
- if not pageno_param.isdigit() or int(pageno_param) < 1:
|
168
|
|
- pageno_param = 1
|
169
|
|
-
|
170
|
|
- self.pageno = int(pageno_param)
|
171
|
|
-
|
172
|
|
- # parse query, if tags are set, which change
|
173
|
|
- # the serch engine or search-language
|
174
|
|
- query_obj = Query(self.request_data['q'], self.disabled_engines)
|
175
|
|
- query_obj.parse_query()
|
176
|
|
-
|
177
|
|
- # set query
|
178
|
|
- self.query = query_obj.getSearchQuery()
|
179
|
|
-
|
180
|
|
- # get last selected language in query, if possible
|
181
|
|
- # TODO support search with multible languages
|
182
|
|
- if len(query_obj.languages):
|
183
|
|
- self.lang = query_obj.languages[-1]
|
184
|
|
-
|
185
|
|
- self.time_range = self.request_data.get('time_range')
|
186
|
|
- self.is_advanced = self.request_data.get('advanced_search')
|
187
|
|
-
|
188
|
|
- self.engines = query_obj.engines
|
189
|
|
-
|
190
|
|
- # if engines are calculated from query,
|
191
|
|
- # set categories by using that informations
|
192
|
|
- if self.engines and query_obj.specific:
|
193
|
|
- self.categories = list(set(engine['category']
|
194
|
|
- for engine in self.engines))
|
195
|
|
-
|
196
|
|
- # otherwise, using defined categories to
|
197
|
|
- # calculate which engines should be used
|
198
|
|
- else:
|
199
|
|
- # set categories/engines
|
200
|
|
- load_default_categories = True
|
201
|
|
- for pd_name, pd in self.request_data.items():
|
202
|
|
- if pd_name == 'categories':
|
203
|
|
- self.categories.extend(categ for categ in map(unicode.strip, pd.split(',')) if categ in categories)
|
204
|
|
- elif pd_name == 'engines':
|
205
|
|
- pd_engines = [{'category': engines[engine].categories[0],
|
206
|
|
- 'name': engine}
|
207
|
|
- for engine in map(unicode.strip, pd.split(',')) if engine in engines]
|
208
|
|
- if pd_engines:
|
209
|
|
- self.engines.extend(pd_engines)
|
210
|
|
- load_default_categories = False
|
211
|
|
- elif pd_name.startswith('category_'):
|
212
|
|
- category = pd_name[9:]
|
213
|
|
-
|
214
|
|
- # if category is not found in list, skip
|
215
|
|
- if category not in categories:
|
216
|
|
- continue
|
217
|
|
-
|
218
|
|
- if pd != 'off':
|
219
|
|
- # add category to list
|
220
|
|
- self.categories.append(category)
|
221
|
|
- elif category in self.categories:
|
222
|
|
- # remove category from list if property is set to 'off'
|
223
|
|
- self.categories.remove(category)
|
224
|
|
-
|
225
|
|
- if not load_default_categories:
|
226
|
|
- if not self.categories:
|
227
|
|
- self.categories = list(set(engine['category']
|
228
|
|
- for engine in self.engines))
|
229
|
|
- return
|
230
|
|
-
|
231
|
217
|
# if no category is specified for this search,
|
232
|
218
|
# using user-defined default-configuration which
|
233
|
219
|
# (is stored in cookie)
|
234
|
|
- if not self.categories:
|
235
|
|
- cookie_categories = request.preferences.get_value('categories')
|
|
220
|
+ if not query_categories:
|
|
221
|
+ cookie_categories = preferences.get_value('categories')
|
236
|
222
|
for ccateg in cookie_categories:
|
237
|
223
|
if ccateg in categories:
|
238
|
|
- self.categories.append(ccateg)
|
|
224
|
+ query_categories.append(ccateg)
|
239
|
225
|
|
240
|
226
|
# if still no category is specified, using general
|
241
|
227
|
# as default-category
|
242
|
|
- if not self.categories:
|
243
|
|
- self.categories = ['general']
|
|
228
|
+ if not query_categories:
|
|
229
|
+ query_categories = ['general']
|
244
|
230
|
|
245
|
231
|
# using all engines for that search, which are
|
246
|
232
|
# declared under the specific categories
|
247
|
|
- for categ in self.categories:
|
248
|
|
- self.engines.extend({'category': categ,
|
249
|
|
- 'name': engine.name}
|
250
|
|
- for engine in categories[categ]
|
251
|
|
- if (engine.name, categ) not in self.disabled_engines)
|
|
233
|
+ for categ in query_categories:
|
|
234
|
+ query_engines.extend({'category': categ,
|
|
235
|
+ 'name': engine.name}
|
|
236
|
+ for engine in categories[categ]
|
|
237
|
+ if (engine.name, categ) not in disabled_engines)
|
|
238
|
+
|
|
239
|
+ return SearchQuery(query, query_engines, query_categories,
|
|
240
|
+ query_lang, query_safesearch, query_pageno, query_time_range)
|
252
|
241
|
|
253
|
|
- # remove suspended engines
|
254
|
|
- self.engines = [e for e in self.engines
|
255
|
|
- if engines[e['name']].suspend_end_time <= time()]
|
|
242
|
+
|
|
243
|
+class Search(object):
|
|
244
|
+
|
|
245
|
+ """Search information container"""
|
|
246
|
+
|
|
247
|
+ def __init__(self, search_query):
|
|
248
|
+ # init vars
|
|
249
|
+ super(Search, self).__init__()
|
|
250
|
+ self.search_query = search_query
|
|
251
|
+ self.result_container = ResultContainer()
|
256
|
252
|
|
257
|
253
|
# do search-request
|
258
|
|
- def search(self, request):
|
|
254
|
+ def search(self):
|
259
|
255
|
global number_of_searches
|
260
|
256
|
|
261
|
257
|
# init vars
|
|
@@ -268,23 +264,30 @@ class Search(object):
|
268
|
264
|
# user_agent = request.headers.get('User-Agent', '')
|
269
|
265
|
user_agent = gen_useragent()
|
270
|
266
|
|
|
267
|
+ search_query = self.search_query
|
|
268
|
+
|
271
|
269
|
# start search-reqest for all selected engines
|
272
|
|
- for selected_engine in self.engines:
|
|
270
|
+ for selected_engine in search_query.engines:
|
273
|
271
|
if selected_engine['name'] not in engines:
|
274
|
272
|
continue
|
275
|
273
|
|
276
|
274
|
engine = engines[selected_engine['name']]
|
277
|
275
|
|
|
276
|
+ # skip suspended engines
|
|
277
|
+ if engine.suspend_end_time and engine.suspend_end_time <= time():
|
|
278
|
+ continue
|
|
279
|
+
|
278
|
280
|
# if paging is not supported, skip
|
279
|
|
- if self.pageno > 1 and not engine.paging:
|
|
281
|
+ if search_query.pageno > 1 and not engine.paging:
|
280
|
282
|
continue
|
281
|
283
|
|
282
|
284
|
# if search-language is set and engine does not
|
283
|
285
|
# provide language-support, skip
|
284
|
|
- if self.lang != 'all' and not engine.language_support:
|
|
286
|
+ if search_query.lang != 'all' and not engine.language_support:
|
285
|
287
|
continue
|
286
|
288
|
|
287
|
|
- if self.time_range and not engine.time_range_support:
|
|
289
|
+ # if time_range is not supported, skip
|
|
290
|
+ if search_query.time_range and not engine.time_range_support:
|
288
|
291
|
continue
|
289
|
292
|
|
290
|
293
|
# set default request parameters
|
|
@@ -292,21 +295,20 @@ class Search(object):
|
292
|
295
|
request_params['headers']['User-Agent'] = user_agent
|
293
|
296
|
request_params['category'] = selected_engine['category']
|
294
|
297
|
request_params['started'] = time()
|
295
|
|
- request_params['pageno'] = self.pageno
|
|
298
|
+ request_params['pageno'] = search_query.pageno
|
296
|
299
|
|
297
|
300
|
if hasattr(engine, 'language') and engine.language:
|
298
|
301
|
request_params['language'] = engine.language
|
299
|
302
|
else:
|
300
|
|
- request_params['language'] = self.lang
|
|
303
|
+ request_params['language'] = search_query.lang
|
301
|
304
|
|
302
|
305
|
# 0 = None, 1 = Moderate, 2 = Strict
|
303
|
|
- request_params['safesearch'] = request.preferences.get_value('safesearch')
|
304
|
|
- request_params['time_range'] = self.time_range
|
305
|
|
- request_params['advanced_search'] = self.is_advanced
|
|
306
|
+ request_params['safesearch'] = search_query.safesearch
|
|
307
|
+ request_params['time_range'] = search_query.time_range
|
306
|
308
|
|
307
|
309
|
# update request parameters dependent on
|
308
|
310
|
# search-engine (contained in engines folder)
|
309
|
|
- engine.request(self.query.encode('utf-8'), request_params)
|
|
311
|
+ engine.request(search_query.query.encode('utf-8'), request_params)
|
310
|
312
|
|
311
|
313
|
if request_params['url'] is None:
|
312
|
314
|
# TODO add support of offline engines
|
|
@@ -346,10 +348,44 @@ class Search(object):
|
346
|
348
|
selected_engine['name']))
|
347
|
349
|
|
348
|
350
|
if not requests:
|
349
|
|
- return self
|
|
351
|
+ return self.result_container
|
350
|
352
|
# send all search-request
|
351
|
353
|
threaded_requests(requests)
|
352
|
354
|
start_new_thread(gc.collect, tuple())
|
353
|
355
|
|
354
|
356
|
# return results, suggestions, answers and infoboxes
|
355
|
|
- return self
|
|
357
|
+ return self.result_container
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+def search_with_plugins(do_search, search_query, request, request_data, result_container):
|
|
361
|
+ """Search using the do_search function and with plugins filtering.
|
|
362
|
+ Standalone function to have a well define locals().
|
|
363
|
+ result_container contains the results after the function call.
|
|
364
|
+ """
|
|
365
|
+ search = search_query
|
|
366
|
+
|
|
367
|
+ if plugins.call('pre_search', request, locals()):
|
|
368
|
+ do_search()
|
|
369
|
+
|
|
370
|
+ plugins.call('post_search', request, locals())
|
|
371
|
+
|
|
372
|
+ results = result_container.get_ordered_results()
|
|
373
|
+
|
|
374
|
+ for result in results:
|
|
375
|
+ plugins.call('on_result', request, locals())
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+class SearchWithPlugins(Search):
|
|
379
|
+
|
|
380
|
+ def __init__(self, search_query, request):
|
|
381
|
+ super(SearchWithPlugins, self).__init__(search_query)
|
|
382
|
+ self.request = request
|
|
383
|
+ self.request_data = request.request_data
|
|
384
|
+
|
|
385
|
+ def search(self):
|
|
386
|
+
|
|
387
|
+ def do_search():
|
|
388
|
+ super(SearchWithPlugins, self).search()
|
|
389
|
+
|
|
390
|
+ search_with_plugins(do_search, self.search_query, self.request, self.request_data, self.result_container)
|
|
391
|
+ return self.result_container
|