Exemplo n.º 1
0
def initialize():
    logger.info('Send SIGUSR1 signal to pid %i to start the checker',
                os.getpid())
    signal.signal(signal.SIGUSR1, _signal_handler)

    # disabled by default
    _set_result({'status': 'disabled'}, include_timestamp=False)

    # special case when debug is activate
    if searx_debug and settings.get('checker', {}).get('off_when_debug', True):
        logger.info('debug mode: checker is disabled')
        return

    # check value of checker.scheduling.every now
    scheduling = settings.get('checker', {}).get('scheduling', None)
    if scheduling is None or not scheduling:
        logger.info('Checker scheduler is disabled')
        return

    #
    _set_result({'status': 'unknown'}, include_timestamp=False)

    start_after = scheduling.get('start_after', (300, 1800))
    start_after = _get_interval(
        start_after, 'checker.scheduling.start_after is not a int or list')
    delay = random.randint(start_after[0], start_after[1])
    logger.info('Start checker in %i seconds', delay)
    t = threading.Timer(delay, _start_scheduling)
    t.daemon = True
    t.start()
Exemplo n.º 2
0
def image_proxify(url):

    if url.startswith('//'):
        url = 'https:' + url

    if not request.preferences.get_value('image_proxy'):
        return url

    if url.startswith('data:image/'):
        # 50 is an arbitrary number to get only the beginning of the image.
        partial_base64 = url[len('data:image/'):50].split(';')
        if len(partial_base64) == 2 \
           and partial_base64[0] in ['gif', 'png', 'jpeg', 'pjpeg', 'webp', 'tiff', 'bmp']\
           and partial_base64[1].startswith('base64,'):
            return url
        else:
            return None

    if settings.get('result_proxy'):
        return proxify(url)

    h = new_hmac(settings['server']['secret_key'], url.encode())

    return '{0}?{1}'.format(url_for('image_proxy'),
                            urlencode(dict(url=url.encode(), h=h)))
Exemplo n.º 3
0
def image_proxify(url):

    if url.startswith("//"):
        url = "https:" + url

    if not request.preferences.get_value("image_proxy"):
        return url

    if url.startswith("data:image/"):
        # 50 is an arbitrary number to get only the beginning of the image.
        partial_base64 = url[len("data:image/"):50].split(";")
        if (len(partial_base64) == 2 and partial_base64[0]
                in ["gif", "png", "jpeg", "pjpeg", "webp", "tiff", "bmp"]
                and partial_base64[1].startswith("base64,")):
            return url
        else:
            return None

    if settings.get("result_proxy"):
        return proxify(url)

    h = new_hmac(settings["server"]["secret_key"], url.encode())

    return "{0}?{1}".format(url_for("image_proxy"),
                            urlencode(dict(url=url.encode(), h=h)))
Exemplo n.º 4
0
def request(method, url, **kwargs):
    """same as requests/requests/api.py request(...) except it use SessionSinglePool and force proxies"""
    global settings
    session = SessionSinglePool()
    kwargs['proxies'] = settings.get('outgoing_proxies', None)
    response = session.request(method=method, url=url, **kwargs)
    session.close()
    return response
Exemplo n.º 5
0
def proxify(url):
    if url.startswith('//'):
        url = 'https:' + url

    if not settings.get('result_proxy'):
        return url

    h = hmac.new(settings['result_proxy']['key'], url.encode('utf-8'), hashlib.sha256).hexdigest()

    return '{0}?{1}'.format(settings['result_proxy']['url'],
                            urlencode(dict(mortyurl=url.encode('utf-8'),
                                           mortyhash=h)))
Exemplo n.º 6
0
Arquivo: webapp.py Projeto: kvch/searx
def image_proxify(url):

    if url.startswith("//"):
        url = "https:" + url

    if not request.preferences.get_value("image_proxy"):
        return url

    if settings.get("result_proxy"):
        return proxify(url)

    h = hmac.new(settings["server"]["secret_key"], url.encode("utf-8"), hashlib.sha256).hexdigest()

    return "{0}?{1}".format(url_for("image_proxy"), urlencode(dict(url=url.encode("utf-8"), h=h)))
