Beispiel #1
0
 def test_eq(self):
     s = SearchQuery('test', [EngineRef('bing', 'general', False)],
                     ['general'], 'all', 0, 1, None, None, None)
     t = SearchQuery('test', [EngineRef('google', 'general', False)],
                     ['general'], 'all', 0, 1, None, None, None)
     self.assertEqual(s, s)
     self.assertNotEqual(s, t)
def get_engineref_from_category_list(category_list: List[str], disabled_engines: List[str]) -> List[EngineRef]:
    result = []
    for categ in category_list:
        result.extend(EngineRef(engine.name, categ)
                      for engine in categories[categ]
                      if (engine.name, categ) not in disabled_engines)
    return result
Beispiel #3
0
def parse_generic(preferences: Preferences, form: Dict[str, str], disabled_engines: List[str]) -> List[EngineRef]:
    query_engineref_list = []
    query_categories = []

    # set categories/engines
    explicit_engine_list = False
    if not is_locked('categories'):
        # parse the form only if the categories are not locked
        for pd_name, pd in form.items():
            if pd_name == 'engines':
                pd_engines = [EngineRef(engine_name, engines[engine_name].categories[0])
                              for engine_name in map(str.strip, pd.split(',')) if engine_name in engines]
                if pd_engines:
                    query_engineref_list.extend(pd_engines)
                    explicit_engine_list = True
            else:
                parse_category_form(query_categories, pd_name, pd)

    if explicit_engine_list:
        # explicit list of engines with the "engines" parameter in the form
        if query_categories:
            # add engines from referenced by the "categories" parameter and the "category_*"" parameters
            query_engineref_list.extend(get_engineref_from_category_list(query_categories, disabled_engines))
    else:
        # no "engines" parameters in the form
        if not query_categories:
            # and neither "categories" parameter nor "category_*"" parameters in the form
            # -> get the categories from the preferences (the cookies or the settings)
            query_categories = get_selected_categories(preferences, None)

        # using all engines for that search, which are
        # declared under the specific categories
        query_engineref_list.extend(get_engineref_from_category_list(query_categories, disabled_engines))

    return query_engineref_list
