Esempio n. 1
0
def autocompleter():
    """Return autocompleter results"""

    # set blocked engines
    disabled_engines = request.preferences.engines.get_disabled()

    # parse query
    if PY3:
        raw_text_query = RawTextQuery(request.form.get('q', b''),
                                      disabled_engines)
    else:
        raw_text_query = RawTextQuery(
            request.form.get('q', u'').encode('utf-8'), disabled_engines)
    raw_text_query.parse_query()

    # check if search query is set
    if not raw_text_query.getSearchQuery():
        return '', 400

    # run autocompleter
    completer = autocomplete_backends.get(
        request.preferences.get_value('autocomplete'))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(raw_text_query)

    # normal autocompletion results only appear if no inner results returned
    # and there is a query part besides the engine and language bangs
    if len(raw_results) == 0 and completer and (
            len(raw_text_query.query_parts) > 1 or
        (len(raw_text_query.languages) == 0 and not raw_text_query.specific)):
        # get language from cookie
        language = request.preferences.get_value('language')
        if not language or language == 'all':
            language = 'en'
        else:
            language = language.split('-')[0]
        # run autocompletion
        raw_results.extend(completer(raw_text_query.getSearchQuery(),
                                     language))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        raw_text_query.changeSearchQuery(result)

        # add parsed result
        results.append(raw_text_query.getFullQuery())

    # return autocompleter results
    if request.form.get('format') == 'x-suggestions':
        return Response(json.dumps([raw_text_query.query, results]),
                        mimetype='application/json')

    return Response(json.dumps(results), mimetype='application/json')
Esempio n. 2
0
def autocompleter():
    """Return autocompleter results"""
    request_data = {}

    # select request method
    if request.method == 'POST':
        request_data = request.form
    else:
        request_data = request.args

    # set blocked engines
    blocked_engines = get_blocked_engines(engines, request.cookies)

    # parse query
    query = Query(request_data.get('q', '').encode('utf-8'), blocked_engines)
    query.parse_query()

    # check if search query is set
    if not query.getSearchQuery():
        return '', 400

    # get autocompleter
    completer = autocomplete_backends.get(
        request.cookies.get('autocomplete',
                            settings['search']['autocomplete']))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(query)

    # normal autocompletion results only appear if max 3 inner results returned
    if len(raw_results) <= 3 and completer:
        # get language from cookie
        language = request.cookies.get('language')
        if not language or language == 'all':
            language = 'en'
        else:
            language = language.split('_')[0]
        # run autocompletion
        raw_results.extend(completer(query.getSearchQuery(), language))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        query.changeSearchQuery(result)

        # add parsed result
        results.append(query.getFullQuery())

    # return autocompleter results
    if request_data.get('format') == 'x-suggestions':
        return Response(json.dumps([query.query, results]),
                        mimetype='application/json')

    return Response(json.dumps(results), mimetype='application/json')
Esempio n. 3
0
def autocompleter():
    """Return autocompleter results"""
    request_data = {}

    # select request method
    if request.method == 'POST':
        request_data = request.form
    else:
        request_data = request.args

    # set blocked engines
    disabled_engines = request.preferences.engines.get_disabled()

    # parse query
    query = Query(request_data.get('q', '').encode('utf-8'), disabled_engines)
    query.parse_query()

    # check if search query is set
    if not query.getSearchQuery():
        return '', 400

    # run autocompleter
    completer = autocomplete_backends.get(request.preferences.get_value('autocomplete'))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(query)

    # normal autocompletion results only appear if max 3 inner results returned
    if len(raw_results) <= 3 and completer:
        # get language from cookie
        language = request.preferences.get_value('language')
        if not language or language == 'all':
            language = 'en'
        else:
            language = language.split('_')[0]
        # run autocompletion
        raw_results.extend(completer(query.getSearchQuery(), language))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        query.changeSearchQuery(result)

        # add parsed result
        results.append(query.getFullQuery())

    # return autocompleter results
    if request_data.get('format') == 'x-suggestions':
        return Response(json.dumps([query.query, results]),
                        mimetype='application/json')

    return Response(json.dumps(results),
                    mimetype='application/json')