Exemplo n.º 7
0
def image_proxify(url):

    if url.startswith('//'):
        url = 'https:' + url

    if not request.preferences.get_value('image_proxy'):
        return url

    if settings.get('result_proxy'):
        return proxify(url)

    h = new_hmac(settings['server']['secret_key'], url.encode('utf-8'))

    return '{0}?{1}'.format(url_for('image_proxy'),
                            urlencode(dict(url=url.encode('utf-8'), h=h)))
Exemplo n.º 8
0
Arquivo: webapp.py Projeto: wflk/searx
def image_proxify(url):

    if url.startswith('//'):
        url = 'https:' + url

    if not request.preferences.get_value('image_proxy'):
        return url

    if settings.get('result_proxy'):
        return proxify(url)

    h = hmac.new(settings['server']['secret_key'], url.encode('utf-8'), hashlib.sha256).hexdigest()

    return '{0}?{1}'.format(url_for('image_proxy'),
                            urlencode(dict(url=url.encode('utf-8'), h=h)))
Exemplo n.º 9
0
Arquivo: webapp.py Projeto: kvch/searx
def proxify(url):
    if url.startswith("//"):
        url = "https:" + url

    if not settings.get("result_proxy"):
        return url

    url_params = dict(mortyurl=url.encode("utf-8"))

    if settings["result_proxy"].get("key"):
        url_params["mortyhash"] = hmac.new(
            settings["result_proxy"]["key"], url.encode("utf-8"), hashlib.sha256
        ).hexdigest()

    return "{0}?{1}".format(settings["result_proxy"]["url"], urlencode(url_params))
Exemplo n.º 10
0
def proxify(url):
    if url.startswith('//'):
        url = 'https:' + url

    if not settings.get('result_proxy'):
        return url

    url_params = dict(mortyurl=url.encode())

    if settings['result_proxy'].get('key'):
        url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'],
                                           url.encode(),
                                           hashlib.sha256).hexdigest()

    return '{0}?{1}'.format(settings['result_proxy']['url'],
                            urlencode(url_params))
Exemplo n.º 11
0
def proxify(url):
    if url.startswith('//'):
        url = 'https:' + url

    if not settings.get('result_proxy'):
        return url

    url_params = dict(mortyurl=url.encode('utf-8'))

    if settings['result_proxy'].get('key'):
        url_params['mortyhash'] = hmac.new(settings['result_proxy']['key'],
                                           url.encode('utf-8'),
                                           hashlib.sha256).hexdigest()

    return '{0}?{1}'.format(settings['result_proxy']['url'],
                            urlencode(url_params))
Exemplo n.º 12
0
def proxify(url):
    if url.startswith("//"):
        url = "https:" + url

    if not settings.get("result_proxy"):
        return url

    url_params = dict(mortyurl=url.encode())

    if settings["result_proxy"].get("key"):
        url_params["mortyhash"] = hmac.new(settings["result_proxy"]["key"],
                                           url.encode(),
                                           hashlib.sha256).hexdigest()

    return "{0}?{1}".format(settings["result_proxy"]["url"],
                            urlencode(url_params))
Exemplo n.º 13
0
def image_proxify(url):
    if url.startswith('//'):
        url = 'https:' + url

    if not request.preferences.get_value('image_proxy'):
        return url

    if url.startswith('data:image/jpeg;base64,'):
        return url

    if settings.get('result_proxy'):
        return proxify(url)

    h = new_hmac(settings['server']['secret_key'], url.encode('utf-8'))

    return '{0}?{1}'.format(url_for('image_proxy'),
                            urlencode(dict(url=url.encode('utf-8'), h=h)))
