123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- '''
- 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>
- '''
- import re
- from searx.languages import language_codes
- from searx.engines import categories, engines, engine_shortcuts
- from searx.search import EngineRef
- from searx.webutils import VALID_LANGUAGE_CODE
- class RawTextQuery:
- """parse raw text query (the value from the html input)"""
- def __init__(self, query, disabled_engines):
- assert isinstance(query, str)
- self.query = query
- self.disabled_engines = []
- if disabled_engines:
- self.disabled_engines = disabled_engines
- self.query_parts = []
- self.user_query_parts = []
- self.enginerefs = []
- self.languages = []
- self.timeout_limit = None
- self.external_bang = None
- self.specific = False
- self._parse_query()
-
-
- def _parse_query(self):
- self.query_parts = []
-
- raw_query_parts = re.split(r'(\s+)', self.query)
- for query_part in raw_query_parts:
- searx_query_part = False
-
- if query_part.isspace()\
- or query_part == '':
- continue
-
- if query_part[0] == '<':
- try:
- raw_timeout_limit = int(query_part[1:])
- if raw_timeout_limit < 100:
-
- self.timeout_limit = float(raw_timeout_limit)
- else:
-
- self.timeout_limit = raw_timeout_limit / 1000.0
- searx_query_part = True
- except ValueError:
-
- pass
-
- if query_part[0] == ':' and len(query_part) > 1:
- lang = query_part[1:].lower().replace('_', '-')
-
-
- for lc in language_codes:
- lang_id, lang_name, country, english_name = map(str.lower, lc)
-
-
- if (lang == lang_id
- or lang == lang_name
- or lang == english_name
- or lang.replace('-', ' ') == country)\
- and lang not in self.languages:
- searx_query_part = 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)
-
- if lang == lang_id:
- break
-
- 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)
- searx_query_part = True
-
- if query_part[0:2] == "!!":
- self.external_bang = query_part[2:]
- searx_query_part = True
- continue
-
- if query_part[0] == '!' or query_part[0] == '?':
- prefix = query_part[1:].replace('-', ' ').replace('_', ' ')
-
- if prefix in engine_shortcuts:
- searx_query_part = True
- engine_name = engine_shortcuts[prefix]
- if engine_name in engines:
- self.enginerefs.append(EngineRef(engine_name, 'none'))
-
- elif prefix in engines:
- searx_query_part = True
- self.enginerefs.append(EngineRef(prefix, 'none'))
-
- elif prefix in categories:
-
-
- searx_query_part = True
- self.enginerefs.extend(EngineRef(engine.name, prefix)
- for engine in categories[prefix]
- if (engine.name, prefix) not in self.disabled_engines)
- if query_part[0] == '!':
- self.specific = True
-
- if searx_query_part:
- self.query_parts.append(query_part)
- else:
- self.user_query_parts.append(query_part)
- def changeQuery(self, query):
- self.user_query_parts = query.strip().split()
- return self
- def getQuery(self):
- return ' '.join(self.user_query_parts)
- def getFullQuery(self):
-
- return '{0} {1}'.format(''.join(self.query_parts), self.getQuery()).strip()
|