def _parse(self, value): found = False # check if any language-code is equal with # declared language-codes for lc in language_codes: lang_id, lang_name, country, english_name = map(str.lower, lc) # if correct language-code is found # set it as new search-language if (value == lang_id or value == lang_name or value == english_name or value.replace('-', ' ') == country)\ and value not in self.raw_text_query.languages: found = True lang_parts = lang_id.split('-') if len(lang_parts) == 2: self.raw_text_query.languages.append(lang_parts[0] + '-' + lang_parts[1].upper()) else: self.raw_text_query.languages.append(lang_id) # to ensure best match (first match is not necessarily the best one) if value == lang_id: break # user may set a valid, yet not selectable language if VALID_LANGUAGE_CODE.match(value): lang_parts = value.split('-') if len(lang_parts) > 1: value = lang_parts[0].lower() + '-' + lang_parts[1].upper() if value not in self.raw_text_query.languages: self.raw_text_query.languages.append(value) found = True return found
def parse_lang(preferences: Preferences, form: Dict[str, str], raw_text_query: RawTextQuery) -> str: # get language # set specific language if set on request, query or preferences # TODO support search with multible languages 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) return query_lang
def _validate_selection(self, selection): if selection != '' and not VALID_LANGUAGE_CODE.match(selection): raise ValidationException( 'Invalid language code: "{0}"'.format(selection))
def _parse_query(self): self.query_parts = [] # split query, including whitespaces raw_query_parts = re.split(r'(\s+)', self.query) for query_part in raw_query_parts: searx_query_part = False # part does only contain spaces, skip if query_part.isspace()\ or query_part == '': continue # this force the timeout if query_part[0] == '<': try: raw_timeout_limit = int(query_part[1:]) if raw_timeout_limit < 100: # below 100, the unit is the second ( <3 = 3 seconds timeout ) self.timeout_limit = float(raw_timeout_limit) else: # 100 or above, the unit is the millisecond ( <850 = 850 milliseconds timeout ) self.timeout_limit = raw_timeout_limit / 1000.0 searx_query_part = True except ValueError: # error not reported to the user pass # this force a language if query_part[0] == ':': lang = query_part[1:].lower().replace('_', '-') # check if any language-code is equal with # declared language-codes for lc in language_codes: lang_id, lang_name, country, english_name = map(str.lower, lc) # if correct language-code is found # set it as new search-language 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) # 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 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 # external bang if query_part[0:2] == "!!": self.external_bang = query_part[2:] searx_query_part = True continue # this force a engine or category if query_part[0] == '!' or query_part[0] == '?': prefix = query_part[1:].replace('-', ' ').replace('_', ' ') # check if prefix is equal with engine shortcut 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', True)) # check if prefix is equal with engine name elif prefix in engines: searx_query_part = True self.enginerefs.append(EngineRef(prefix, 'none', True)) # check if prefix is equal with categorie name elif prefix in categories: # using all engines for that search, which # are declared under that categorie name 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 # append query part to query_part list if searx_query_part: self.query_parts.append(query_part) else: self.user_query_parts.append(query_part)