Exemplo n.º 14
0
def render(template_name, override_theme=None, **kwargs):
    disabled_engines = request.preferences.engines.get_disabled()

    enabled_categories = set(category for engine_name in engines
                             for category in engines[engine_name].categories
                             if (engine_name,
                                 category) not in disabled_engines)

    if "categories" not in kwargs:
        kwargs["categories"] = [
            x for x in _get_ordered_categories() if x in enabled_categories
        ]

    if "autocomplete" not in kwargs:
        kwargs["autocomplete"] = request.preferences.get_value("autocomplete")

    locale = request.preferences.get_value("locale")

    if locale in rtl_locales and "rtl" not in kwargs:
        kwargs["rtl"] = True

    kwargs["searx_version"] = VERSION_STRING

    kwargs["method"] = request.preferences.get_value("method")

    kwargs["safesearch"] = str(request.preferences.get_value("safesearch"))

    kwargs["language_codes"] = languages
    if "current_language" not in kwargs:
        kwargs["current_language"] = match_language(
            request.preferences.get_value("language"), LANGUAGE_CODES)

    # override url_for function in templates
    kwargs["url_for"] = url_for_theme

    kwargs["image_proxify"] = image_proxify

    kwargs["proxify"] = proxify if settings.get("result_proxy",
                                                {}).get("url") else None

    kwargs["opensearch_url"] = (
        url_for("opensearch") + "?" +
        urlencode({
            "method": kwargs["method"],
            "autocomplete": kwargs["autocomplete"]
        }))

    kwargs["get_result_template"] = get_result_template

    kwargs["theme"] = get_current_theme_name(override=override_theme)

    kwargs["template_name"] = template_name

    kwargs["cookies"] = request.cookies

    kwargs["errors"] = request.errors

    kwargs["instance_name"] = settings["general"]["instance_name"]

    kwargs["results_on_new_tab"] = request.preferences.get_value(
        "results_on_new_tab")

    kwargs["preferences"] = request.preferences

    kwargs["brand"] = brand

    kwargs["scripts"] = set()
    kwargs["endpoint"] = "results" if "q" in kwargs else request.endpoint
    for plugin in request.user_plugins:
        for script in plugin.js_dependencies:
            kwargs["scripts"].add(script)

    kwargs["styles"] = set()
    for plugin in request.user_plugins:
        for css in plugin.css_dependencies:
            kwargs["styles"].add(css)

    return render_template("{}/{}".format(kwargs["theme"], template_name),
                           **kwargs)
Exemplo n.º 15
0
def render(template_name, override_theme=None, **kwargs):
    disabled_engines = request.preferences.engines.get_disabled()

    enabled_categories = set(category for engine_name in engines
                             for category in engines[engine_name].categories
                             if (engine_name, category) not in disabled_engines)

    if 'categories' not in kwargs:
        kwargs['categories'] = ['general']
        kwargs['categories'].extend(x for x in
                                    sorted(categories.keys())
                                    if x != 'general'
                                    and x in enabled_categories)

    if 'all_categories' not in kwargs:
        kwargs['all_categories'] = ['general']
        kwargs['all_categories'].extend(x for x in
                                        sorted(categories.keys())
                                        if x != 'general')

    if 'selected_categories' not in kwargs:
        kwargs['selected_categories'] = []
        for arg in request.args:
            if arg.startswith('category_'):
                c = arg.split('_', 1)[1]
                if c in categories:
                    kwargs['selected_categories'].append(c)

    if not kwargs['selected_categories']:
        cookie_categories = request.preferences.get_value('categories')
        for ccateg in cookie_categories:
            kwargs['selected_categories'].append(ccateg)

    if not kwargs['selected_categories']:
        kwargs['selected_categories'] = ['general']

    if 'autocomplete' not in kwargs:
        kwargs['autocomplete'] = request.preferences.get_value('autocomplete')

    if get_locale() in rtl_locales and 'rtl' not in kwargs:
        kwargs['rtl'] = True

    kwargs['searx_version'] = VERSION_STRING

    kwargs['method'] = request.preferences.get_value('method')

    kwargs['safesearch'] = str(request.preferences.get_value('safesearch'))

    kwargs['language_codes'] = languages
    if 'current_language' not in kwargs:
        kwargs['current_language'] = match_language(request.preferences.get_value('language'),
                                                    LANGUAGE_CODES,
                                                    fallback=settings['search']['language'])

    # override url_for function in templates
    kwargs['url_for'] = url_for_theme

    kwargs['image_proxify'] = image_proxify

    kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None

    kwargs['get_result_template'] = get_result_template

    kwargs['theme'] = get_current_theme_name(override=override_theme)

    kwargs['template_name'] = template_name

    kwargs['cookies'] = request.cookies

    kwargs['errors'] = request.errors

    kwargs['instance_name'] = settings['general']['instance_name']

    kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')

    kwargs['unicode'] = unicode

    kwargs['preferences'] = request.preferences

    kwargs['scripts'] = set()
    for plugin in request.user_plugins:
        for script in plugin.js_dependencies:
            kwargs['scripts'].add(script)

    kwargs['styles'] = set()
    for plugin in request.user_plugins:
        for css in plugin.css_dependencies:
            kwargs['styles'].add(css)

    return render_template(
        '{}/{}'.format(kwargs['theme'], template_name), **kwargs)
