searx/webapp.py
def index():
    """Render index page.

    Supported outputs: html, json, csv, rss.
    """

    # output_format
    output_format = request.form.get('format', 'html')
    if output_format not in ['html', 'csv', 'json', 'rss']:
        output_format = 'html'

    # check if there is query
    if request.form.get('q') is None:
        if output_format == 'html':
            return render(
                'index.html',
            )
        else:
            return index_error(output_format, 'No query'), 400

    # search
    search_query = None
    result_container = None
    try:
        search_query = get_search_query_from_webapp(request.preferences, request.form)
        # search = Search(search_query) #  without plugins
        search = SearchWithPlugins(search_query, request.user_plugins, request)
        result_container = search.search()
    except Exception as e:
        # log exception
        logger.exception('search error')

        # is it an invalid input parameter or something else ?
        if (issubclass(e.__class__, SearxParameterException)):
            return index_error(output_format, e.message), 400
        else:
            return index_error(output_format, gettext('search error')), 500

    # results
    results = result_container.get_ordered_results()
    number_of_results = result_container.results_number()
    if number_of_results < result_container.results_length():
        number_of_results = 0

    # UI
    advanced_search = request.form.get('advanced_search', None)

    # output
    for result in results:
        if output_format == 'html':
            if 'content' in result and result['content']:
                result['content'] = highlight_content(escape(result['content'][:1024]), search_query.query)
            result['title'] = highlight_content(escape(result['title'] or u''), search_query.query)
        else:
            if result.get('content'):
                result['content'] = html_to_text(result['content']).strip()
            # removing html content and whitespace duplications
            result['title'] = ' '.join(html_to_text(result['title']).strip().split())

        result['pretty_url'] = prettify_url(result['url'])

        # TODO, check if timezone is calculated right
        if 'publishedDate' in result:
            try:  # test if publishedDate >= 1900 (datetime module bug)
                result['pubdate'] = result['publishedDate'].strftime('%Y-%m-%d %H:%M:%S%z')
            except ValueError:
                result['publishedDate'] = None
            else:
                if result['publishedDate'].replace(tzinfo=None) >= datetime.now() - timedelta(days=1):
                    timedifference = datetime.now() - result['publishedDate'].replace(tzinfo=None)
                    minutes = int((timedifference.seconds / 60) % 60)
                    hours = int(timedifference.seconds / 60 / 60)
                    if hours == 0:
                        result['publishedDate'] = gettext(u'{minutes} minute(s) ago').format(minutes=minutes)
                    else:
                        result['publishedDate'] = gettext(u'{hours} hour(s), {minutes} minute(s) ago').format(hours=hours, minutes=minutes)  # noqa
                else:
                    result['publishedDate'] = format_date(result['publishedDate'])

    if output_format == 'json':
        return Response(json.dumps({'query': search_query.query.decode('utf-8'),
                                    'number_of_results': number_of_results,
                                    'results': results,
                                    'answers': list(result_container.answers),
                                    'corrections': list(result_container.corrections),
                                    'infoboxes': result_container.infoboxes,
                                    'suggestions': list(result_container.suggestions),
                                    'unresponsive_engines': list(result_container.unresponsive_engines)},
                                   default=lambda item: list(item) if isinstance(item, set) else item),
                        mimetype='application/json')
    elif output_format == 'csv':
        csv = UnicodeWriter(StringIO())
        keys = ('title', 'url', 'content', 'host', 'engine', 'score')
        csv.writerow(keys)
        for row in results:
            row['host'] = row['parsed_url'].netloc
            csv.writerow([row.get(key, '') for key in keys])
        csv.stream.seek(0)
        response = Response(csv.stream.read(), mimetype='application/csv')
        cont_disp = 'attachment;Filename=searx_-_{0}.csv'.format(search_query.query)
        response.headers.add('Content-Disposition', cont_disp)
        return response
    elif output_format == 'rss':
        response_rss = render(
            'opensearch_response_rss.xml',
            results=results,
            q=request.form['q'],
            number_of_results=number_of_results,
            base_url=get_base_url(),
            override_theme='__common__',
        )
        return Response(response_rss, mimetype='text/xml')

    return render(
        'results.html',
        results=results,
        q=request.form['q'],
        selected_categories=search_query.categories,
        pageno=search_query.pageno,
        time_range=search_query.time_range,
        number_of_results=format_decimal(number_of_results),
        advanced_search=advanced_search,
        suggestions=result_container.suggestions,
        answers=result_container.answers,
        corrections=result_container.corrections,
        infoboxes=result_container.infoboxes,
        paging=result_container.paging,
        unresponsive_engines=result_container.unresponsive_engines,
        current_language=match_language(search_query.lang,
                                        LANGUAGE_CODES,
                                        fallback=settings['search']['language']),
        base_url=get_base_url(),
        theme=get_current_theme_name(),
        favicons=global_favicons[themes.index(get_current_theme_name())]
    )
