@@ -22,7 +22,7 @@ from datetime import datetime
2222from operator import itemgetter
2323from urlparse import urlparse, unquote
2424from searx.engines import (
25− categories, engines, engine_shortcuts
25+ categories, engines
2626)
2727from searx.languages import language_codes
2828from searx.utils import gen_useragent
@@ -39,7 +39,13 @@ def default_request_params():
3939
4040
4141# create a callback wrapper for the search engine results
42−def make_callback(engine_name, results, suggestions, answers, infoboxes, callback, params):
42+def make_callback(engine_name,
43+ results,
44+ suggestions,
45+ answers,
46+ infoboxes,
47+ callback,
48+ params):
4349
4450 # creating a callback wrapper for the search engine results
4551 def process_callback(response, **kwargs):
@@ -95,7 +101,7 @@ def make_callback(engine_name, results, suggestions, answers, infoboxes, callbac
95101def content_result_len(content):
96102 if isinstance(content, basestring):
97103 content = re.sub('[,;:!?\./\\\\ ()-_]', '', content)
98− return len(content)
104+ return len(content)
99105 else:
100106 return 0
101107
@@ -126,7 +132,8 @@ def score_results(results):
126132
127133 # strip multiple spaces and cariage returns from content
128134 if 'content' in res:
129− res['content'] = re.sub(' +', ' ', res['content'].strip().replace('\n', ''))
135+ res['content'] = re.sub(' +', ' ',
136+ res['content'].strip().replace('\n', ''))
130137
131138 # get weight of this engine if possible
132139 if hasattr(engines[res['engine']], 'weight'):
@@ -139,8 +146,12 @@ def score_results(results):
139146 duplicated = False
140147 for new_res in results:
141148 # remove / from the end of the url if required
142− p1 = res['parsed_url'].path[:-1] if res['parsed_url'].path.endswith('/') else res['parsed_url'].path # noqa
143− p2 = new_res['parsed_url'].path[:-1] if new_res['parsed_url'].path.endswith('/') else new_res['parsed_url'].path # noqa
149+ p1 = res['parsed_url'].path[:-1]\
150+ if res['parsed_url'].path.endswith('/')\
151+ else res['parsed_url'].path
152+ p2 = new_res['parsed_url'].path[:-1]\
153+ if new_res['parsed_url'].path.endswith('/')\
154+ else new_res['parsed_url'].path
144155
145156 # check if that result is a duplicate
146157 if res['host'] == new_res['host'] and\
@@ -153,7 +164,8 @@ def score_results(results):
153164 # merge duplicates together
154165 if duplicated:
155166 # using content with more text
156− if content_result_len(res.get('content', '')) > content_result_len(duplicated.get('content', '')):
167+ if content_result_len(res.get('content', '')) >\
168+ content_result_len(duplicated.get('content', '')):
157169 duplicated['content'] = res['content']
158170
159171 # increase result-score
@@ -182,17 +194,25 @@ def score_results(results):
182194
183195 for i, res in enumerate(results):
184196 # FIXME : handle more than one category per engine
185− category = engines[res['engine']].categories[0] + ':' + '' if 'template' not in res else res['template']
186−
187− current = None if category not in categoryPositions else categoryPositions[category]
188−
189− # group with previous results using the same category if the group can accept more result and is not too far from the current position
190− if current != None and (current['count'] > 0) and (len(gresults) - current['index'] < 20):
191− # group with the previous results using the same category with this one
197+ category = engines[res['engine']].categories[0] + ':' + ''\
198+ if 'template' not in res\
199+ else res['template']
200+
201+ current = None if category not in categoryPositions\
202+ else categoryPositions[category]
203+
204+ # group with previous results using the same category
205+ # if the group can accept more result and is not too far
206+ # from the current position
207+ if current is not None and (current['count'] > 0)\
208+ and (len(gresults) - current['index'] < 20):
209+ # group with the previous results using
210+ # the same category with this one
192211 index = current['index']
193212 gresults.insert(index, res)
194213
195− # update every index after the current one (including the current one)
214+ # update every index after the current one
215+ # (including the current one)
196216 for k in categoryPositions:
197217 v = categoryPositions[k]['index']
198218 if v >= index:
@@ -206,7 +226,7 @@ def score_results(results):
206226 gresults.append(res)
207227
208228 # update categoryIndex
209− categoryPositions[category] = { 'index' : len(gresults), 'count' : 8 }
229+ categoryPositions[category] = {'index': len(gresults), 'count': 8}
210230
211231 # return gresults
212232 return gresults
@@ -215,21 +235,21 @@ def score_results(results):
215235def merge_two_infoboxes(infobox1, infobox2):
216236 if 'urls' in infobox2:
217237 urls1 = infobox1.get('urls', None)
218− if urls1 == None:
238+ if urls1 is None:
219239 urls1 = []
220240 infobox1.set('urls', urls1)
221241
222242 urlSet = set()
223243 for url in infobox1.get('urls', []):
224244 urlSet.add(url.get('url', None))
225−
245+
226246 for url in infobox2.get('urls', []):
227247 if url.get('url', None) not in urlSet:
228248 urls1.append(url)
229249
230250 if 'attributes' in infobox2:
231251 attributes1 = infobox1.get('attributes', None)
232− if attributes1 == None:
252+ if attributes1 is None:
233253 attributes1 = []
234254 infobox1.set('attributes', attributes1)
235255
@@ -237,14 +257,14 @@ def merge_two_infoboxes(infobox1, infobox2):
237257 for attribute in infobox1.get('attributes', []):
238258 if attribute.get('label', None) not in attributeSet:
239259 attributeSet.add(attribute.get('label', None))
240−
260+
241261 for attribute in infobox2.get('attributes', []):
242262 attributes1.append(attribute)
243263
244264 if 'content' in infobox2:
245265 content1 = infobox1.get('content', None)
246266 content2 = infobox2.get('content', '')
247− if content1 != None:
267+ if content1 is not None:
248268 if content_result_len(content2) > content_result_len(content1):
249269 infobox1['content'] = content2
250270 else:
@@ -257,12 +277,12 @@ def merge_infoboxes(infoboxes):
257277 for infobox in infoboxes:
258278 add_infobox = True
259279 infobox_id = infobox.get('id', None)
260− if infobox_id != None:
280+ if infobox_id is not None:
261281 existingIndex = infoboxes_id.get(infobox_id, None)
262− if existingIndex != None:
282+ if existingIndex is not None:
263283 merge_two_infoboxes(results[existingIndex], infobox)
264− add_infobox=False
265−
284+ add_infobox = False
285+
266286 if add_infobox:
267287 results.append(infobox)
268288 infoboxes_id[infobox_id] = len(results)-1
@@ -318,7 +338,8 @@ class Search(object):
318338
319339 self.pageno = int(pageno_param)
320340
321− # parse query, if tags are set, which change the serch engine or search-language
341+ # parse query, if tags are set, which change
342+ # the serch engine or search-language
322343 query_obj = Query(self.request_data['q'], self.blocked_engines)
323344 query_obj.parse_query()
324345
@@ -334,25 +355,29 @@ class Search(object):
334355
335356 self.categories = []
336357
337− # if engines are calculated from query, set categories by using that informations
358+ # if engines are calculated from query,
359+ # set categories by using that informations
338360 if self.engines:
339361 self.categories = list(set(engine['category']
340362 for engine in self.engines))
341363
342− # otherwise, using defined categories to calculate which engines should be used
364+ # otherwise, using defined categories to
365+ # calculate which engines should be used
343366 else:
344367 # set used categories
345368 for pd_name, pd in self.request_data.items():
346369 if pd_name.startswith('category_'):
347370 category = pd_name[9:]
348371 # if category is not found in list, skip
349− if not category in categories:
372+ if category not in categories:
350373 continue
351374
352375 # add category to list
353376 self.categories.append(category)
354377
355− # if no category is specified for this search, using user-defined default-configuration which (is stored in cookie)
378+ # if no category is specified for this search,
379+ # using user-defined default-configuration which
380+ # (is stored in cookie)
356381 if not self.categories:
357382 cookie_categories = request.cookies.get('categories', '')
358383 cookie_categories = cookie_categories.split(',')
@@ -360,16 +385,18 @@ class Search(object):
360385 if ccateg in categories:
361386 self.categories.append(ccateg)
362387
363− # if still no category is specified, using general as default-category
388+ # if still no category is specified, using general
389+ # as default-category
364390 if not self.categories:
365391 self.categories = ['general']
366392
367− # using all engines for that search, which are declared under the specific categories
393+ # using all engines for that search, which are
394+ # declared under the specific categories
368395 for categ in self.categories:
369396 self.engines.extend({'category': categ,
370397 'name': x.name}
371398 for x in categories[categ]
372− if not x.name in self.blocked_engines)
399+ if x.name not in self.blocked_engines)
373400
374401 # do search-request
375402 def search(self, request):
@@ -386,7 +413,7 @@ class Search(object):
386413 number_of_searches += 1
387414
388415 # set default useragent
389− #user_agent = request.headers.get('User-Agent', '')
416+ # user_agent = request.headers.get('User-Agent', '')
390417 user_agent = gen_useragent()
391418
392419 # start search-reqest for all selected engines
@@ -400,7 +427,8 @@ class Search(object):
400427 if self.pageno > 1 and not engine.paging:
401428 continue
402429
403− # if search-language is set and engine does not provide language-support, skip
430+ # if search-language is set and engine does not
431+ # provide language-support, skip
404432 if self.lang != 'all' and not engine.language_support:
405433 continue
406434
@@ -412,7 +440,8 @@ class Search(object):
412440 request_params['pageno'] = self.pageno
413441 request_params['language'] = self.lang
414442
415− # update request parameters dependent on search-engine (contained in engines folder)
443+ # update request parameters dependent on
444+ # search-engine (contained in engines folder)
416445 request_params = engine.request(self.query.encode('utf-8'),
417446 request_params)
418447
@@ -431,7 +460,8 @@ class Search(object):
431460 request_params
432461 )
433462
434− # create dictionary which contain all informations about the request
463+ # create dictionary which contain all
464+ # informations about the request
435465 request_args = dict(
436466 headers=request_params['headers'],
437467 hooks=dict(response=callback),