Exemplo n.º 16
0
Arquivo: webapp.py Projeto: kvch/searx
def render(template_name, override_theme=None, **kwargs):
    disabled_engines = request.preferences.engines.get_disabled()

    enabled_categories = set(
        category
        for engine_name in engines
        for category in engines[engine_name].categories
        if (engine_name, category) not in disabled_engines
    )

    if "categories" not in kwargs:
        kwargs["categories"] = ["general"]
        kwargs["categories"].extend(x for x in sorted(categories.keys()) if x != "general" and x in enabled_categories)

    if "all_categories" not in kwargs:
        kwargs["all_categories"] = ["general"]
        kwargs["all_categories"].extend(x for x in sorted(categories.keys()) if x != "general")

    if "selected_categories" not in kwargs:
        kwargs["selected_categories"] = []
        for arg in request.args:
            if arg.startswith("category_"):
                c = arg.split("_", 1)[1]
                if c in categories:
                    kwargs["selected_categories"].append(c)

    if not kwargs["selected_categories"]:
        cookie_categories = request.preferences.get_value("categories")
        for ccateg in cookie_categories:
            kwargs["selected_categories"].append(ccateg)

    if not kwargs["selected_categories"]:
        kwargs["selected_categories"] = ["general"]

    if "autocomplete" not in kwargs:
        kwargs["autocomplete"] = request.preferences.get_value("autocomplete")

    if get_locale() in rtl_locales and "rtl" not in kwargs:
        kwargs["rtl"] = True

    kwargs["searx_version"] = VERSION_STRING

    kwargs["method"] = request.preferences.get_value("method")

    kwargs["safesearch"] = str(request.preferences.get_value("safesearch"))

    # override url_for function in templates
    kwargs["url_for"] = url_for_theme

    kwargs["image_proxify"] = image_proxify

    kwargs["proxify"] = proxify if settings.get("result_proxy") else None

    kwargs["get_result_template"] = get_result_template

    kwargs["theme"] = get_current_theme_name(override=override_theme)

    kwargs["template_name"] = template_name

    kwargs["cookies"] = request.cookies

    kwargs["errors"] = request.errors

    kwargs["instance_name"] = settings["general"]["instance_name"]

    kwargs["results_on_new_tab"] = request.preferences.get_value("results_on_new_tab")

    kwargs["scripts"] = set()
    for plugin in request.user_plugins:
        for script in plugin.js_dependencies:
            kwargs["scripts"].add(script)

    kwargs["styles"] = set()
    for plugin in request.user_plugins:
        for css in plugin.css_dependencies:
            kwargs["styles"].add(css)

    return render_template("{}/{}".format(kwargs["theme"], template_name), **kwargs)
Exemplo n.º 17
0
from searx import logger
try:
    from pygments import highlight
    from pygments.lexers import get_lexer_by_name
    from pygments.formatters import HtmlFormatter
except:
    logger.critical("cannot import dependency: pygments")
    from sys import exit
    exit(1)


logger = logger.getChild('webapp')

static_path, templates_path, themes =\
    get_themes(settings['themes_path']
               if settings.get('themes_path')
               else searx_dir)

default_theme = settings['server'].get('default_theme', 'default')

static_files = get_static_files(searx_dir)

result_templates = get_result_templates(searx_dir)

app = Flask(
    __name__,
    static_folder=static_path,
    template_folder=templates_path
)

app.secret_key = settings['server']['secret_key']
Exemplo n.º 18
0
        self.init_poolmanager(pool_connections, pool_maxsize, block=pool_block, **conn_params)

    def __setstate__(self, state):
        # Can't handle by adding 'proxy_manager' to self.__attrs__ because
        # because self.poolmanager uses a lambda function, which isn't pickleable.
        self.proxy_manager = {}
        self.config = {}

        for attr, value in state.items():
            setattr(self, attr, value)

        self.init_poolmanager(self._pool_connections, self._pool_maxsize,
                              block=self._pool_block, **self._conn_params)