Esempio n. 4
0
def autocompleter():
    """Return autocompleter results"""

    # set blocked engines
    disabled_engines = request.preferences.engines.get_disabled()

    # parse query
    raw_text_query = RawTextQuery(request.form.get("q", ""), disabled_engines)

    # check if search query is set
    if not raw_text_query.getQuery():
        return "", 400

    # run autocompleter
    completer = autocomplete_backends.get(
        request.preferences.get_value("autocomplete"))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(raw_text_query)

    # normal autocompletion results only appear if no inner results returned
    # and there is a query part besides the engine and language bangs
    if (len(raw_results) == 0 and completer
            and (len(raw_text_query.query_parts) > 1 or
                 (len(raw_text_query.languages) == 0
                  and not raw_text_query.specific))):
        # get language from cookie
        language = request.preferences.get_value("language")
        if not language or language == "all":
            language = "en"
        else:
            language = language.split("-")[0]
        # run autocompletion
        raw_results.extend(completer(raw_text_query.getQuery(), language))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        raw_text_query.changeQuery(result)

        # add parsed result
        results.append(raw_text_query.getFullQuery())

    # return autocompleter results
    if request.headers.get("X-Requested-With") == "XMLHttpRequest":
        return Response(json.dumps(results), mimetype="application/json")

    return Response(
        json.dumps([raw_text_query.query, results]),
        mimetype="application/x-suggestions+json",
    )
Esempio n. 5
0
def autocompleter():
    """Return autocompleter results"""

    # set blocked engines
    disabled_engines = request.preferences.engines.get_disabled()

    # parse query
    if PY3:
        raw_text_query = RawTextQuery(request.form.get('q', b''), disabled_engines)
    else:
        raw_text_query = RawTextQuery(request.form.get('q', u'').encode('utf-8'), disabled_engines)
    raw_text_query.parse_query()

    # check if search query is set
    if not raw_text_query.getSearchQuery():
        return '', 400

    # run autocompleter
    completer = autocomplete_backends.get(request.preferences.get_value('autocomplete'))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(raw_text_query)

    # normal autocompletion results only appear if max 3 inner results returned
    if len(raw_results) <= 3 and completer:
        # get language from cookie
        language = request.preferences.get_value('language')
        if not language:
            language = settings['search']['language']
        else:
            language = language.split('-')[0]
        # run autocompletion
        raw_results.extend(completer(raw_text_query.getSearchQuery(), language))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        raw_text_query.changeSearchQuery(result)

        # add parsed result
        results.append(raw_text_query.getFullQuery())

    # return autocompleter results
    if request.form.get('format') == 'x-suggestions':
        return Response(json.dumps([raw_text_query.query, results]),
                        mimetype='application/json')

    return Response(json.dumps(results),
                    mimetype='application/json')
Esempio n. 6
0
def autocompleter():
    """Return autocompleter results"""
    request_data = {}

    # select request method
    if request.method == "POST":
        request_data = request.form
    else:
        request_data = request.args

    # set blocked engines
    blocked_engines = get_blocked_engines(engines, request.cookies)

    # parse query
    query = Query(request_data.get("q", "").encode("utf-8"), blocked_engines)
    query.parse_query()

    # check if search query is set
    if not query.getSearchQuery():
        return "", 400

    # run autocompleter
    completer = autocomplete_backends.get(request.cookies.get("autocomplete", settings["search"]["autocomplete"]))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(query)

    # normal autocompletion results only appear if max 3 inner results returned
    if len(raw_results) <= 3 and completer:
        # run autocompletion
        raw_results.extend(completer(query.getSearchQuery()))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        query.changeSearchQuery(result)

        # add parsed result
        results.append(query.getFullQuery())

    # return autocompleter results
    if request_data.get("format") == "x-suggestions":
        return Response(json.dumps([query.query, results]), mimetype="application/json")

    return Response(json.dumps(results), mimetype="application/json")
Esempio n. 7
0
File: webapp.py Progetto: kvch/searx
def autocompleter():
    """Return autocompleter results"""

    # set blocked engines
    disabled_engines = request.preferences.engines.get_disabled()

    # parse query
    raw_text_query = RawTextQuery(request.form.get("q", "").encode("utf-8"), disabled_engines)
    raw_text_query.parse_query()

    # check if search query is set
    if not raw_text_query.getSearchQuery():
        return "", 400

    # run autocompleter
    completer = autocomplete_backends.get(request.preferences.get_value("autocomplete"))

    # parse searx specific autocompleter results like !bang
    raw_results = searx_bang(raw_text_query)

    # normal autocompletion results only appear if max 3 inner results returned
    if len(raw_results) <= 3 and completer:
        # get language from cookie
        language = request.preferences.get_value("language")
        if not language or language == "all":
            language = "en"
        else:
            language = language.split("_")[0]
        # run autocompletion
        raw_results.extend(completer(raw_text_query.getSearchQuery(), language))

    # parse results (write :language and !engine back to result string)
    results = []
    for result in raw_results:
        raw_text_query.changeSearchQuery(result)

        # add parsed result
        results.append(raw_text_query.getFullQuery())

    # return autocompleter results
    if request.form.get("format") == "x-suggestions":
        return Response(json.dumps([raw_text_query.query, results]), mimetype="application/json")

    return Response(json.dumps(results), mimetype="application/json")