def index():
    """Render index page.

    Supported outputs: html, json, csv, rss.
    """

    # output_format
    output_format = request.form.get('format', 'html')
    if output_format not in ['html', 'csv', 'json', 'rss']:
        output_format = 'html'

    # check if there is query
    if request.form.get('q') is None:
        if output_format == 'html':
            return render(
                'index.html',
            )
        else:
            return index_error(output_format, 'No query'), 400

    # search
    search_query = None
    result_container = None
    try:
        search_query = get_search_query_from_webapp(request.preferences, request.form)
        # search = Search(search_query) #  without plugins
        search = SearchWithPlugins(search_query, request.user_plugins, request)
        result_container = search.search()
    except Exception as e:
        # log exception
        logger.exception('search error')

        # is it an invalid input parameter or something else ?
        if (issubclass(e.__class__, SearxParameterException)):
            return index_error(output_format, e.message), 400
        else:
            return index_error(output_format, gettext('search error')), 500

    # results
    results = result_container.get_ordered_results()
    number_of_results = result_container.results_number()
    if number_of_results < result_container.results_length():
        number_of_results = 0

    # UI
    advanced_search = request.form.get('advanced_search', None)

    # output
    for result in results:
        if output_format == 'html':
            if 'content' in result and result['content']:
                result['content'] = highlight_content(escape(result['content'][:1024]), search_query.query)
            result['title'] = highlight_content(escape(result['title'] or u''), search_query.query)
        else:
            if result.get('content'):
                result['content'] = html_to_text(result['content']).strip()
            # removing html content and whitespace duplications
            result['title'] = ' '.join(html_to_text(result['title']).strip().split())

        result['pretty_url'] = prettify_url(result['url'])

        # TODO, check if timezone is calculated right
        if 'publishedDate' in result:
            try:  # test if publishedDate >= 1900 (datetime module bug)
                result['pubdate'] = result['publishedDate'].strftime('%Y-%m-%d %H:%M:%S%z')
            except ValueError:
                result['publishedDate'] = None
            else:
                if result['publishedDate'].replace(tzinfo=None) >= datetime.now() - timedelta(days=1):
                    timedifference = datetime.now() - result['publishedDate'].replace(tzinfo=None)
                    minutes = int((timedifference.seconds / 60) % 60)
                    hours = int(timedifference.seconds / 60 / 60)
                    if hours == 0:
                        result['publishedDate'] = gettext(u'{minutes} minute(s) ago').format(minutes=minutes)
                    else:
                        result['publishedDate'] = gettext(u'{hours} hour(s), {minutes} minute(s) ago').format(hours=hours, minutes=minutes)  # noqa
                else:
                    result['publishedDate'] = format_date(result['publishedDate'])

    if output_format == 'json':
        return Response(json.dumps({'query': search_query.query.decode('utf-8'),
                                    'number_of_results': number_of_results,
                                    'results': results,
                                    'answers': list(result_container.answers),
                                    'corrections': list(result_container.corrections),
                                    'infoboxes': result_container.infoboxes,
                                    'suggestions': list(result_container.suggestions),
                                    'unresponsive_engines': list(result_container.unresponsive_engines)},
                                   default=lambda item: list(item) if isinstance(item, set) else item),
                        mimetype='application/json')
    elif output_format == 'csv':
        csv = UnicodeWriter(StringIO())
        keys = ('title', 'url', 'content', 'host', 'engine', 'score')
        csv.writerow(keys)
        for row in results:
            row['host'] = row['parsed_url'].netloc
            csv.writerow([row.get(key, '') for key in keys])
        csv.stream.seek(0)
        response = Response(csv.stream.read(), mimetype='application/csv')
        cont_disp = 'attachment;Filename=searx_-_{0}.csv'.format(search_query.query)
        response.headers.add('Content-Disposition', cont_disp)
        return response
    elif output_format == 'rss':
        response_rss = render(
            'opensearch_response_rss.xml',
            results=results,
            q=request.form['q'],
            number_of_results=number_of_results,
            base_url=get_base_url(),
            override_theme='__common__',
        )
        return Response(response_rss, mimetype='text/xml')

    return render(
        'results.html',
        results=results,
        q=request.form['q'],
        selected_categories=search_query.categories,
        pageno=search_query.pageno,
        time_range=search_query.time_range,
        number_of_results=format_decimal(number_of_results),
        advanced_search=advanced_search,
        suggestions=result_container.suggestions,
        answers=result_container.answers,
        corrections=result_container.corrections,
        infoboxes=result_container.infoboxes,
        paging=result_container.paging,
        unresponsive_engines=result_container.unresponsive_engines,
        current_language=match_language(search_query.lang,
                                        LANGUAGE_CODES,
                                        fallback=settings['search']['language']),
        base_url=get_base_url(),
        theme=get_current_theme_name(),
        favicons=global_favicons[themes.index(get_current_theme_name())]
    )
