[enh] specify search engine in queries

This commit is contained in:
asciimoo 2013-11-04 00:21:27 +01:00
parent 70278567ec
commit 64cc960f9f
1 changed files with 29 additions and 20 deletions

View File

@ -23,7 +23,7 @@ if __name__ == "__main__":
path.append(realpath(dirname(realpath(__file__))+'/../')) path.append(realpath(dirname(realpath(__file__))+'/../'))
from flask import Flask, request, render_template, url_for, Response, make_response 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 from searx import settings
import json import json
@ -56,6 +56,14 @@ def render(template_name, **kwargs):
kwargs['selected_categories'] = ['general'] kwargs['selected_categories'] = ['general']
return render_template(template_name, **kwargs) 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']) @app.route('/', methods=['GET', 'POST'])
def index(): def index():
global categories global categories
@ -66,26 +74,27 @@ def index():
if not request_data.get('q'): if not request_data.get('q'):
return render('index.html') return render('index.html')
query = request_data['q'].encode('utf-8')
selected_categories = [] 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 = [] query, selected_engines = parse_query(request_data['q'].encode('utf-8'))
for categ in selected_categories:
selected_engines.extend({'category': categ, 'name': x.name} for x in categories[categ]) 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) results = search(query, request, selected_engines)
for result in results: for result in results:
@ -97,7 +106,7 @@ def index():
return Response(json.dumps({'query': query, 'results': results}), mimetype='application/json') return Response(json.dumps({'query': query, 'results': results}), mimetype='application/json')
template = render('results.html' template = render('results.html'
,results=results ,results=results
,q=query.decode('utf-8') ,q=request_data['q']
,selected_categories=selected_categories ,selected_categories=selected_categories
,number_of_results=len(results) ,number_of_results=len(results)
) )