Beispiel #4
0
 def test_timeout_query_above_max(self):
     settings['outgoing']['max_request_timeout'] = 10.0
     search_query = SearchQuery('test', [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
                                'en-US', SAFESEARCH, PAGENO, None, 15.0)
     search = searx.search.Search(search_query)
     search.search()
     self.assertEqual(search.actual_timeout, 10.0)
Beispiel #5
0
 def test_repr(self):
     s = SearchQuery('test', [EngineRef('bing', 'general', False)],
                     ['general'], 'all', 0, 1, '1', 5.0, 'g')
     self.assertEqual(repr(
         s
     ), "SearchQuery('test', [EngineRef('bing', 'general', False)], ['general'], 'all', 0, 1, '1', 5.0, 'g')"
                      )  # noqa
Beispiel #6
0
 def test_get_search_query(self):
     """test get_search_query."""
     args = sas.parse_argument(['rain', ])
     search_q = sas.get_search_query(args)
     self.assertTrue(search_q)
     self.assertEqual(search_q, SearchQuery('rain', [EngineRef('engine1', 'general')],
                      'all', 0, 1, None, None, None))
Beispiel #7
0
 def test_timeout_query_below_max(self):
     searx.search.max_request_timeout = 10.0
     search_query = SearchQuery('test',
                                [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
                                'en-US', SAFESEARCH, PAGENO, None, 5.0)
     search = searx.search.Search(search_query)
     search.search()
     self.assertEqual(search.actual_timeout, 5.0)
Beispiel #8
0
 def test_timeout_simple(self):
     searx.search.max_request_timeout = None
     search_query = SearchQuery('test',
                                [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
                                ['general'], 'en-US', SAFESEARCH, PAGENO,
                                None, None)
     search = searx.search.Search(search_query)
     search.search()
     self.assertEqual(search.actual_timeout, 3.0)
Beispiel #9
0
    def test_external_bang(self):
        search_query = SearchQuery('yes yes',
                                   [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
                                   'en-US', SAFESEARCH, PAGENO, None, None,
                                   external_bang="yt")
        search = searx.search.Search(search_query)
        results = search.search()
        # For checking if the user redirected with the youtube external bang
        self.assertTrue(results.redirect_url is not None)

        search_query = SearchQuery('youtube never gonna give you up',
                                   [EngineRef(PUBLIC_ENGINE_NAME, 'general')],
                                   'en-US', SAFESEARCH, PAGENO, None, None)

        search = searx.search.Search(search_query)
        results = search.search()
        # This should not redirect
        self.assertTrue(results.redirect_url is None)
Beispiel #10
0
    def _parse(self, value):
        # check if prefix is equal with engine shortcut
        if value in engine_shortcuts:
            value = engine_shortcuts[value]

        # check if prefix is equal with engine name
        if value in engines:
            self.raw_text_query.enginerefs.append(EngineRef(value, 'none'))
            return True

        # check if prefix is equal with categorie name
        if value in categories:
            # using all engines for that search, which
            # are declared under that categorie name
            self.raw_text_query.enginerefs.extend(EngineRef(engine.name, value)
                                                  for engine in categories[value]
                                                  if (engine.name, value) not in self.raw_text_query.disabled_engines)
            return True

        return False
Beispiel #11
0
 def test_get_search_query(self):
     """test get_search_query."""
     sas = get_standalone_searx_module()
     args = sas.parse_argument([
         'rain',
     ])
     search_q = sas.get_search_query(args)
     self.assertTrue(search_q)
     self.assertEqual(
         search_q,
         SearchQuery('rain', [EngineRef('engine1', 'general', False)],
                     ['general'], 'all', 0, 1, None, None, None))
Beispiel #12
0
def parse_generic(preferences: Preferences, form: Dict[str, str], disabled_engines: List[str])\
        -> Tuple[List[EngineRef], List[str]]:
    query_engineref_list = []
    query_categories = []

    # set categories/engines
    load_default_categories = True
    for pd_name, pd in form.items():
        if pd_name == 'engines':
            pd_engines = [
                EngineRef(engine_name, engines[engine_name].categories[0])
                for engine_name in map(str.strip, pd.split(','))
                if engine_name in engines
            ]
            if pd_engines:
                query_engineref_list.extend(pd_engines)
                load_default_categories = False
        else:
            parse_category_form(query_categories, pd_name, pd)

    if not load_default_categories:
        if not query_categories:
            query_categories = list(
                set(engine['category'] for engine in query_engineref_list))
    else:
        if not query_categories:
            query_categories = get_selected_categories(preferences, None)

        # using all engines for that search, which are
        # declared under the specific categories
        for categ in query_categories:
            query_engineref_list.extend(
                EngineRef(engine.name, categ) for engine in categories[categ]
                if (engine.name, categ) not in disabled_engines)

    return query_engineref_list, query_categories
Beispiel #13
0
    def _parse_query(self):
        self.query_parts = []

        # split query, including whitespaces
        raw_query_parts = re.split(r'(\s+)', self.query)

        parse_next = True

        for query_part in raw_query_parts:
            if not parse_next:
                self.query_parts[-1] += query_part
                continue

            parse_next = False

            # part does only contain spaces, skip
            if query_part.isspace()\
               or query_part == '':
                parse_next = True
                self.query_parts.append(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
                    parse_next = 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:
                        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
                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

            # external bang
            if query_part[0:2] == "!!":
                self.external_bang = query_part[2:]
                parse_next = 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:
                    parse_next = 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:
                    parse_next = 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
                    parse_next = 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
            self.query_parts.append(query_part)
Beispiel #14
0
from searx.search import EngineRef
from searx.webadapter import validate_engineref_list

PRIVATE_ENGINE_NAME = 'general private offline'
TEST_ENGINES = [
    {
        'name': PRIVATE_ENGINE_NAME,
        'engine': 'dummy-offline',
        'categories': 'general',
        'shortcut': 'do',
        'timeout': 3.0,
        'engine_type': 'offline',
        'tokens': ['my-token'],
    },
]
SEARCHQUERY = [EngineRef(PRIVATE_ENGINE_NAME, 'general')]


class ValidateQueryCase(SearxTestCase):
    @classmethod
    def setUpClass(cls):
        searx.search.initialize(TEST_ENGINES)

    def test_query_private_engine_without_token(self):
        preferences = Preferences(['oscar'], ['general'], engines, [])
        valid, unknown, invalid_token = validate_engineref_list(
            SEARCHQUERY, preferences)
        self.assertEqual(len(valid), 0)
        self.assertEqual(len(unknown), 0)
        self.assertEqual(len(invalid_token), 1)
Beispiel #15
0
 def test_no_external_bang_query(self):
     result = get_bang_url(
         SearchQuery('test',
                     engineref_list=[EngineRef('wikipedia', 'general')]))
     self.assertEqual(result, None)