[enh] categories to request callback

This commit is contained in:
asciimoo 2013-10-22 18:57:20 +02:00
parent f01b1dc2ce
commit 8520be3cd6
2 changed files with 41 additions and 39 deletions

View file

@ -61,18 +61,25 @@ def make_callback(engine_name, results, callback):
results[engine_name] = cb_res results[engine_name] = cb_res
return process_callback return process_callback
def search(query, request, selected_engines): def search(query, request, selected_categories):
global engines global engines, categories
requests = [] requests = []
results = {} results = {}
selected_engines = []
user_agent = request.headers.get('User-Agent', '') user_agent = request.headers.get('User-Agent', '')
for ename, engine in engines.items(): if not len(selected_categories):
if ename not in selected_engines: 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 continue
engine = engines[selected_engine['name']]
request_params = default_request_params() request_params = default_request_params()
request_params['headers']['User-Agent'] = user_agent request_params['headers']['User-Agent'] = user_agent
request_params['category'] = selected_engine['category']
request_params = engine.request(query, request_params) 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': if request_params['method'] == 'GET':
req = grequests.get(request_params['url'] req = grequests.get(request_params['url']
,headers=request_params['headers'] ,headers=request_params['headers']

View file

@ -63,7 +63,8 @@ def index():
request_data = request.form request_data = request.form
else: else:
request_data = request.args request_data = request.args
if request_data.get('q'): if not request_data.get('q'):
return render('index.html')
selected_engines = [] selected_engines = []
selected_categories = [] selected_categories = []
for pd_name,pd in request_data.items(): for pd_name,pd in request_data.items():
@ -72,18 +73,13 @@ def index():
if not category in categories: if not category in categories:
continue continue
selected_categories.append(category) selected_categories.append(category)
selected_engines.extend(x.name for x in categories[category]) if not len(selected_categories):
if not len(selected_engines):
cookie_categories = request.cookies.get('categories', '').split(',') cookie_categories = request.cookies.get('categories', '').split(',')
for ccateg in cookie_categories: for ccateg in cookie_categories:
if ccateg in categories: if ccateg in categories:
selected_categories.append(ccateg) 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') query = request_data['q'].encode('utf-8')
results = search(query, request, selected_engines) results = search(query, request, selected_categories)
if request_data.get('format') == 'json': if request_data.get('format') == 'json':
# TODO HTTP headers # TODO HTTP headers
return json.dumps({'query': query, 'results': results}) return json.dumps({'query': query, 'results': results})
@ -96,7 +92,6 @@ def index():
resp = make_response(template) resp = make_response(template)
resp.set_cookie('categories', ','.join(selected_categories)) resp.set_cookie('categories', ','.join(selected_categories))
return resp return resp
return render('index.html')
@app.route('/favicon.ico', methods=['GET']) @app.route('/favicon.ico', methods=['GET'])
def fav(): def fav():