utils/standalone_searx.py

Script to run searx from terminal.

Getting categories without initiate the engine will only return [‘general’]

>>> import searx.engines
... list(searx.engines.categories.keys())
['general']
>>> import searx
... searx.engines.initialize_engines(searx.settings['engines'])
... list(searx.engines.categories.keys())
['general', 'it', 'science', 'images', 'news', 'videos', 'music', 'files', 'social media', 'map']

Example to use this script:

$ SEARX_DEBUG=1 python3 utils/standalone_searx.py rain

Example to run it from python:

>>> import importlib
... import json
... import sys
... import searx
... import searx.engines
... search_query = 'rain'
... # initialize engines
... searx.engines.initialize_engines(searx.settings['engines'])
... # load engines categories once instead of each time the function called
... engine_cs = list(searx.engines.categories.keys())
... # load module
... spec = importlib.util.spec_from_file_location(
...     'utils.standalone_searx', 'utils/standalone_searx.py')
... sas = importlib.util.module_from_spec(spec)
... spec.loader.exec_module(sas)
... # use function from module
... prog_args = sas.parse_argument([search_query], category_choices=engine_cs)
... search_q = sas.get_search_query(prog_args, engine_categories=engine_cs)
... res_dict = sas.to_dict(search_q)
... sys.stdout.write(json.dumps(
...     res_dict, sort_keys=True, indent=4, ensure_ascii=False,
...     default=sas.json_serial))
{
    "answers": [],
    "infoboxes": [ {...} ],
    "paging": true,
    "results": [... ],
    "results_number": 820000000.0,
    "search": {
        "lang": "all",
        "pageno": 1,
        "q": "rain",
        "safesearch": 0,
        "timerange": null
    },
    "suggestions": [...]
}
standalone_searx.get_search_query(args: argparse.Namespace, engine_categories: Optional[List[str]] = None) → searx.search.SearchQuery[source]

Get search results for the query

standalone_searx.json_serial(obj: Any) → Any[source]

JSON serializer for objects not serializable by default json code.

Raises

TypeError – raised when obj is not serializable

standalone_searx.no_parsed_url(results: List[Dict[str, Any]]) → List[Dict[str, Any]][source]

Remove parsed url from dict.

standalone_searx.parse_argument(args: Optional[List[str]] = None, category_choices: Optional[List[str]] = None)argparse.Namespace[source]

Parse command line.

Raises

SystemExit – Query argument required on args

Examples:

>>> import importlib
... # load module
... spec = importlib.util.spec_from_file_location(
...     'utils.standalone_searx', 'utils/standalone_searx.py')
... sas = importlib.util.module_from_spec(spec)
... spec.loader.exec_module(sas)
... sas.parse_argument()
usage: ptipython [-h] [--category [{general}]] [--lang [LANG]] [--pageno [PAGENO]] [--safesearch [{0,1,2}]] [--timerange [{day,week,month,year}]]
                 query
SystemExit: 2
>>> sas.parse_argument(['rain'])
Namespace(category='general', lang='all', pageno=1, query='rain', safesearch='0', timerange=None)
standalone_searx.to_dict(search_query: searx.search.SearchQuery) → Dict[str, Any][source]

Get result from parsed arguments.