if settings.get('source_ips'):
    http_adapters = cycle(HTTPAdapterWithConnParams(pool_connections=100, source_address=(source_ip, 0))
                          for source_ip in settings['source_ips'])
    https_adapters = cycle(HTTPAdapterWithConnParams(pool_connections=100, source_address=(source_ip, 0))
                           for source_ip in settings['source_ips'])
else:
    http_adapters = cycle((HTTPAdapterWithConnParams(pool_connections=100), ))
    https_adapters = cycle((HTTPAdapterWithConnParams(pool_connections=100), ))


class SessionSinglePool(requests.Session):

    def __init__(self):
        super(SessionSinglePool, self).__init__()

        # reuse the same adapters
Exemplo n.º 19
0
from searx.plugins import plugins

# check if the pyopenssl, ndg-httpsclient, pyasn1 packages are installed.
# They are needed for SSL connection without trouble, see #298
try:
    import OpenSSL.SSL  # NOQA
    import ndg.httpsclient  # NOQA
    import pyasn1  # NOQA
except ImportError:
    logger.critical("The pyopenssl, ndg-httpsclient, pyasn1 packages have to be installed.\n"
                    "Some HTTPS connections will failed")


static_path, templates_path, themes =\
    get_themes(settings['themes_path']
               if settings.get('themes_path')
               else searx_dir)

default_theme = settings['server'].get('default_theme', 'default')

static_files = get_static_files(searx_dir)

result_templates = get_result_templates(searx_dir)

app = Flask(
    __name__,
    static_folder=static_path,
    template_folder=templates_path
)

app.jinja_env.trim_blocks = True
Exemplo n.º 20
0
from searx import settings
from searx.answerers import ask
from searx.external_bang import get_bang_url
from searx.results import ResultContainer
from searx import logger
from searx.plugins import plugins
from searx.search.models import EngineRef, SearchQuery
from searx.search.processors import processors, initialize as initialize_processors
from searx.search.checker import initialize as initialize_checker
from searx.metrics import initialize as initialize_metrics, counter_inc, histogram_observe_time


logger = logger.getChild('search')

max_request_timeout = settings.get('outgoing', {}).get('max_request_timeout' or None)
if max_request_timeout is None:
    logger.info('max_request_timeout={0}'.format(max_request_timeout))
else:
    if isinstance(max_request_timeout, float):
        logger.info('max_request_timeout={0} second(s)'.format(max_request_timeout))
    else:
        logger.critical('outgoing.max_request_timeout if defined has to be float')
        import sys
        sys.exit(1)


def initialize(settings_engines=None, enable_checker=False):
    settings_engines = settings_engines or settings['engines']
    initialize_processors(settings_engines)
    initialize_metrics([engine['name'] for engine in settings_engines])
Exemplo n.º 21
0
    categories, engines, get_engines_stats, engine_shortcuts
)
from searx.utils import (
    UnicodeWriter, highlight_content, html_to_text, get_themes
)
from searx.version import VERSION_STRING
from searx.languages import language_codes
from searx.https_rewrite import https_url_rewrite
from searx.search import Search
from searx.query import Query
from searx.autocomplete import backends as autocomplete_backends


static_path, templates_path, themes =\
    get_themes(settings['themes_path']
               if settings.get('themes_path')
               else searx_dir)

default_theme = settings['server'].get('default_theme', 'default')

app = Flask(
    __name__,
    static_folder=static_path,
    template_folder=templates_path
)

app.secret_key = settings['server']['secret_key']

babel = Babel(app)

global_favicons = []
Exemplo n.º 22
0
from searx import settings, searx_dir
from searx.engines import (
    categories, engines, get_engines_stats, engine_shortcuts
)
from searx.utils import (
    UnicodeWriter, highlight_content, html_to_text, get_themes
)
from searx.https_rewrite import https_rules
from searx.languages import language_codes
from searx.search import Search
from searx.autocomplete import backends as autocomplete_backends


