mirror of
https://github.com/searxng/searxng.git
synced 2024-12-30 05:00:29 +00:00
[enh] categories to request callback
This commit is contained in:
parent
f01b1dc2ce
commit
8520be3cd6
2 changed files with 41 additions and 39 deletions
|
@ -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']
|
||||
|
|
|
@ -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():
|
||||
|
|
Loading…
Reference in a new issue