From 64cc960f9fdf72d04bce50d19a292ed3df9b8aa7 Mon Sep 17 00:00:00 2001 From: asciimoo Date: Mon, 4 Nov 2013 00:21:27 +0100 Subject: [PATCH] [enh] specify search engine in queries --- searx/webapp.py | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index 6dad9efc..4fb4e458 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -23,7 +23,7 @@ if __name__ == "__main__": path.append(realpath(dirname(realpath(__file__))+'/../')) from flask import Flask, request, render_template, url_for, Response, make_response -from searx.engines import search, categories, get_engines_stats +from searx.engines import search, categories, engines, get_engines_stats from searx import settings import json @@ -56,6 +56,14 @@ def render(template_name, **kwargs): kwargs['selected_categories'] = ['general'] return render_template(template_name, **kwargs) +def parse_query(query): + query_engines = [] + query_parts = query.split() + if query_parts[0].startswith('-') and query_parts[0][1:] in engines: + query_engines.append({'category': 'TODO', 'name': query_parts[0][1:]}) + query = query.replace(query_parts[0], '', 1).strip() + return query, query_engines + @app.route('/', methods=['GET', 'POST']) def index(): global categories @@ -66,26 +74,27 @@ def index(): if not request_data.get('q'): return render('index.html') - query = request_data['q'].encode('utf-8') - 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) - if not len(selected_categories): - selected_categories = ['general'] - selected_engines = [] - for categ in selected_categories: - selected_engines.extend({'category': categ, 'name': x.name} for x in categories[categ]) + query, selected_engines = parse_query(request_data['q'].encode('utf-8')) + + if not len(selected_engines): + 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) + 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]) results = search(query, request, selected_engines) for result in results: @@ -97,7 +106,7 @@ def index(): return Response(json.dumps({'query': query, 'results': results}), mimetype='application/json') template = render('results.html' ,results=results - ,q=query.decode('utf-8') + ,q=request_data['q'] ,selected_categories=selected_categories ,number_of_results=len(results) )