static_path, templates_path, themes =\
    get_themes(settings['themes_path']
               if settings.get('themes_path')
               else searx_dir)
default_theme = settings['default_theme'] if \
    settings.get('default_theme', None) else 'default'

app = Flask(
    __name__,
    static_folder=static_path,
    template_folder=templates_path
)

app.secret_key = settings['server']['secret_key']

babel = Babel(app)

#TODO configurable via settings.yml
Exemplo n.º 23
0
def initialize(settings_engines=None, settings_outgoing=None):
    from searx.engines import engines
    from searx import settings

    global NETWORKS

    settings_engines = settings_engines or settings.get('engines')
    settings_outgoing = settings_outgoing or settings.get('outgoing')

    # default parameters for AsyncHTTPTransport
    # see https://github.com/encode/httpx/blob/e05a5372eb6172287458b37447c30f650047e1b8/httpx/_transports/default.py#L108-L121  # noqa
    default_params = {
        'enable_http': False,
        'verify': True,
        'enable_http2': settings_outgoing.get('enable_http2', True),
        # Magic number kept from previous code
        'max_connections': settings_outgoing.get('pool_connections', 100),
        # Picked from constructor
        'max_keepalive_connections': settings_outgoing.get('pool_maxsize', 10),
        #
        'keepalive_expiry': settings_outgoing.get('keepalive_expiry', 5.0),
        'local_addresses': settings_outgoing.get('source_ips'),
        'proxies': settings_outgoing.get('proxies'),
        # default maximum redirect
        # from https://github.com/psf/requests/blob/8c211a96cdbe9fe320d63d9e1ae15c5c07e179f8/requests/models.py#L55
        'max_redirects': settings_outgoing.get('max_redirects', 30),
        #
        'retries': settings_outgoing.get('retries', 0),
        'retry_on_http_error': None,
    }

    def new_network(params):
        nonlocal default_params
        result = {}
        result.update(default_params)
        result.update(params)
        return Network(**result)

    def iter_networks():
        nonlocal settings_engines
        for engine_spec in settings_engines:
            engine_name = engine_spec['name']
            engine = engines.get(engine_name)
            if engine is None:
                continue
            network = getattr(engine, 'network', None)
            yield engine_name, engine, network

    if NETWORKS:
        done()
    NETWORKS.clear()
    NETWORKS[DEFAULT_NAME] = new_network({})
    NETWORKS['ipv4'] = new_network({'local_addresses': '0.0.0.0'})
    NETWORKS['ipv6'] = new_network({'local_addresses': '::'})

    # define networks from outgoing.networks
    for network_name, network in settings_outgoing.get('networks', {}).items():
        NETWORKS[network_name] = new_network(network)

    # define networks from engines.[i].network (except references)
    for engine_name, engine, network in iter_networks():
        if network is None:
            network = {}
            for attribute_name, attribute_value in default_params.items():
                if hasattr(engine, attribute_name):
                    network[attribute_name] = getattr(engine, attribute_name)
                else:
                    network[attribute_name] = attribute_value
            NETWORKS[engine_name] = new_network(network)
        elif isinstance(network, dict):
            NETWORKS[engine_name] = new_network(network)

    # define networks from engines.[i].network (references)
    for engine_name, engine, network in iter_networks():
        if isinstance(network, str):
            NETWORKS[engine_name] = NETWORKS[network]
Exemplo n.º 24
0
# check if the pyopenssl, ndg-httpsclient, pyasn1 packages are installed.
# They are needed for SSL connection without trouble, see #298
try:
    import OpenSSL.SSL  # NOQA
    import ndg.httpsclient  # NOQA
    import pyasn1  # NOQA
except ImportError:
    logger.critical(
        "The pyopenssl, ndg-httpsclient, pyasn1 packages have to be installed.\n"
        "Some HTTPS connections will failed")


static_path, templates_path, themes =\
    get_themes(settings['themes_path']
               if settings.get('themes_path')
               else searx_dir)

default_theme = settings['server'].get('default_theme', 'default')

static_files = get_static_files(searx_dir)

result_templates = get_result_templates(searx_dir)

app = Flask(__name__,
            static_folder=static_path,
            template_folder=templates_path)