def index ( ) : # output_format output_format = request . form . get ( 'format' , 'html' ) if output_format not in [ 'html' , 'csv' , 'json' , 'rss' ] : output_format = 'html' # check if there is query if request . form . get ( 'q' ) is None : if output_format == 'html' : return render ( 'index.html' , ) else : return index_error ( output_format , 'No query' ) , 400 # search search_query = None result_container = None try : search_query = get_search_query_from_webapp ( request . preferences , request . form ) # search = Search(search_query) #  without plugins search = SearchWithPlugins ( search_query , request . user_plugins , request ) result_container = search . search ( ) except Exception as e : # log exception logger . exception ( 'search error' ) # is it an invalid input parameter or something else ? if ( issubclass ( e . __class__ , SearxParameterException ) ) : return index_error ( output_format , e . message ) , 400 else : return index_error ( output_format , gettext ( 'search error' ) ) , 500 # results results = result_container . get_ordered_results ( ) number_of_results = result_container . results_number ( ) if number_of_results < result_container . results_length ( ) : number_of_results = 0 # UI advanced_search = request . form . get ( 'advanced_search' , None ) # output for result in results : if output_format == 'html' : if 'content' in result and result [ 'content' ] : result [ 'content' ] = highlight_content ( escape ( result [ 'content' ] [ : 1024 ] ) , search_query . query ) result [ 'title' ] = highlight_content ( escape ( result [ 'title' ] or u'' ) , search_query . query ) else : if result . get ( 'content' ) : result [ 'content' ] = html_to_text ( result [ 'content' ] ) . strip ( ) # removing html content and whitespace duplications result [ 'title' ] = ' ' . join ( html_to_text ( result [ 'title' ] ) . strip ( ) . split ( ) ) result [ 'pretty_url' ] = prettify_url ( result [ 'url' ] ) # TODO, check if timezone is calculated right if 'publishedDate' in result : try : # test if publishedDate >= 1900 (datetime module bug) result [ 'pubdate' ] = result [ 'publishedDate' ] . strftime ( '%Y-%m-%d %H:%M:%S%z' ) except ValueError : result [ 'publishedDate' ] = None else : if result [ 'publishedDate' ] . replace ( tzinfo = None ) >= datetime . now ( ) - timedelta ( days = 1 ) : timedifference = datetime . now ( ) - result [ 'publishedDate' ] . replace ( tzinfo = None ) minutes = int ( ( timedifference . seconds / 60 ) % 60 ) hours = int ( timedifference . seconds / 60 / 60 ) if hours == 0 : result [ 'publishedDate' ] = gettext ( u'{minutes} minute(s) ago' ) . format ( minutes = minutes ) else : result [ 'publishedDate' ] = gettext ( u'{hours} hour(s), {minutes} minute(s) ago' ) . format ( hours = hours , minutes = minutes ) # noqa else : result [ 'publishedDate' ] = format_date ( result [ 'publishedDate' ] ) if output_format == 'json' : return Response ( json . dumps ( { 'query' : search_query . query . decode ( 'utf-8' ) , 'number_of_results' : number_of_results , 'results' : results , 'answers' : list ( result_container . answers ) , 'corrections' : list ( result_container . corrections ) , 'infoboxes' : result_container . infoboxes , 'suggestions' : list ( result_container . suggestions ) , 'unresponsive_engines' : list ( result_container . unresponsive_engines ) } , default = lambda item : list ( item ) if isinstance ( item , set ) else item ) , mimetype = 'application/json' ) elif output_format == 'csv' : csv = UnicodeWriter ( StringIO ( ) ) keys = ( 'title' , 'url' , 'content' , 'host' , 'engine' , 'score' ) csv . writerow ( keys ) for row in results : row [ 'host' ] = row [ 'parsed_url' ] . netloc csv . writerow ( [ row . get ( key , '' ) for key in keys ] ) csv . stream . seek ( 0 ) response = Response ( csv . stream . read ( ) , mimetype = 'application/csv' ) cont_disp = 'attachment;Filename=searx_-_{0}.csv' . format ( search_query . query ) response . headers . add ( 'Content-Disposition' , cont_disp ) return response elif output_format == 'rss' : response_rss = render ( 'opensearch_response_rss.xml' , results = results , q = request . form [ 'q' ] , number_of_results = number_of_results , base_url = get_base_url ( ) , override_theme = '__common__' , ) return Response ( response_rss , mimetype = 'text/xml' ) return render ( 'results.html' , results = results , q = request . form [ 'q' ] , selected_categories = search_query . categories , pageno = search_query . pageno , time_range = search_query . time_range , number_of_results = format_decimal ( number_of_results ) , advanced_search = advanced_search , suggestions = result_container . suggestions , answers = result_container . answers , corrections = result_container . corrections , infoboxes = result_container . infoboxes , paging = result_container . paging , unresponsive_engines = result_container . unresponsive_engines , current_language = match_language ( search_query . lang , LANGUAGE_CODES , fallback = settings [ 'search' ] [ 'language' ] ) , base_url = get_base_url ( ) , theme = get_current_theme_name ( ) , favicons = global_favicons [ themes . index ( get_current_theme_name ( ) ) ] )
Render index page.

    Supported outputs: html, json, csv, rss.
Render index page .