query.py 6.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python

'''
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) 2014 by Thomas Pointhuber, <thomas.pointhuber@gmx.at>
'''

from searx.languages import language_codes
from searx.engines import (
    categories, engines, engine_shortcuts
)
import re
Adam Tauber's avatar
Adam Tauber committed
25
26
import sys

27

28
VALID_LANGUAGE_CODE = re.compile(r'^[a-z]{2,3}(-[a-zA-Z]{2})?$')
29

30

dalf's avatar
dalf committed
31
32
class RawTextQuery(object):
    """parse raw text query (the value from the html input)"""
33

34
    def __init__(self, query, disabled_engines):
35
        self.query = query
36
        self.disabled_engines = []
Adam Tauber's avatar
Adam Tauber committed
37

38
39
        if disabled_engines:
            self.disabled_engines = disabled_engines
Adam Tauber's avatar
Adam Tauber committed
40

41
42
43
        self.query_parts = []
        self.engines = []
        self.languages = []
Cqoicebordel's avatar
Cqoicebordel committed
44
        self.specific = False
Adam Tauber's avatar
Adam Tauber committed
45
46
47

    # parse query, if tags are set, which
    # change the serch engine or search-language
48
49
    def parse_query(self):
        self.query_parts = []
Adam Tauber's avatar
Adam Tauber committed
50

51
        # split query, including whitespaces
52
        raw_query_parts = re.split(r'(\s+)' if isinstance(self.query, str) else r'(\s+)'.encode(), self.query)
Adam Tauber's avatar
Adam Tauber committed
53

54
        parse_next = True
Adam Tauber's avatar
Adam Tauber committed
55

56
57
58
59
        for query_part in raw_query_parts:
            if not parse_next:
                self.query_parts[-1] += query_part
                continue
Adam Tauber's avatar
Adam Tauber committed
60

61
            parse_next = False
Adam Tauber's avatar
Adam Tauber committed
62

63
            # part does only contain spaces, skip
64
65
            if query_part.isspace()\
               or query_part == '':
66
67
68
69
                parse_next = True
                self.query_parts.append(query_part)
                continue

Adam Tauber's avatar
Adam Tauber committed
70
            # this force a language
71
            if query_part[0] == ':':
72
                lang = query_part[1:].lower().replace('_', '-')
73

Adam Tauber's avatar
Adam Tauber committed
74
75
                # check if any language-code is equal with
                # declared language-codes
76
                for lc in language_codes:
Nicolas Gelot's avatar
Nicolas Gelot committed
77
                    lang_id, lang_name, country, english_name = map(str.lower, lc)
78

Adam Tauber's avatar
Adam Tauber committed
79
80
                    # if correct language-code is found
                    # set it as new search-language
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
                    if (lang == lang_id
                        or lang == lang_name
                        or lang == english_name
                        or lang.replace('-', ' ') == country)\
                       and lang not in self.languages:
                            parse_next = True
                            lang_parts = lang_id.split('-')
                            if len(lang_parts) == 2:
                                self.languages.append(lang_parts[0] + '-' + lang_parts[1].upper())
                            else:
                                self.languages.append(lang_id)
                            # to ensure best match (first match is not necessarily the best one)
                            if lang == lang_id:
                                break

                # user may set a valid, yet not selectable language
97
98
99
100
101
102
103
                if VALID_LANGUAGE_CODE.match(lang):
                    lang_parts = lang.split('-')
                    if len(lang_parts) > 1:
                        lang = lang_parts[0].lower() + '-' + lang_parts[1].upper()
                    if lang not in self.languages:
                        self.languages.append(lang)
                        parse_next = True
104
105

            # this force a engine or category
Cqoicebordel's avatar
Cqoicebordel committed
106
            if query_part[0] == '!' or query_part[0] == '?':
107
                prefix = query_part[1:].replace('-', ' ').replace('_', ' ')
108
109

                # check if prefix is equal with engine shortcut
110
                if prefix in engine_shortcuts:
111
                    parse_next = True
112
113
114
115
116
117
                    engine_name = engine_shortcuts[prefix]
                    if engine_name in engines:
                        for engine_category in engines[engine_name].categories:
                            self.engines.append({'category': engine_category,
                                                 'name': engine_name,
                                                 'from_bang': True})
Adam Tauber's avatar
Adam Tauber committed
118

119
                # check if prefix is equal with engine name
120
                elif prefix in engines:
121
                    parse_next = True
122
                    if prefix in engines:
123
                        for engine_category in engines[prefix].categories:
124
                            self.engines.append({'category': engine_category,
125
                                                 'name': prefix,
126
                                                 'from_bang': True})
127
128
129

                # check if prefix is equal with categorie name
                elif prefix in categories:
Adam Tauber's avatar
Adam Tauber committed
130
131
                    # using all engines for that search, which
                    # are declared under that categorie name
132
133
                    parse_next = True
                    self.engines.extend({'category': prefix,
134
                                         'name': engine.name}
135
                                        for engine in categories[prefix]
136
                                        if (engine.name, prefix) not in self.disabled_engines)
Adam Tauber's avatar
Adam Tauber committed
137

Cqoicebordel's avatar
Cqoicebordel committed
138
139
140
            if query_part[0] == '!':
                self.specific = True

141
142
143
144
145
146
147
148
            # append query part to query_part list
            self.query_parts.append(query_part)

    def changeSearchQuery(self, search_query):
        if len(self.query_parts):
            self.query_parts[-1] = search_query
        else:
            self.query_parts.append(search_query)
Adam Tauber's avatar
Adam Tauber committed
149

150
151
152
153
154
    def getSearchQuery(self):
        if len(self.query_parts):
            return self.query_parts[-1]
        else:
            return ''
Adam Tauber's avatar
Adam Tauber committed
155

156
157
    def getFullQuery(self):
        # get full querry including whitespaces
Nicolas Gelot's avatar
Nicolas Gelot committed
158
        return ''.join(self.query_parts)
dalf's avatar
dalf committed
159
160
161
162
163
164


class SearchQuery(object):
    """container for all the search parameters (query, language, etc...)"""

    def __init__(self, query, engines, categories, lang, safesearch, pageno, time_range):
Johnny Kalajdzic's avatar
Johnny Kalajdzic committed
165
        self.query = query
dalf's avatar
dalf committed
166
167
168
169
170
171
172
173
174
        self.engines = engines
        self.categories = categories
        self.lang = lang
        self.safesearch = safesearch
        self.pageno = pageno
        self.time_range = time_range

    def __str__(self):
        return str(self.query) + ";" + str(self.engines)