app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
app.secret_key = settings['server']['secret_key']
Exemplo n.º 25
0
def render(template_name, override_theme=None, **kwargs):
    disabled_engines = request.preferences.engines.get_disabled()

    enabled_categories = set(category for engine_name in engines
                             for category in engines[engine_name].categories
                             if (engine_name, category) not in disabled_engines)

    if 'categories' not in kwargs:
        kwargs['categories'] = [x for x in
                                _get_ordered_categories()
                                if x in enabled_categories]

    if 'autocomplete' not in kwargs:
        kwargs['autocomplete'] = request.preferences.get_value('autocomplete')

    locale = request.preferences.get_value('locale')

    if locale in rtl_locales and 'rtl' not in kwargs:
        kwargs['rtl'] = True

    kwargs['searx_version'] = VERSION_STRING

    kwargs['method'] = request.preferences.get_value('method')

    kwargs['safesearch'] = str(request.preferences.get_value('safesearch'))

    kwargs['language_codes'] = languages
    if 'current_language' not in kwargs:
        kwargs['current_language'] = match_language(request.preferences.get_value('language'),
                                                    LANGUAGE_CODES)

    # override url_for function in templates
    kwargs['url_for'] = url_for_theme

    kwargs['image_proxify'] = image_proxify

    kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None

    kwargs['opensearch_url'] = url_for('opensearch') + '?' \
        + urlencode({'method': kwargs['method'], 'autocomplete': kwargs['autocomplete']})

    kwargs['get_result_template'] = get_result_template

    kwargs['theme'] = get_current_theme_name(override=override_theme)

    kwargs['template_name'] = template_name

    kwargs['cookies'] = request.cookies

    kwargs['errors'] = request.errors

    kwargs['instance_name'] = settings['general']['instance_name']

    kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')

    kwargs['preferences'] = request.preferences

    kwargs['brand'] = brand

    kwargs['scripts'] = set()
    kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint
    for plugin in request.user_plugins:
        for script in plugin.js_dependencies:
            kwargs['scripts'].add(script)

    kwargs['styles'] = set()
    for plugin in request.user_plugins:
        for css in plugin.css_dependencies:
            kwargs['styles'].add(css)

    return render_template(
        '{}/{}'.format(kwargs['theme'], template_name), **kwargs)
Exemplo n.º 26
0
def _get_every():
    every = settings.get('checker', {}).get('scheduling',
                                            {}).get('every', (300, 1800))
    return _get_interval(every,
                         'checker.scheduling.every is not a int or list')
Exemplo n.º 27
0
def render(template_name, override_theme=None, **kwargs):
    disabled_engines = request.preferences.engines.get_disabled()

    enabled_categories = set(category for engine_name in engines
                             for category in engines[engine_name].categories
                             if (engine_name, category) not in disabled_engines)

    if 'categories' not in kwargs:
        kwargs['categories'] = ['general']
        kwargs['categories'].extend(x for x in
                                    sorted(categories.keys())
                                    if x != 'general'
                                    and x in enabled_categories)

    if 'all_categories' not in kwargs:
        kwargs['all_categories'] = ['general']
        kwargs['all_categories'].extend(x for x in
                                        sorted(categories.keys())
                                        if x != 'general')

    if 'selected_categories' not in kwargs:
        kwargs['selected_categories'] = []
        for arg in request.args:
            if arg.startswith('category_'):
                c = arg.split('_', 1)[1]
                if c in categories:
                    kwargs['selected_categories'].append(c)

    if not kwargs['selected_categories']:
        cookie_categories = request.preferences.get_value('categories')
        for ccateg in cookie_categories:
            kwargs['selected_categories'].append(ccateg)

    if not kwargs['selected_categories']:
        kwargs['selected_categories'] = ['general']

    if 'autocomplete' not in kwargs:
        kwargs['autocomplete'] = request.preferences.get_value('autocomplete')

    locale = request.preferences.get_value('locale')

    if locale in rtl_locales and 'rtl' not in kwargs:
        kwargs['rtl'] = True

    kwargs['searx_version'] = VERSION_STRING

    kwargs['method'] = request.preferences.get_value('method')

    kwargs['safesearch'] = str(request.preferences.get_value('safesearch'))

    kwargs['language_codes'] = languages
    if 'current_language' not in kwargs:
        kwargs['current_language'] = match_language(request.preferences.get_value('language'),
                                                    LANGUAGE_CODES)

    # override url_for function in templates
    kwargs['url_for'] = url_for_theme

    kwargs['image_proxify'] = image_proxify

    kwargs['proxify'] = proxify if settings.get('result_proxy', {}).get('url') else None

    kwargs['get_result_template'] = get_result_template

    kwargs['theme'] = get_current_theme_name(override=override_theme)

    kwargs['template_name'] = template_name

    kwargs['cookies'] = request.cookies

    kwargs['errors'] = request.errors

    kwargs['instance_name'] = settings['general']['instance_name']

    kwargs['results_on_new_tab'] = request.preferences.get_value('results_on_new_tab')

    kwargs['unicode'] = unicode

    kwargs['preferences'] = request.preferences

    kwargs['brand'] = brand

    kwargs['scripts'] = set()
    for plugin in request.user_plugins:
        for script in plugin.js_dependencies:
            kwargs['scripts'].add(script)

    kwargs['styles'] = set()
    for plugin in request.user_plugins:
        for css in plugin.css_dependencies:
            kwargs['styles'].add(css)

    return render_template(
        '{}/{}'.format(kwargs['theme'], template_name), **kwargs)
