From 8520be3cd6bb2ae4aa4ee483f632ef7fca318e1a Mon Sep 17 00:00:00 2001 From: asciimoo Date: Tue, 22 Oct 2013 18:57:20 +0200 Subject: [PATCH] [enh] categories to request callback --- searx/engines/__init__.py | 17 +++++++---- searx/webapp.py | 63 ++++++++++++++++++--------------------- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/searx/engines/__init__.py b/searx/engines/__init__.py index aa329269e..8339a13dc 100644 --- a/searx/engines/__init__.py +++ b/searx/engines/__init__.py @@ -61,18 +61,25 @@ def make_callback(engine_name, results, callback): results[engine_name] = cb_res return process_callback -def search(query, request, selected_engines): - global engines +def search(query, request, selected_categories): + global engines, categories requests = [] results = {} + selected_engines = [] user_agent = request.headers.get('User-Agent', '') - for ename, engine in engines.items(): - if ename not in selected_engines: + if not len(selected_categories): + selected_categories = ['general'] + for categ in selected_categories: + selected_engines.extend({'category': categ, 'name': x.name} for x in categories[categ]) + for selected_engine in selected_engines: + if selected_engine['name'] not in engines: continue + engine = engines[selected_engine['name']] request_params = default_request_params() request_params['headers']['User-Agent'] = user_agent + request_params['category'] = selected_engine['category'] request_params = engine.request(query, request_params) - callback = make_callback(ename, results, engine.response) + callback = make_callback(selected_engine['name'], results, engine.response) if request_params['method'] == 'GET': req = grequests.get(request_params['url'] ,headers=request_params['headers'] diff --git a/searx/webapp.py b/searx/webapp.py index 3c3984571..b64d6a466 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -63,40 +63,35 @@ def index(): request_data = request.form else: request_data = request.args - if request_data.get('q'): - selected_engines = [] - selected_categories = [] - for pd_name,pd in request_data.items(): - if pd_name.startswith('category_'): - category = pd_name[9:] - if not category in categories: - continue - selected_categories.append(category) - selected_engines.extend(x.name for x in categories[category]) - if not len(selected_engines): - cookie_categories = request.cookies.get('categories', '').split(',') - for ccateg in cookie_categories: - if ccateg in categories: - selected_categories.append(ccateg) - selected_engines.extend(x.name for x in categories[ccateg]) - if not len(selected_engines): - selected_categories.append('general') - selected_engines.extend(x.name for x in categories['general']) - query = request_data['q'].encode('utf-8') - results = search(query, request, selected_engines) - if request_data.get('format') == 'json': - # TODO HTTP headers - return json.dumps({'query': query, 'results': results}) - template = render('results.html' - ,results=results - ,q=query.decode('utf-8') - ,selected_categories=selected_categories - ,number_of_results=len(results) - ) - resp = make_response(template) - resp.set_cookie('categories', ','.join(selected_categories)) - return resp - return render('index.html') + if not request_data.get('q'): + return render('index.html') + selected_engines = [] + selected_categories = [] + for pd_name,pd in request_data.items(): + if pd_name.startswith('category_'): + category = pd_name[9:] + if not category in categories: + continue + selected_categories.append(category) + if not len(selected_categories): + cookie_categories = request.cookies.get('categories', '').split(',') + for ccateg in cookie_categories: + if ccateg in categories: + selected_categories.append(ccateg) + query = request_data['q'].encode('utf-8') + results = search(query, request, selected_categories) + if request_data.get('format') == 'json': + # TODO HTTP headers + return json.dumps({'query': query, 'results': results}) + template = render('results.html' + ,results=results + ,q=query.decode('utf-8') + ,selected_categories=selected_categories + ,number_of_results=len(results) + ) + resp = make_response(template) + resp.set_cookie('categories', ','.join(selected_categories)) + return resp @app.route('/favicon.ico', methods=['GET']) def fav():