searxngRebrandZaclys/searx/engines/__init__.py

223 lines
7.3 KiB
Python
Raw Normal View History

2013-10-14 21:09:13 +00:00
2013-10-16 22:32:32 +00:00
'''
searx is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
searx is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with searx. If not, see < http://www.gnu.org/licenses/ >.
(C) 2013- by Adam Tauber, <asciimoo@gmail.com>
'''
from os.path import realpath, dirname
2014-02-05 19:24:31 +00:00
import sys
from flask_babel import gettext
2014-07-07 11:59:27 +00:00
from operator import itemgetter
from json import loads
from requests import get
2014-02-05 19:24:31 +00:00
from searx import settings
2015-01-09 03:13:05 +00:00
from searx import logger
from searx.utils import load_module
2015-01-09 03:13:05 +00:00
logger = logger.getChild('engines')
2013-10-14 21:09:13 +00:00
engine_dir = dirname(realpath(__file__))
2013-10-23 21:54:46 +00:00
2013-10-15 17:11:43 +00:00
engines = {}
2013-10-14 21:09:13 +00:00
2013-10-17 19:06:28 +00:00
categories = {'general': []}
2016-12-28 19:00:53 +00:00
languages = loads(open(engine_dir + '/../data/engines_languages.json').read())
2013-10-17 19:06:28 +00:00
engine_shortcuts = {}
2016-02-19 14:13:01 +00:00
engine_default_args = {'paging': False,
'categories': ['general'],
'language_support': True,
'supported_languages': [],
2016-02-19 14:13:01 +00:00
'safesearch': False,
'timeout': settings['outgoing']['request_timeout'],
'shortcut': '-',
'disabled': False,
'suspend_end_time': 0,
2016-07-17 16:42:30 +00:00
'continuous_errors': 0,
'time_range_support': False}
2014-12-13 18:26:40 +00:00
def load_engine(engine_data):
if '_' in engine_data['name']:
logger.error('Engine name conains underscore: "{}"'.format(engine_data['name']))
sys.exit(1)
engine_module = engine_data['engine']
try:
engine = load_module(engine_module + '.py', engine_dir)
except:
logger.exception('Cannot load engine "{}"'.format(engine_module))
return None
2014-01-31 03:35:23 +00:00
2013-10-23 21:54:46 +00:00
for param_name in engine_data:
if param_name == 'engine':
continue
if param_name == 'categories':
if engine_data['categories'] == 'none':
engine.categories = []
else:
engine.categories = map(
str.strip, engine_data['categories'].split(','))
2013-10-23 21:54:46 +00:00
continue
setattr(engine, param_name, engine_data[param_name])
2016-02-19 14:13:01 +00:00
for arg_name, arg_value in engine_default_args.iteritems():
if not hasattr(engine, arg_name):
setattr(engine, arg_name, arg_value)
# checking required variables
2013-10-25 21:41:14 +00:00
for engine_attr in dir(engine):
if engine_attr.startswith('_'):
continue
2014-01-20 01:31:20 +00:00
if getattr(engine, engine_attr) is None:
2015-01-09 03:13:05 +00:00
logger.error('Missing engine config attribute: "{0}.{1}"'
.format(engine.name, engine_attr))
2013-10-25 21:41:14 +00:00
sys.exit(1)
# assign supported languages from json file
if engine_data['name'] in languages:
setattr(engine, 'supported_languages', languages[engine_data['name']])
# assign language fetching method if auxiliary method exists
if hasattr(engine, '_fetch_supported_languages'):
setattr(engine, 'fetch_supported_languages',
lambda: engine._fetch_supported_languages(get(engine.supported_languages_url)))
engine.stats = {
'result_count': 0,
'search_count': 0,
'page_load_time': 0,
'page_load_count': 0,
'engine_time': 0,
'engine_time_count': 0,
'score_count': 0,
'errors': 0
}
2016-02-19 14:13:01 +00:00
for category_name in engine.categories:
categories.setdefault(category_name, []).append(engine)
if engine.shortcut in engine_shortcuts:
logger.error('Engine config error: ambigious shortcut: {0}'.format(engine.shortcut))
sys.exit(1)
engine_shortcuts[engine.shortcut] = engine.name
2013-10-15 16:19:06 +00:00
2014-12-13 18:26:40 +00:00
return engine
def to_percentage(stats, maxvalue):
for engine_stat in stats:
if maxvalue:
engine_stat['percentage'] = int(engine_stat['avg'] / maxvalue * 100)
else:
engine_stat['percentage'] = 0
return stats
2013-10-26 23:02:28 +00:00
def get_engines_stats():
2014-01-05 22:13:53 +00:00
# TODO refactor
2013-10-27 00:50:24 +00:00
pageloads = []
engine_times = []
2013-10-27 00:50:24 +00:00
results = []
2013-10-27 13:21:23 +00:00
scores = []
errors = []
2014-01-05 22:13:53 +00:00
scores_per_result = []
2013-10-26 23:02:28 +00:00
max_pageload = max_engine_times = max_results = max_score = max_errors = max_score_per_result = 0 # noqa
2013-10-26 23:02:28 +00:00
for engine in engines.values():
if engine.stats['search_count'] == 0:
continue
results_num = \
engine.stats['result_count'] / float(engine.stats['search_count'])
if engine.stats['page_load_count'] != 0:
load_times = engine.stats['page_load_time'] / float(engine.stats['page_load_count']) # noqa
else:
load_times = 0
if engine.stats['engine_time_count'] != 0:
this_engine_time = engine.stats['engine_time'] / float(engine.stats['engine_time_count']) # noqa
else:
this_engine_time = 0
2013-10-27 13:21:23 +00:00
if results_num:
score = engine.stats['score_count'] / float(engine.stats['search_count']) # noqa
2014-01-05 22:13:53 +00:00
score_per_result = score / results_num
2013-10-27 13:21:23 +00:00
else:
2014-01-05 22:13:53 +00:00
score = score_per_result = 0.0
2013-10-27 00:50:24 +00:00
max_pageload = max(load_times, max_pageload)
max_engine_times = max(this_engine_time, max_engine_times)
max_results = max(results_num, max_results)
2013-10-27 13:21:23 +00:00
max_score = max(score, max_score)
2014-01-05 22:13:53 +00:00
max_score_per_result = max(score_per_result, max_score_per_result)
max_errors = max(max_errors, engine.stats['errors'])
2013-10-27 00:50:24 +00:00
pageloads.append({'avg': load_times, 'name': engine.name})
engine_times.append({'avg': this_engine_time, 'name': engine.name})
2013-10-27 00:50:24 +00:00
results.append({'avg': results_num, 'name': engine.name})
2013-10-27 13:21:23 +00:00
scores.append({'avg': score, 'name': engine.name})
errors.append({'avg': engine.stats['errors'], 'name': engine.name})
scores_per_result.append({
'avg': score_per_result,
'name': engine.name
})
2013-10-27 00:50:24 +00:00
pageloads = to_percentage(pageloads, max_pageload)
engine_times = to_percentage(engine_times, max_engine_times)
results = to_percentage(results, max_results)
scores = to_percentage(scores, max_score)
scores_per_result = to_percentage(scores_per_result, max_score_per_result)
erros = to_percentage(errors, max_errors)
return [
(
gettext('Engine time (sec)'),
sorted(engine_times, key=itemgetter('avg'))
),
(
2014-01-21 23:17:49 +00:00
gettext('Page loads (sec)'),
sorted(pageloads, key=itemgetter('avg'))
),
(
gettext('Number of results'),
sorted(results, key=itemgetter('avg'), reverse=True)
),
(
2014-01-21 23:17:49 +00:00
gettext('Scores'),
sorted(scores, key=itemgetter('avg'), reverse=True)
),
(
gettext('Scores per result'),
sorted(scores_per_result, key=itemgetter('avg'), reverse=True)
),
2014-01-21 23:17:49 +00:00
(
gettext('Errors'),
sorted(errors, key=itemgetter('avg'), reverse=True)
),
]
2014-12-13 18:26:40 +00:00
2016-12-27 16:25:19 +00:00
def initialize_engines(engine_list):
for engine_data in engine_list:
engine = load_engine(engine_data)
if engine is not None:
engines[engine.name] = engine