Exemplo n.º 28
0
def render(template_name, override_theme=None, **kwargs):
    # values from the HTTP requests
    kwargs['endpoint'] = 'results' if 'q' in kwargs else request.endpoint
    kwargs['cookies'] = request.cookies
    kwargs['errors'] = request.errors

    # values from the preferences
    kwargs['preferences'] = request.preferences
    kwargs['method'] = request.preferences.get_value('method')
    kwargs['autocomplete'] = request.preferences.get_value('autocomplete')
    kwargs['results_on_new_tab'] = request.preferences.get_value(
        'results_on_new_tab')
    kwargs['advanced_search'] = request.preferences.get_value(
        'advanced_search')
    kwargs['safesearch'] = str(request.preferences.get_value('safesearch'))
    kwargs['theme'] = get_current_theme_name(override=override_theme)
    kwargs['all_categories'] = _get_ordered_categories()
    kwargs['categories'] = _get_enable_categories(kwargs['all_categories'])

    # i18n
    kwargs['language_codes'] = languages  # from searx.languages
    kwargs['translations'] = json.dumps(get_translations(),
                                        separators=(',', ':'))

    locale = request.preferences.get_value('locale')
    if locale in rtl_locales and 'rtl' not in kwargs:
        kwargs['rtl'] = True
    if 'current_language' not in kwargs:
        kwargs['current_language'] = match_language(
            request.preferences.get_value('language'), LANGUAGE_CODES)

    # values from settings
    kwargs['search_formats'] = [
        x for x in settings['search']['formats'] if x != 'html'
    ]

    # brand
    kwargs['instance_name'] = settings['general']['instance_name']
    kwargs['searx_version'] = VERSION_STRING
    kwargs['brand'] = brand

    # helpers to create links to other pages
    kwargs['url_for'] = url_for_theme  # override url_for function in templates
    kwargs['image_proxify'] = image_proxify
    kwargs['proxify'] = proxify if settings.get('result_proxy',
                                                {}).get('url') else None
    kwargs['proxify_results'] = settings.get('result_proxy',
                                             {}).get('proxify_results', True)
    kwargs['get_result_template'] = get_result_template
    kwargs['opensearch_url'] = (
        url_for('opensearch') + '?' +
        urlencode({
            'method': kwargs['method'],
            'autocomplete': kwargs['autocomplete']
        }))

    # scripts from plugins
    kwargs['scripts'] = set()
    for plugin in request.user_plugins:
        for script in plugin.js_dependencies:
            kwargs['scripts'].add(script)

    # styles from plugins
    kwargs['styles'] = set()
    for plugin in request.user_plugins:
        for css in plugin.css_dependencies:
            kwargs['styles'].add(css)

    start_time = default_timer()
    result = render_template('{}/{}'.format(kwargs['theme'], template_name),
                             **kwargs)
    request.render_time += default_timer() - start_time  # pylint: disable=assigning-non-slot

    return result