mirror of https://github.com/searxng/searxng.git
[enh] category support
This commit is contained in:
parent
1aff439931
commit
f76d80de54
|
@ -27,15 +27,23 @@ engine_dir = dirname(realpath(__file__))
|
||||||
|
|
||||||
engines = {}
|
engines = {}
|
||||||
|
|
||||||
|
categories = {'general': []}
|
||||||
|
|
||||||
for filename in listdir(engine_dir):
|
for filename in listdir(engine_dir):
|
||||||
modname = splitext(filename)[0]
|
modname = splitext(filename)[0]
|
||||||
if filename.startswith('_') or not filename.endswith('.py'):
|
if filename.startswith('_') or not filename.endswith('.py'):
|
||||||
continue
|
continue
|
||||||
filepath = join(engine_dir, filename)
|
filepath = join(engine_dir, filename)
|
||||||
engine = load_source(modname, filepath)
|
engine = load_source(modname, filepath)
|
||||||
|
engine.name = modname
|
||||||
if not hasattr(engine, 'request') or not hasattr(engine, 'response'):
|
if not hasattr(engine, 'request') or not hasattr(engine, 'response'):
|
||||||
continue
|
continue
|
||||||
engines[modname] = engine
|
engines[modname] = engine
|
||||||
|
if not hasattr(engine, 'categories'):
|
||||||
|
categories['general'].append(engine)
|
||||||
|
else:
|
||||||
|
for category_name in engine.categories:
|
||||||
|
categories.setdefault(category_name, []).append(engine)
|
||||||
|
|
||||||
def default_request_params():
|
def default_request_params():
|
||||||
return {'method': 'GET', 'headers': {}, 'data': {}, 'url': ''}
|
return {'method': 'GET', 'headers': {}, 'data': {}, 'url': ''}
|
||||||
|
|
|
@ -2,8 +2,8 @@
|
||||||
<input type="text" class="q" name="q" tabindex="1" autocomplete="off" {% if q %}value="{{ q }}"{% endif %}/>
|
<input type="text" class="q" name="q" tabindex="1" autocomplete="off" {% if q %}value="{{ q }}"{% endif %}/>
|
||||||
<input type="submit" value="search" />
|
<input type="submit" value="search" />
|
||||||
<p>
|
<p>
|
||||||
{% for engine in engines %}
|
{% for category in categories %}
|
||||||
{{ engine }}: <input type="checkbox" name="engine_{{ engine }}" checked="checked"/>
|
{{ category }}: <input type="checkbox" name="category_{{ category }}" {% if category in selected_categories %}checked="checked"{% endif %} />
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</p>
|
</p>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -25,7 +25,7 @@ if __name__ == "__main__":
|
||||||
from flask import Flask, request, flash, render_template, url_for, Response
|
from flask import Flask, request, flash, render_template, url_for, Response
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
from os import getenv
|
from os import getenv
|
||||||
from searx.engines import search, engines
|
from searx.engines import search, categories
|
||||||
import json
|
import json
|
||||||
|
|
||||||
cfg = ConfigParser.SafeConfigParser()
|
cfg = ConfigParser.SafeConfigParser()
|
||||||
|
@ -51,27 +51,36 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def render(template_name, **kwargs):
|
def render(template_name, **kwargs):
|
||||||
kwargs['engines'] = engines.keys()
|
global categories
|
||||||
|
kwargs['categories'] = categories.keys()
|
||||||
|
if not 'selected_categories' in kwargs:
|
||||||
|
kwargs['selected_categories'] = ['general']
|
||||||
return render_template(template_name, **kwargs)
|
return render_template(template_name, **kwargs)
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
def index():
|
def index():
|
||||||
|
global categories
|
||||||
if request.method=='POST':
|
if request.method=='POST':
|
||||||
if not request.form.get('q'):
|
if not request.form.get('q'):
|
||||||
flash('Wrong post data')
|
flash('Wrong post data')
|
||||||
return render('index.html')
|
return render('index.html')
|
||||||
selected_engines = []
|
selected_engines = []
|
||||||
|
selected_categories = []
|
||||||
for pd_name,pd in request.form.items():
|
for pd_name,pd in request.form.items():
|
||||||
if pd_name.startswith('engine_'):
|
if pd_name.startswith('category_'):
|
||||||
selected_engines.append(pd_name[7:])
|
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):
|
if not len(selected_engines):
|
||||||
selected_engines = engines.keys()
|
selected_engines = [x.name for x in categories['general']]
|
||||||
query = request.form['q'].encode('utf-8')
|
query = request.form['q'].encode('utf-8')
|
||||||
results = search(query, request, selected_engines)
|
results = search(query, request, selected_engines)
|
||||||
if request.form.get('format') == 'json':
|
if request.form.get('format') == 'json':
|
||||||
# TODO HTTP headers
|
# TODO HTTP headers
|
||||||
return json.dumps({'query': query, 'results': results})
|
return json.dumps({'query': query, 'results': results})
|
||||||
return render('results.html', results=results, q=query.decode('utf-8'))
|
return render('results.html', results=results, q=query.decode('utf-8'), selected_categories=selected_categories)
|
||||||
return render('index.html')
|
return render('index.html')
|
||||||
|
|
||||||
@app.route('/favicon.ico', methods=['GET'])
|
@app.route('/favicon.ico', methods=['GET'])
|
||||||
|
|
Loading…
Reference in New Issue