2021-01-05 10:24:39 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
|
|
|
|
import typing
|
2022-08-01 15:01:59 +00:00
|
|
|
import babel
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EngineRef:
|
2021-09-09 09:23:57 +00:00
|
|
|
"""Reference by names to an engine and category"""
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
__slots__ = 'name', 'category'
|
|
|
|
|
|
|
|
def __init__(self, name: str, category: str):
|
|
|
|
self.name = name
|
|
|
|
self.category = category
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "EngineRef({!r}, {!r})".format(self.name, self.category)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.name == other.name and self.category == other.category
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name, self.category))
|
|
|
|
|
|
|
|
|
|
|
|
class SearchQuery:
|
|
|
|
"""container for all the search parameters (query, language, etc...)"""
|
|
|
|
|
2021-12-27 08:26:22 +00:00
|
|
|
__slots__ = (
|
|
|
|
'query',
|
|
|
|
'engineref_list',
|
|
|
|
'lang',
|
2022-08-01 15:01:59 +00:00
|
|
|
'locale',
|
2021-12-27 08:26:22 +00:00
|
|
|
'safesearch',
|
|
|
|
'pageno',
|
|
|
|
'time_range',
|
|
|
|
'timeout_limit',
|
|
|
|
'external_bang',
|
|
|
|
'engine_data',
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
engineref_list: typing.List[EngineRef],
|
|
|
|
lang: str = 'all',
|
|
|
|
safesearch: int = 0,
|
|
|
|
pageno: int = 1,
|
|
|
|
time_range: typing.Optional[str] = None,
|
|
|
|
timeout_limit: typing.Optional[float] = None,
|
|
|
|
external_bang: typing.Optional[str] = None,
|
|
|
|
engine_data: typing.Optional[typing.Dict[str, str]] = None,
|
|
|
|
):
|
2021-01-05 10:24:39 +00:00
|
|
|
self.query = query
|
|
|
|
self.engineref_list = engineref_list
|
|
|
|
self.lang = lang
|
|
|
|
self.safesearch = safesearch
|
|
|
|
self.pageno = pageno
|
|
|
|
self.time_range = time_range
|
|
|
|
self.timeout_limit = timeout_limit
|
|
|
|
self.external_bang = external_bang
|
2021-03-25 08:37:37 +00:00
|
|
|
self.engine_data = engine_data or {}
|
2021-01-05 10:24:39 +00:00
|
|
|
|
2022-08-01 15:01:59 +00:00
|
|
|
self.locale = None
|
|
|
|
if self.lang:
|
|
|
|
try:
|
|
|
|
self.locale = babel.Locale.parse(self.lang, sep='-')
|
|
|
|
except babel.core.UnknownLocaleError:
|
|
|
|
pass
|
|
|
|
|
2021-01-05 10:24:39 +00:00
|
|
|
@property
|
|
|
|
def categories(self):
|
|
|
|
return list(set(map(lambda engineref: engineref.category, self.engineref_list)))
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-12-27 08:26:22 +00:00
|
|
|
return "SearchQuery({!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r}, {!r})".format(
|
|
|
|
self.query,
|
|
|
|
self.engineref_list,
|
|
|
|
self.lang,
|
|
|
|
self.safesearch,
|
|
|
|
self.pageno,
|
|
|
|
self.time_range,
|
|
|
|
self.timeout_limit,
|
|
|
|
self.external_bang,
|
|
|
|
)
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
2021-12-27 08:26:22 +00:00
|
|
|
return (
|
|
|
|
self.query == other.query
|
|
|
|
and self.engineref_list == other.engineref_list
|
|
|
|
and self.lang == other.lang
|
|
|
|
and self.safesearch == other.safesearch
|
|
|
|
and self.pageno == other.pageno
|
|
|
|
and self.time_range == other.time_range
|
|
|
|
and self.timeout_limit == other.timeout_limit
|
2021-01-05 10:24:39 +00:00
|
|
|
and self.external_bang == other.external_bang
|
2021-12-27 08:26:22 +00:00
|
|
|
)
|
2021-01-05 10:24:39 +00:00
|
|
|
|
|
|
|
def __hash__(self):
|
2021-12-27 08:26:22 +00:00
|
|
|
return hash(
|
|
|
|
(
|
|
|
|
self.query,
|
|
|
|
tuple(self.engineref_list),
|
|
|
|
self.lang,
|
|
|
|
self.safesearch,
|
|
|
|
self.pageno,
|
|
|
|
self.time_range,
|
|
|
|
self.timeout_limit,
|
|
|
|
self.external_bang,
|
|
|
|
)
|
|
|
|
)
|