[enh] specify search engine in queries

This commit is contained in:
asciimoo 2013-11-04 00:21:27 +01:00
parent 70278567ec
commit 64cc960f9f

View File

@ -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,9 +74,11 @@ def index():
if not request_data.get('q'):
return render('index.html')
query = request_data['q'].encode('utf-8')
selected_categories = []
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:]
@ -83,7 +93,6 @@ def index():
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])
@ -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)
)