[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,40 +63,35 @@ 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'):
selected_engines = [] return render('index.html')
selected_categories = [] selected_engines = []
for pd_name,pd in request_data.items(): selected_categories = []
if pd_name.startswith('category_'): for pd_name,pd in request_data.items():
category = pd_name[9:] if pd_name.startswith('category_'):
if not category in categories: category = pd_name[9:]
continue if not category in categories:
selected_categories.append(category) continue
selected_engines.extend(x.name for x in categories[category]) selected_categories.append(category)
if not len(selected_engines): if not len(selected_categories):
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]) query = request_data['q'].encode('utf-8')
if not len(selected_engines): results = search(query, request, selected_categories)
selected_categories.append('general') if request_data.get('format') == 'json':
selected_engines.extend(x.name for x in categories['general']) # TODO HTTP headers
query = request_data['q'].encode('utf-8') return json.dumps({'query': query, 'results': results})
results = search(query, request, selected_engines) template = render('results.html'
if request_data.get('format') == 'json': ,results=results
# TODO HTTP headers ,q=query.decode('utf-8')
return json.dumps({'query': query, 'results': results}) ,selected_categories=selected_categories
template = render('results.html' ,number_of_results=len(results)
,results=results )
,q=query.decode('utf-8') resp = make_response(template)
,selected_categories=selected_categories resp.set_cookie('categories', ','.join(selected_categories))
,number_of_results=len(results) return resp
)
resp = make_response(template)
resp.set_cookie('categories', ','.join(selected_categories))
return resp
return render('index.html')
@app.route('/favicon.ico', methods=['GET']) @app.route('/favicon.ico', methods=['GET'])
def fav(): def fav():