|
@@ -17,26 +17,36 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
|
|
(C) 2013- by Adam Tauber, <asciimoo@gmail.com>
|
|
(C) 2013- by Adam Tauber, <asciimoo@gmail.com>
|
|
'''
|
|
'''
|
|
|
|
|
|
|
|
+import json
|
|
|
|
+import cStringIO
|
|
import os
|
|
import os
|
|
-import sys
|
|
|
|
-if __name__ == "__main__":
|
|
|
|
- sys.path.append(os.path.realpath(os.path.dirname(os.path.realpath(__file__))+'/../'))
|
|
|
|
|
|
|
|
-from searx import settings
|
|
|
|
|
|
+from flask import Flask, request, render_template
|
|
|
|
+from flask import url_for, Response, make_response, redirect
|
|
|
|
+from flask import send_from_directory
|
|
|
|
|
|
-from flask import Flask, request, render_template, url_for, Response, make_response, redirect
|
|
|
|
|
|
+from searx import settings
|
|
from searx.engines import search, categories, engines, get_engines_stats
|
|
from searx.engines import search, categories, engines, get_engines_stats
|
|
-import json
|
|
|
|
-import cStringIO
|
|
|
|
from searx.utils import UnicodeWriter
|
|
from searx.utils import UnicodeWriter
|
|
-from flask import send_from_directory
|
|
|
|
from searx.utils import highlight_content, html_to_text
|
|
from searx.utils import highlight_content, html_to_text
|
|
|
|
|
|
|
|
+from flask.ext.babel import Babel
|
|
|
|
+
|
|
|
|
|
|
|
|
+app = Flask(
|
|
|
|
+ __name__,
|
|
|
|
+ static_folder=os.path.join(os.path.dirname(__file__), 'static'),
|
|
|
|
+ template_folder=os.path.join(os.path.dirname(__file__), 'templates')
|
|
|
|
+)
|
|
|
|
|
|
-app = Flask(__name__)
|
|
|
|
app.secret_key = settings['server']['secret_key']
|
|
app.secret_key = settings['server']['secret_key']
|
|
|
|
|
|
|
|
+babel = Babel(app)
|
|
|
|
+
|
|
|
|
+#TODO configurable via settings.yml
|
|
|
|
+favicons = ['wikipedia', 'youtube', 'vimeo', 'soundcloud',
|
|
|
|
+ 'twitter', 'stackoverflow', 'github']
|
|
|
|
+
|
|
|
|
|
|
opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
|
opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
|
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
|
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
|
@@ -51,6 +61,24 @@ opensearch_xml = '''<?xml version="1.0" encoding="utf-8"?>
|
|
'''
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
+@babel.localeselector
|
|
|
|
+def get_locale():
|
|
|
|
+ locale = request.accept_languages.best_match(settings['locales'].keys())
|
|
|
|
+
|
|
|
|
+ if request.cookies.get('locale', '') in settings['locales']:
|
|
|
|
+ locale = request.cookies.get('locale', '')
|
|
|
|
+
|
|
|
|
+ if 'locale' in request.args\
|
|
|
|
+ and request.args['locale'] in settings['locales']:
|
|
|
|
+ locale = request.args['locale']
|
|
|
|
+
|
|
|
|
+ if 'locale' in request.form\
|
|
|
|
+ and request.form['locale'] in settings['locales']:
|
|
|
|
+ locale = request.form['locale']
|
|
|
|
+
|
|
|
|
+ return locale
|
|
|
|
+
|
|
|
|
+
|
|
def get_base_url():
|
|
def get_base_url():
|
|
if settings['server']['base_url']:
|
|
if settings['server']['base_url']:
|
|
hostname = settings['server']['base_url']
|
|
hostname = settings['server']['base_url']
|
|
@@ -65,7 +93,8 @@ def get_base_url():
|
|
def render(template_name, **kwargs):
|
|
def render(template_name, **kwargs):
|
|
global categories
|
|
global categories
|
|
kwargs['categories'] = ['general']
|
|
kwargs['categories'] = ['general']
|
|
- kwargs['categories'].extend(x for x in sorted(categories.keys()) if x != 'general')
|
|
|
|
|
|
+ kwargs['categories'].extend(x for x in
|
|
|
|
+ sorted(categories.keys()) if x != 'general')
|
|
if not 'selected_categories' in kwargs:
|
|
if not 'selected_categories' in kwargs:
|
|
kwargs['selected_categories'] = []
|
|
kwargs['selected_categories'] = []
|
|
cookie_categories = request.cookies.get('categories', '').split(',')
|
|
cookie_categories = request.cookies.get('categories', '').split(',')
|
|
@@ -76,6 +105,7 @@ def render(template_name, **kwargs):
|
|
kwargs['selected_categories'] = ['general']
|
|
kwargs['selected_categories'] = ['general']
|
|
return render_template(template_name, **kwargs)
|
|
return render_template(template_name, **kwargs)
|
|
|
|
|
|
|
|
+
|
|
def parse_query(query):
|
|
def parse_query(query):
|
|
query_engines = []
|
|
query_engines = []
|
|
query_parts = query.split()
|
|
query_parts = query.split()
|
|
@@ -89,7 +119,7 @@ def parse_query(query):
|
|
def index():
|
|
def index():
|
|
global categories
|
|
global categories
|
|
|
|
|
|
- if request.method=='POST':
|
|
|
|
|
|
+ if request.method == 'POST':
|
|
request_data = request.form
|
|
request_data = request.form
|
|
else:
|
|
else:
|
|
request_data = request.args
|
|
request_data = request.args
|
|
@@ -101,14 +131,15 @@ def index():
|
|
query, selected_engines = parse_query(request_data['q'].encode('utf-8'))
|
|
query, selected_engines = parse_query(request_data['q'].encode('utf-8'))
|
|
|
|
|
|
if not len(selected_engines):
|
|
if not len(selected_engines):
|
|
- for pd_name,pd in request_data.items():
|
|
|
|
|
|
+ for pd_name, pd in request_data.items():
|
|
if pd_name.startswith('category_'):
|
|
if pd_name.startswith('category_'):
|
|
category = pd_name[9:]
|
|
category = pd_name[9:]
|
|
if not category in categories:
|
|
if not category in categories:
|
|
continue
|
|
continue
|
|
selected_categories.append(category)
|
|
selected_categories.append(category)
|
|
if not len(selected_categories):
|
|
if not len(selected_categories):
|
|
- cookie_categories = request.cookies.get('categories', '').split(',')
|
|
|
|
|
|
+ cookie_categories = request.cookies.get('categories', '')
|
|
|
|
+ cookie_categories = cookie_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)
|
|
@@ -116,7 +147,9 @@ def index():
|
|
selected_categories = ['general']
|
|
selected_categories = ['general']
|
|
|
|
|
|
for categ in selected_categories:
|
|
for categ in selected_categories:
|
|
- selected_engines.extend({'category': categ, 'name': x.name} for x in categories[categ])
|
|
|
|
|
|
+ selected_engines.extend({'category': categ,
|
|
|
|
+ 'name': x.name}
|
|
|
|
+ for x in categories[categ])
|
|
|
|
|
|
results, suggestions = search(query, request, selected_engines)
|
|
results, suggestions = search(query, request, selected_engines)
|
|
|
|
|
|
@@ -131,16 +164,18 @@ def index():
|
|
result['content'] = html_to_text(result['content']).strip()
|
|
result['content'] = html_to_text(result['content']).strip()
|
|
result['title'] = html_to_text(result['title']).strip()
|
|
result['title'] = html_to_text(result['title']).strip()
|
|
if len(result['url']) > 74:
|
|
if len(result['url']) > 74:
|
|
- result['pretty_url'] = result['url'][:35] + '[..]' + result['url'][-35:]
|
|
|
|
|
|
+ url_parts = result['url'][:35], result['url'][-35:]
|
|
|
|
+ result['pretty_url'] = '{0}[...]{1}'.format(*url_parts)
|
|
else:
|
|
else:
|
|
result['pretty_url'] = result['url']
|
|
result['pretty_url'] = result['url']
|
|
|
|
|
|
for engine in result['engines']:
|
|
for engine in result['engines']:
|
|
- if engine in ['wikipedia', 'youtube', 'vimeo', 'soundcloud', 'twitter', 'stackoverflow', 'github']:
|
|
|
|
|
|
+ if engine in favicons:
|
|
result['favicon'] = engine
|
|
result['favicon'] = engine
|
|
|
|
|
|
if request_data.get('format') == 'json':
|
|
if request_data.get('format') == 'json':
|
|
- return Response(json.dumps({'query': query, 'results': results}), mimetype='application/json')
|
|
|
|
|
|
+ return Response(json.dumps({'query': query, 'results': results}),
|
|
|
|
+ mimetype='application/json')
|
|
elif request_data.get('format') == 'csv':
|
|
elif request_data.get('format') == 'csv':
|
|
csv = UnicodeWriter(cStringIO.StringIO())
|
|
csv = UnicodeWriter(cStringIO.StringIO())
|
|
keys = ('title', 'url', 'content', 'host', 'engine', 'score')
|
|
keys = ('title', 'url', 'content', 'host', 'engine', 'score')
|
|
@@ -151,26 +186,28 @@ def index():
|
|
csv.writerow([row.get(key, '') for key in keys])
|
|
csv.writerow([row.get(key, '') for key in keys])
|
|
csv.stream.seek(0)
|
|
csv.stream.seek(0)
|
|
response = Response(csv.stream.read(), mimetype='application/csv')
|
|
response = Response(csv.stream.read(), mimetype='application/csv')
|
|
- response.headers.add('Content-Disposition', 'attachment;Filename=searx_-_{0}.csv'.format('_'.join(query.split())))
|
|
|
|
|
|
+ content_disp = 'attachment;Filename=searx_-_{0}.csv'.format(query)
|
|
|
|
+ response.headers.add('Content-Disposition', content_disp)
|
|
return response
|
|
return response
|
|
elif request_data.get('format') == 'rss':
|
|
elif request_data.get('format') == 'rss':
|
|
- response_rss = render('opensearch_response_rss.xml'
|
|
|
|
- ,results=results
|
|
|
|
- ,q=request_data['q']
|
|
|
|
- ,number_of_results=len(results)
|
|
|
|
- ,base_url=get_base_url()
|
|
|
|
- )
|
|
|
|
|
|
+ response_rss = render(
|
|
|
|
+ 'opensearch_response_rss.xml',
|
|
|
|
+ results=results,
|
|
|
|
+ q=request_data['q'],
|
|
|
|
+ number_of_results=len(results),
|
|
|
|
+ base_url=get_base_url()
|
|
|
|
+ )
|
|
return Response(response_rss, mimetype='text/xml')
|
|
return Response(response_rss, mimetype='text/xml')
|
|
|
|
|
|
-
|
|
|
|
- return render('results.html'
|
|
|
|
- ,results=results
|
|
|
|
- ,q=request_data['q']
|
|
|
|
- ,selected_categories=selected_categories
|
|
|
|
- ,number_of_results=len(results)+len(featured_results)
|
|
|
|
- ,featured_results=featured_results
|
|
|
|
- ,suggestions=suggestions
|
|
|
|
- )
|
|
|
|
|
|
+ return render(
|
|
|
|
+ 'results.html',
|
|
|
|
+ results=results,
|
|
|
|
+ q=request_data['q'],
|
|
|
|
+ selected_categories=selected_categories,
|
|
|
|
+ number_of_results=len(results) + len(featured_results),
|
|
|
|
+ featured_results=featured_results,
|
|
|
|
+ suggestions=suggestions
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
@app.route('/about', methods=['GET'])
|
|
@app.route('/about', methods=['GET'])
|
|
@@ -187,20 +224,37 @@ def list_engines():
|
|
@app.route('/preferences', methods=['GET', 'POST'])
|
|
@app.route('/preferences', methods=['GET', 'POST'])
|
|
def preferences():
|
|
def preferences():
|
|
|
|
|
|
- if request.method=='POST':
|
|
|
|
|
|
+ if request.method == 'POST':
|
|
selected_categories = []
|
|
selected_categories = []
|
|
- for pd_name,pd in request.form.items():
|
|
|
|
|
|
+ locale = None
|
|
|
|
+ for pd_name, pd in request.form.items():
|
|
if pd_name.startswith('category_'):
|
|
if pd_name.startswith('category_'):
|
|
category = pd_name[9:]
|
|
category = pd_name[9:]
|
|
if not category in categories:
|
|
if not category in categories:
|
|
continue
|
|
continue
|
|
selected_categories.append(category)
|
|
selected_categories.append(category)
|
|
|
|
+ elif pd_name == 'locale' and pd in settings['locales']:
|
|
|
|
+ locale = pd
|
|
|
|
+
|
|
|
|
+ resp = make_response(redirect('/'))
|
|
|
|
+
|
|
|
|
+ if locale:
|
|
|
|
+ # cookie max age: 4 weeks
|
|
|
|
+ resp.set_cookie(
|
|
|
|
+ 'locale', locale,
|
|
|
|
+ max_age=60 * 60 * 24 * 7 * 4
|
|
|
|
+ )
|
|
|
|
+
|
|
if selected_categories:
|
|
if selected_categories:
|
|
- resp = make_response(redirect('/'))
|
|
|
|
# cookie max age: 4 weeks
|
|
# cookie max age: 4 weeks
|
|
- resp.set_cookie('categories', ','.join(selected_categories), max_age=60*60*24*7*4)
|
|
|
|
- return resp
|
|
|
|
- return render('preferences.html')
|
|
|
|
|
|
+ resp.set_cookie(
|
|
|
|
+ 'categories', ','.join(selected_categories),
|
|
|
|
+ max_age=60 * 60 * 24 * 7 * 4
|
|
|
|
+ )
|
|
|
|
+ return resp
|
|
|
|
+ return render('preferences.html',
|
|
|
|
+ locales=settings['locales'],
|
|
|
|
+ current_locale=get_locale())
|
|
|
|
|
|
|
|
|
|
@app.route('/stats', methods=['GET'])
|
|
@app.route('/stats', methods=['GET'])
|
|
@@ -216,6 +270,7 @@ def robots():
|
|
Allow: /
|
|
Allow: /
|
|
Allow: /about
|
|
Allow: /about
|
|
Disallow: /stats
|
|
Disallow: /stats
|
|
|
|
+Disallow: /engines
|
|
""", mimetype='text/plain')
|
|
""", mimetype='text/plain')
|
|
|
|
|
|
|
|
|
|
@@ -229,24 +284,27 @@ def opensearch():
|
|
base_url = get_base_url()
|
|
base_url = get_base_url()
|
|
ret = opensearch_xml.format(method=method, host=base_url)
|
|
ret = opensearch_xml.format(method=method, host=base_url)
|
|
resp = Response(response=ret,
|
|
resp = Response(response=ret,
|
|
- status=200,
|
|
|
|
- mimetype="application/xml")
|
|
|
|
|
|
+ status=200,
|
|
|
|
+ mimetype="application/xml")
|
|
return resp
|
|
return resp
|
|
|
|
|
|
|
|
+
|
|
@app.route('/favicon.ico')
|
|
@app.route('/favicon.ico')
|
|
def favicon():
|
|
def favicon():
|
|
return send_from_directory(os.path.join(app.root_path, 'static/img'),
|
|
return send_from_directory(os.path.join(app.root_path, 'static/img'),
|
|
- 'favicon.png', mimetype='image/vnd.microsoft.icon')
|
|
|
|
|
|
+ 'favicon.png',
|
|
|
|
+ mimetype='image/vnd.microsoft.icon')
|
|
|
|
|
|
|
|
|
|
def run():
|
|
def run():
|
|
from gevent import monkey
|
|
from gevent import monkey
|
|
monkey.patch_all()
|
|
monkey.patch_all()
|
|
|
|
|
|
- app.run(debug = settings['server']['debug']
|
|
|
|
- ,use_debugger = settings['server']['debug']
|
|
|
|
- ,port = settings['server']['port']
|
|
|
|
- )
|
|
|
|
|
|
+ app.run(
|
|
|
|
+ debug=settings['server']['debug'],
|
|
|
|
+ use_debugger=settings['server']['debug'],
|
|
|
|
+ port=settings['server']['port']
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|