2021-03-02 14:24:55 +01:00
|
|
|
from collections import defaultdict
|
2020-09-24 16:26:00 +02:00
|
|
|
from typing import Dict, List, Optional, Tuple
|
2020-09-22 13:59:27 +02:00
|
|
|
from searx.exceptions import SearxParameterException
|
2020-10-01 11:29:31 +02:00
|
|
|
from searx.webutils import VALID_LANGUAGE_CODE
|
|
|
|
from searx.query import RawTextQuery
|
2020-09-22 13:59:27 +02:00
|
|
|
from searx.engines import categories, engines
|
2020-09-22 16:22:22 +02:00
|
|
|
from searx.search import SearchQuery, EngineRef
|
2020-10-23 16:22:55 +02:00
|
|
|
from searx.preferences import Preferences, is_locked
|
2020-09-22 13:59:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
# remove duplicate queries.
|
|
|
|
# FIXME: does not fix "!music !soundcloud", because the categories are 'none' and 'music'
|
2020-09-24 16:26:00 +02:00
|
|
|
def deduplicate_engineref_list(engineref_list: List[EngineRef]) -> List[EngineRef]:
|
2020-09-22 16:22:22 +02:00
|
|
|
engineref_dict = {q.category + '|' + q.name: q for q in engineref_list}
|
2020-10-06 15:23:19 +02:00
|
|
|
return list(engineref_dict.values())
|
2020-09-22 13:59:27 +02:00
|
|
|
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
def validate_engineref_list(
|
|
|
|
engineref_list: List[EngineRef], preferences: Preferences
|
|
|
|
) -> Tuple[List[EngineRef], List[EngineRef], List[EngineRef]]:
|
2020-09-24 16:26:00 +02:00
|
|
|
"""Validate query_engines according to the preferences
|
2020-09-10 18:08:14 +02:00
|
|
|
|
2020-09-24 16:26:00 +02:00
|
|
|
Returns:
|
|
|
|
List[EngineRef]: list of existing engines with a validated token
|
|
|
|
List[EngineRef]: list of unknown engine
|
|
|
|
List[EngineRef]: list of engine with invalid token according to the preferences
|
2020-09-22 16:55:59 +02:00
|
|
|
"""
|
|
|
|
valid = []
|
|
|
|
unknown = []
|
|
|
|
no_token = []
|
|
|
|
for engineref in engineref_list:
|
|
|
|
if engineref.name not in engines:
|
|
|
|
unknown.append(engineref)
|
|
|
|
continue
|
|
|
|
|
|
|
|
engine = engines[engineref.name]
|
|
|
|
if not preferences.validate_token(engine):
|
|
|
|
no_token.append(engineref)
|
|
|
|
continue
|
|
|
|
|
|
|
|
valid.append(engineref)
|
|
|
|
return valid, unknown, no_token
|
|
|
|
|
|
|
|
|
2020-09-24 16:26:00 +02:00
|
|
|
def parse_pageno(form: Dict[str, str]) -> int:
|
2020-09-22 13:59:27 +02:00
|
|
|
pageno_param = form.get('pageno', '1')
|
|
|
|
if not pageno_param.isdigit() or int(pageno_param) < 1:
|
|
|
|
raise SearxParameterException('pageno', pageno_param)
|
2020-09-22 17:45:32 +02:00
|
|
|
return int(pageno_param)
|
2020-09-22 13:59:27 +02:00
|
|
|
|
2020-09-22 17:45:32 +02:00
|
|
|
|
2020-09-24 16:26:00 +02:00
|
|
|
def parse_lang(preferences: Preferences, form: Dict[str, str], raw_text_query: RawTextQuery) -> str:
|
2020-10-23 16:22:55 +02:00
|
|
|
if is_locked('language'):
|
2021-09-04 08:39:45 +02:00
|
|
|
return preferences.get_value('language')
|
2020-09-22 13:59:27 +02:00
|
|
|
# get language
|
|
|
|
# set specific language if set on request, query or preferences
|
2022-09-27 17:01:00 +02:00
|
|
|
# TODO support search with multiple languages
|
2020-09-22 13:59:27 +02:00
|
|
|
if len(raw_text_query.languages):
|
|
|
|
query_lang = raw_text_query.languages[-1]
|
|
|
|
elif 'language' in form:
|
|
|
|
query_lang = form.get('language')
|
|
|
|
else:
|
|
|
|
query_lang = preferences.get_value('language')
|
|
|
|
|
|
|
|
# check language
|
|
|
|
if not VALID_LANGUAGE_CODE.match(query_lang):
|
|
|
|
raise SearxParameterException('language', query_lang)
|
|
|
|
|
2020-09-22 17:45:32 +02:00
|
|
|
return query_lang
|
|
|
|
|
|
|
|
|
2020-09-24 16:26:00 +02:00
|
|
|
def parse_safesearch(preferences: Preferences, form: Dict[str, str]) -> int:
|
2020-10-23 16:22:55 +02:00
|
|
|
if is_locked('safesearch'):
|
|
|
|
return preferences.get_value('safesearch')
|
|
|
|
|
2020-09-22 13:59:27 +02:00
|
|
|
if 'safesearch' in form:
|
|
|
|
query_safesearch = form.get('safesearch')
|
|
|
|
# first check safesearch
|
|
|
|
if not query_safesearch.isdigit():
|
|
|
|
raise SearxParameterException('safesearch', query_safesearch)
|
|
|
|
query_safesearch = int(query_safesearch)
|
|
|
|
else:
|
|
|
|
query_safesearch = preferences.get_value('safesearch')
|
|
|
|
|
|
|
|
# safesearch : second check
|
|
|
|
if query_safesearch < 0 or query_safesearch > 2:
|
|
|
|
raise SearxParameterException('safesearch', query_safesearch)
|
|
|
|
|
2020-09-22 17:45:32 +02:00
|
|
|
return query_safesearch
|
|
|
|
|
2020-09-22 13:59:27 +02:00
|
|
|
|
2020-10-06 15:23:19 +02:00
|
|
|
def parse_time_range(form: Dict[str, str]) -> Optional[str]:
|
2020-09-22 17:45:32 +02:00
|
|
|
query_time_range = form.get('time_range')
|
2020-09-22 13:59:27 +02:00
|
|
|
# check time_range
|
2020-09-22 16:31:17 +02:00
|
|
|
query_time_range = None if query_time_range in ('', 'None') else query_time_range
|
|
|
|
if query_time_range not in (None, 'day', 'week', 'month', 'year'):
|
2020-09-22 13:59:27 +02:00
|
|
|
raise SearxParameterException('time_range', query_time_range)
|
2020-09-22 17:45:32 +02:00
|
|
|
return query_time_range
|
2020-09-22 13:59:27 +02:00
|
|
|
|
|
|
|
|
2020-09-24 16:26:00 +02:00
|
|
|
def parse_timeout(form: Dict[str, str], raw_text_query: RawTextQuery) -> Optional[float]:
|
2020-10-06 15:23:19 +02:00
|
|
|
timeout_limit = raw_text_query.timeout_limit
|
|
|
|
if timeout_limit is None:
|
|
|
|
timeout_limit = form.get('timeout_limit')
|
|
|
|
|
|
|
|
if timeout_limit is None or timeout_limit in ['None', '']:
|
|
|
|
return None
|
|
|
|
try:
|
|
|
|
return float(timeout_limit)
|
2020-12-17 09:57:57 +01:00
|
|
|
except ValueError as e:
|
|
|
|
raise SearxParameterException('timeout_limit', timeout_limit) from e
|
2020-09-22 13:59:27 +02:00
|
|
|
|
2020-09-22 17:45:32 +02:00
|
|
|
|
2020-09-24 16:26:00 +02:00
|
|
|
def parse_category_form(query_categories: List[str], name: str, value: str) -> None:
|
2020-09-22 18:03:42 +02:00
|
|
|
if name == 'categories':
|
|
|
|
query_categories.extend(categ for categ in map(str.strip, value.split(',')) if categ in categories)
|
|
|
|
elif name.startswith('category_'):
|
|
|
|
category = name[9:]
|
|
|
|
|
|
|
|
# if category is not found in list, skip
|
|
|
|
if category not in categories:
|
|
|
|
return
|
|
|
|
|
|
|
|
if value != 'off':
|
|
|
|
# add category to list
|
|
|
|
query_categories.append(category)
|
|
|
|
elif category in query_categories:
|
|
|
|
# remove category from list if property is set to 'off'
|
|
|
|
query_categories.remove(category)
|
|
|
|
|
|
|
|
|
2020-10-06 15:23:19 +02:00
|
|
|
def get_selected_categories(preferences: Preferences, form: Optional[Dict[str, str]]) -> List[str]:
|
2020-09-22 18:03:42 +02:00
|
|
|
selected_categories = []
|
|
|
|
|
2020-11-03 10:55:49 +01:00
|
|
|
if not is_locked('categories') and form is not None:
|
2020-09-22 18:03:42 +02:00
|
|
|
for name, value in form.items():
|
|
|
|
parse_category_form(selected_categories, name, value)
|
|
|
|
|
|
|
|
# if no category is specified for this search,
|
|
|
|
# using user-defined default-configuration which
|
|
|
|
# (is stored in cookie)
|
|
|
|
if not selected_categories:
|
|
|
|
cookie_categories = preferences.get_value('categories')
|
|
|
|
for ccateg in cookie_categories:
|
|
|
|
selected_categories.append(ccateg)
|
|
|
|
|
|
|
|
# if still no category is specified, using general
|
|
|
|
# as default-category
|
|
|
|
if not selected_categories:
|
|
|
|
selected_categories = ['general']
|
|
|
|
|
|
|
|
return selected_categories
|
|
|
|
|
|
|
|
|
2020-10-06 15:23:19 +02:00
|
|
|
def get_engineref_from_category_list(category_list: List[str], disabled_engines: List[str]) -> List[EngineRef]:
|
|
|
|
result = []
|
|
|
|
for categ in category_list:
|
2021-12-27 09:26:22 +01:00
|
|
|
result.extend(
|
|
|
|
EngineRef(engine.name, categ)
|
|
|
|
for engine in categories[categ]
|
|
|
|
if (engine.name, categ) not in disabled_engines
|
|
|
|
)
|
2020-10-06 15:23:19 +02:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-12-17 13:51:57 +01:00
|
|
|
def parse_generic(preferences: Preferences, form: Dict[str, str], disabled_engines: List[str]) -> List[EngineRef]:
|
2020-09-22 18:03:42 +02:00
|
|
|
query_engineref_list = []
|
|
|
|
query_categories = []
|
|
|
|
|
|
|
|
# set categories/engines
|
2020-10-06 15:23:19 +02:00
|
|
|
explicit_engine_list = False
|
2020-11-03 10:55:49 +01:00
|
|
|
if not is_locked('categories'):
|
|
|
|
# parse the form only if the categories are not locked
|
|
|
|
for pd_name, pd in form.items():
|
|
|
|
if pd_name == 'engines':
|
2021-12-27 09:26:22 +01:00
|
|
|
pd_engines = [
|
|
|
|
EngineRef(engine_name, engines[engine_name].categories[0])
|
|
|
|
for engine_name in map(str.strip, pd.split(','))
|
|
|
|
if engine_name in engines
|
|
|
|
]
|
2020-11-03 10:55:49 +01:00
|
|
|
if pd_engines:
|
|
|
|
query_engineref_list.extend(pd_engines)
|
|
|
|
explicit_engine_list = True
|
|
|
|
else:
|
|
|
|
parse_category_form(query_categories, pd_name, pd)
|
2020-09-22 18:03:42 +02:00
|
|
|
|
2020-10-06 15:23:19 +02:00
|
|
|
if explicit_engine_list:
|
|
|
|
# explicit list of engines with the "engines" parameter in the form
|
|
|
|
if query_categories:
|
|
|
|
# add engines from referenced by the "categories" parameter and the "category_*"" parameters
|
|
|
|
query_engineref_list.extend(get_engineref_from_category_list(query_categories, disabled_engines))
|
2020-09-22 18:03:42 +02:00
|
|
|
else:
|
2020-10-06 15:23:19 +02:00
|
|
|
# no "engines" parameters in the form
|
2020-09-22 18:03:42 +02:00
|
|
|
if not query_categories:
|
2020-10-06 15:23:19 +02:00
|
|
|
# and neither "categories" parameter nor "category_*"" parameters in the form
|
|
|
|
# -> get the categories from the preferences (the cookies or the settings)
|
2020-09-24 16:26:00 +02:00
|
|
|
query_categories = get_selected_categories(preferences, None)
|
2020-09-22 18:03:42 +02:00
|
|
|
|
|
|
|
# using all engines for that search, which are
|
|
|
|
# declared under the specific categories
|
2020-10-06 15:23:19 +02:00
|
|
|
query_engineref_list.extend(get_engineref_from_category_list(query_categories, disabled_engines))
|
2020-09-22 18:03:42 +02:00
|
|
|
|
2020-12-17 13:51:57 +01:00
|
|
|
return query_engineref_list
|
2020-09-22 18:03:42 +02:00
|
|
|
|
|
|
|
|
2021-03-02 14:24:55 +01:00
|
|
|
def parse_engine_data(form):
|
|
|
|
engine_data = defaultdict(dict)
|
|
|
|
for k, v in form.items():
|
|
|
|
if k.startswith("engine_data"):
|
|
|
|
_, engine, key = k.split('-')
|
|
|
|
engine_data[engine][key] = v
|
|
|
|
return engine_data
|
|
|
|
|
|
|
|
|
2021-12-27 09:26:22 +01:00
|
|
|
def get_search_query_from_webapp(
|
|
|
|
preferences: Preferences, form: Dict[str, str]
|
|
|
|
) -> Tuple[SearchQuery, RawTextQuery, List[EngineRef], List[EngineRef]]:
|
2020-09-22 17:45:32 +02:00
|
|
|
# no text for the query ?
|
|
|
|
if not form.get('q'):
|
|
|
|
raise SearxParameterException('q', '')
|
|
|
|
|
|
|
|
# set blocked engines
|
|
|
|
disabled_engines = preferences.engines.get_disabled()
|
|
|
|
|
|
|
|
# parse query, if tags are set, which change
|
2022-09-27 17:01:00 +02:00
|
|
|
# the search engine or search-language
|
2020-09-22 17:45:32 +02:00
|
|
|
raw_text_query = RawTextQuery(form['q'], disabled_engines)
|
|
|
|
|
|
|
|
# set query
|
|
|
|
query = raw_text_query.getQuery()
|
|
|
|
query_pageno = parse_pageno(form)
|
2020-09-24 16:26:00 +02:00
|
|
|
query_lang = parse_lang(preferences, form, raw_text_query)
|
|
|
|
query_safesearch = parse_safesearch(preferences, form)
|
2020-09-22 17:45:32 +02:00
|
|
|
query_time_range = parse_time_range(form)
|
2020-09-24 16:26:00 +02:00
|
|
|
query_timeout = parse_timeout(form, raw_text_query)
|
2020-09-22 17:45:32 +02:00
|
|
|
external_bang = raw_text_query.external_bang
|
2021-03-02 14:24:55 +01:00
|
|
|
engine_data = parse_engine_data(form)
|
2020-09-22 17:45:32 +02:00
|
|
|
|
2022-01-03 20:23:46 +01:00
|
|
|
if not is_locked('categories') and raw_text_query.specific:
|
2020-09-22 18:03:42 +02:00
|
|
|
# if engines are calculated from query,
|
2022-09-27 17:01:00 +02:00
|
|
|
# set categories by using that information
|
2020-12-18 12:19:14 +01:00
|
|
|
query_engineref_list = raw_text_query.enginerefs
|
2020-09-22 13:59:27 +02:00
|
|
|
else:
|
2020-09-22 18:03:42 +02:00
|
|
|
# otherwise, using defined categories to
|
|
|
|
# calculate which engines should be used
|
2020-12-17 13:51:57 +01:00
|
|
|
query_engineref_list = parse_generic(preferences, form, disabled_engines)
|
2020-09-22 13:59:27 +02:00
|
|
|
|
2020-09-22 16:22:22 +02:00
|
|
|
query_engineref_list = deduplicate_engineref_list(query_engineref_list)
|
2021-12-27 09:26:22 +01:00
|
|
|
query_engineref_list, query_engineref_list_unknown, query_engineref_list_notoken = validate_engineref_list(
|
|
|
|
query_engineref_list, preferences
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
SearchQuery(
|
|
|
|
query,
|
|
|
|
query_engineref_list,
|
|
|
|
query_lang,
|
|
|
|
query_safesearch,
|
|
|
|
query_pageno,
|
|
|
|
query_time_range,
|
|
|
|
query_timeout,
|
|
|
|
external_bang=external_bang,
|
|
|
|
engine_data=engine_data,
|
|
|
|
),
|
|
|
|
raw_text_query,
|
|
|
|
query_engineref_list_unknown,
|
|
|
|
query_engineref_list_notoken,
|
|
|
|
)
|