summaryrefslogtreecommitdiff
path: root/searx/search/models.py
blob: bbca1cd1d2ae8a9f23d1b545337b2506df02aea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# SPDX-License-Identifier: AGPL-3.0-or-later

import typing
import babel


class EngineRef:
    """Reference by names to an engine and category"""

    __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...)"""

    __slots__ = (
        'query',
        'engineref_list',
        'lang',
        'locale',
        '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,
    ):
        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
        self.engine_data = engine_data or {}

        self.locale = None
        if self.lang:
            try:
                self.locale = babel.Locale.parse(self.lang, sep='-')
            except babel.core.UnknownLocaleError:
                pass

    @property
    def categories(self):
        return list(set(map(lambda engineref: engineref.category, self.engineref_list)))

    def __repr__(self):
        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,
        )

    def __eq__(self, other):
        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
            and self.external_bang == other.external_bang
        )

    def __hash__(self):
        return hash(
            (
                self.query,
                tuple(self.engineref_list),
                self.lang,
                self.safesearch,
                self.pageno,
                self.time_range,
                self.timeout_limit,
                self.external_bang,
            )
        )