123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title>searx_extra/standalone_searx.py — Searx Documentation (Searx-1.1.0.tex)</title>
- <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
- <link rel="stylesheet" type="text/css" href="../_static/searx.css" />
- <link rel="stylesheet" type="text/css" href="../_static/tabs.css" />
- <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
- <script src="../_static/jquery.js"></script>
- <script src="../_static/underscore.js"></script>
- <script src="../_static/_sphinx_javascript_frameworks_compat.js"></script>
- <script src="../_static/doctools.js"></script>
- <script src="../_static/sphinx_highlight.js"></script>
- <link rel="index" title="Index" href="../genindex.html" />
- <link rel="search" title="Search" href="../search.html" />
- <link rel="next" title="Admin’s tooling box" href="../utils/index.html" />
- <link rel="prev" title="Tooling box searx_extra for developers and users" href="index.html" />
- </head><body>
- <div class="related" role="navigation" aria-label="related navigation">
- <h3>Navigation</h3>
- <ul>
- <li class="right" style="margin-right: 10px">
- <a href="../genindex.html" title="General Index"
- accesskey="I">index</a></li>
- <li class="right" >
- <a href="../py-modindex.html" title="Python Module Index"
- >modules</a> |</li>
- <li class="right" >
- <a href="../utils/index.html" title="Admin’s tooling box"
- accesskey="N">next</a> |</li>
- <li class="right" >
- <a href="index.html" title="Tooling box searx_extra for developers and users"
- accesskey="P">previous</a> |</li>
- <li class="nav-item nav-item-0"><a href="../index.html">Searx Documentation (Searx-1.1.0.tex)</a> »</li>
- <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Tooling box <code class="docutils literal notranslate"><span class="pre">searx_extra</span></code> for developers and users</a> »</li>
- <li class="nav-item nav-item-this"><a href=""><code class="docutils literal notranslate"><span class="pre">searx_extra/standalone_searx.py</span></code></a></li>
- </ul>
- </div>
- <div class="document">
- <div class="documentwrapper">
- <div class="bodywrapper">
- <div class="body" role="main">
-
- <section id="module-searx_extra.standalone_searx">
- <span id="searx-extra-standalone-searx-py"></span><span id="standalone-searx-py"></span><h1><code class="docutils literal notranslate"><span class="pre">searx_extra/standalone_searx.py</span></code><a class="headerlink" href="#module-searx_extra.standalone_searx" title="Permalink to this heading">¶</a></h1>
- <p>Script to run searx from terminal.</p>
- <p>Getting categories without initiate the engine will only return <cite>[‘general’]</cite></p>
- <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">searx.engines</span>
- <span class="gp">... </span><span class="nb">list</span><span class="p">(</span><span class="n">searx</span><span class="o">.</span><span class="n">engines</span><span class="o">.</span><span class="n">categories</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
- <span class="go">['general']</span>
- <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">searx.search</span>
- <span class="gp">... </span><span class="n">searx</span><span class="o">.</span><span class="n">search</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>
- <span class="gp">... </span><span class="nb">list</span><span class="p">(</span><span class="n">searx</span><span class="o">.</span><span class="n">engines</span><span class="o">.</span><span class="n">categories</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
- <span class="go">['general', 'it', 'science', 'images', 'news', 'videos', 'music', 'files', 'social media', 'map']</span>
- </pre></div>
- </div>
- <p>Example to use this script:</p>
- <div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ python3 searx_extra/standalone_searx.py rain
- </pre></div>
- </div>
- <p>Example to run it from python:</p>
- <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">importlib</span>
- <span class="gp">... </span><span class="kn">import</span> <span class="nn">json</span>
- <span class="gp">... </span><span class="kn">import</span> <span class="nn">sys</span>
- <span class="gp">... </span><span class="kn">import</span> <span class="nn">searx.engines</span>
- <span class="gp">... </span><span class="kn">import</span> <span class="nn">searx.search</span>
- <span class="gp">... </span><span class="n">search_query</span> <span class="o">=</span> <span class="s1">'rain'</span>
- <span class="gp">... </span><span class="c1"># initialize engines</span>
- <span class="gp">... </span><span class="n">searx</span><span class="o">.</span><span class="n">search</span><span class="o">.</span><span class="n">initialize</span><span class="p">()</span>
- <span class="gp">... </span><span class="c1"># load engines categories once instead of each time the function called</span>
- <span class="gp">... </span><span class="n">engine_cs</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">searx</span><span class="o">.</span><span class="n">engines</span><span class="o">.</span><span class="n">categories</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
- <span class="gp">... </span><span class="c1"># load module</span>
- <span class="gp">... </span><span class="n">spec</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">spec_from_file_location</span><span class="p">(</span>
- <span class="gp">... </span> <span class="s1">'utils.standalone_searx'</span><span class="p">,</span> <span class="s1">'searx_extra/standalone_searx.py'</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">sas</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">sas</span><span class="p">)</span>
- <span class="gp">... </span><span class="c1"># use function from module</span>
- <span class="gp">... </span><span class="n">prog_args</span> <span class="o">=</span> <span class="n">sas</span><span class="o">.</span><span class="n">parse_argument</span><span class="p">([</span><span class="n">search_query</span><span class="p">],</span> <span class="n">category_choices</span><span class="o">=</span><span class="n">engine_cs</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">search_q</span> <span class="o">=</span> <span class="n">sas</span><span class="o">.</span><span class="n">get_search_query</span><span class="p">(</span><span class="n">prog_args</span><span class="p">,</span> <span class="n">engine_categories</span><span class="o">=</span><span class="n">engine_cs</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">res_dict</span> <span class="o">=</span> <span class="n">sas</span><span class="o">.</span><span class="n">to_dict</span><span class="p">(</span><span class="n">search_q</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span>
- <span class="gp">... </span> <span class="n">res_dict</span><span class="p">,</span> <span class="n">sort_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">ensure_ascii</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span>
- <span class="gp">... </span> <span class="n">default</span><span class="o">=</span><span class="n">sas</span><span class="o">.</span><span class="n">json_serial</span><span class="p">))</span>
- <span class="go">{</span>
- <span class="go"> "answers": [],</span>
- <span class="go"> "infoboxes": [ {...} ],</span>
- <span class="go"> "paging": true,</span>
- <span class="go"> "results": [... ],</span>
- <span class="go"> "results_number": 820000000.0,</span>
- <span class="go"> "search": {</span>
- <span class="go"> "lang": "all",</span>
- <span class="go"> "pageno": 1,</span>
- <span class="go"> "q": "rain",</span>
- <span class="go"> "safesearch": 0,</span>
- <span class="go"> "timerange": null</span>
- <span class="go"> },</span>
- <span class="go"> "suggestions": [...]</span>
- <span class="go">}</span>
- </pre></div>
- </div>
- <dl class="py function">
- <dt class="sig sig-object py" id="searx_extra.standalone_searx.get_search_query">
- <span class="sig-prename descclassname"><span class="pre">searx_extra.standalone_searx.</span></span><span class="sig-name descname"><span class="pre">get_search_query</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/argparse.html#argparse.Namespace" title="(in Python v3.11)"><span class="pre">Namespace</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">engine_categories</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.11)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.11)"><span class="pre">List</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.11)"><span class="pre">str</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><span class="pre">SearchQuery</span></span></span><a class="reference internal" href="../_modules/searx_extra/standalone_searx.html#get_search_query"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#searx_extra.standalone_searx.get_search_query" title="Permalink to this definition">¶</a></dt>
- <dd><p>Get search results for the query</p>
- </dd></dl>
- <dl class="py function">
- <dt class="sig sig-object py" id="searx_extra.standalone_searx.json_serial">
- <span class="sig-prename descclassname"><span class="pre">searx_extra.standalone_searx.</span></span><span class="sig-name descname"><span class="pre">json_serial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.11)"><span class="pre">Any</span></a></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.11)"><span class="pre">Any</span></a></span></span><a class="reference internal" href="../_modules/searx_extra/standalone_searx.html#json_serial"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#searx_extra.standalone_searx.json_serial" title="Permalink to this definition">¶</a></dt>
- <dd><p>JSON serializer for objects not serializable by default json code.</p>
- <dl class="field-list simple">
- <dt class="field-odd">Raises<span class="colon">:</span></dt>
- <dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/exceptions.html#TypeError" title="(in Python v3.11)"><strong>TypeError</strong></a> – raised when <strong>obj</strong> is not serializable</p>
- </dd>
- </dl>
- </dd></dl>
- <dl class="py function">
- <dt class="sig sig-object py" id="searx_extra.standalone_searx.no_parsed_url">
- <span class="sig-prename descclassname"><span class="pre">searx_extra.standalone_searx.</span></span><span class="sig-name descname"><span class="pre">no_parsed_url</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">results</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.11)"><span class="pre">List</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Dict" title="(in Python v3.11)"><span class="pre">Dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.11)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.11)"><span class="pre">Any</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.11)"><span class="pre">List</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Dict" title="(in Python v3.11)"><span class="pre">Dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.11)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.11)"><span class="pre">Any</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span></span><a class="reference internal" href="../_modules/searx_extra/standalone_searx.html#no_parsed_url"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#searx_extra.standalone_searx.no_parsed_url" title="Permalink to this definition">¶</a></dt>
- <dd><p>Remove parsed url from dict.</p>
- </dd></dl>
- <dl class="py function">
- <dt class="sig sig-object py" id="searx_extra.standalone_searx.parse_argument">
- <span class="sig-prename descclassname"><span class="pre">searx_extra.standalone_searx.</span></span><span class="sig-name descname"><span class="pre">parse_argument</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.11)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.11)"><span class="pre">List</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.11)"><span class="pre">str</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">category_choices</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Optional" title="(in Python v3.11)"><span class="pre">Optional</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.List" title="(in Python v3.11)"><span class="pre">List</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.11)"><span class="pre">str</span></a><span class="p"><span class="pre">]</span></span><span class="p"><span class="pre">]</span></span></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/argparse.html#argparse.Namespace" title="(in Python v3.11)"><span class="pre">Namespace</span></a></span></span><a class="reference internal" href="../_modules/searx_extra/standalone_searx.html#parse_argument"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#searx_extra.standalone_searx.parse_argument" title="Permalink to this definition">¶</a></dt>
- <dd><p>Parse command line.</p>
- <dl class="field-list simple">
- <dt class="field-odd">Raises<span class="colon">:</span></dt>
- <dd class="field-odd"><p><a class="reference external" href="https://docs.python.org/3/library/exceptions.html#SystemExit" title="(in Python v3.11)"><strong>SystemExit</strong></a> – Query argument required on <cite>args</cite></p>
- </dd>
- </dl>
- <p>Examples:</p>
- <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">importlib</span>
- <span class="gp">... </span><span class="c1"># load module</span>
- <span class="gp">... </span><span class="n">spec</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">spec_from_file_location</span><span class="p">(</span>
- <span class="gp">... </span> <span class="s1">'utils.standalone_searx'</span><span class="p">,</span> <span class="s1">'utils/standalone_searx.py'</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">sas</span> <span class="o">=</span> <span class="n">importlib</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">module_from_spec</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">spec</span><span class="o">.</span><span class="n">loader</span><span class="o">.</span><span class="n">exec_module</span><span class="p">(</span><span class="n">sas</span><span class="p">)</span>
- <span class="gp">... </span><span class="n">sas</span><span class="o">.</span><span class="n">parse_argument</span><span class="p">()</span>
- <span class="go">usage: ptipython [-h] [--category [{general}]] [--lang [LANG]] [--pageno [PAGENO]] [--safesearch [{0,1,2}]] [--timerange [{day,week,month,year}]]</span>
- <span class="go"> query</span>
- <span class="go">SystemExit: 2</span>
- <span class="gp">>>> </span><span class="n">sas</span><span class="o">.</span><span class="n">parse_argument</span><span class="p">([</span><span class="s1">'rain'</span><span class="p">])</span>
- <span class="go">Namespace(category='general', lang='all', pageno=1, query='rain', safesearch='0', timerange=None)</span>
- </pre></div>
- </div>
- </dd></dl>
- <dl class="py function">
- <dt class="sig sig-object py" id="searx_extra.standalone_searx.to_dict">
- <span class="sig-prename descclassname"><span class="pre">searx_extra.standalone_searx.</span></span><span class="sig-name descname"><span class="pre">to_dict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">search_query</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><span class="pre">SearchQuery</span></span></em><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">→</span> <span class="sig-return-typehint"><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Dict" title="(in Python v3.11)"><span class="pre">Dict</span></a><span class="p"><span class="pre">[</span></span><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.11)"><span class="pre">str</span></a><span class="p"><span class="pre">,</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/typing.html#typing.Any" title="(in Python v3.11)"><span class="pre">Any</span></a><span class="p"><span class="pre">]</span></span></span></span><a class="reference internal" href="../_modules/searx_extra/standalone_searx.html#to_dict"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#searx_extra.standalone_searx.to_dict" title="Permalink to this definition">¶</a></dt>
- <dd><p>Get result from parsed arguments.</p>
- </dd></dl>
- </section>
- <div class="clearer"></div>
- </div>
- </div>
- </div>
- <span id="sidebar-top"></span>
- <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
- <div class="sphinxsidebarwrapper">
-
-
- <p class="logo"><a href="../index.html">
- <img class="logo" src="../_static/searx_logo_small.png" alt="Logo"/>
- </a></p>
-
- <h3>Project Links</h3>
- <ul>
- <li><a href="https://searx.github.io/searx/blog/index.html">Blog</a>
-
- <li><a href="https://github.com/searx/searx">Source</a>
-
- <li><a href="https://github.com/searx/searx/wiki">Wiki</a>
-
- <li><a href="https://twitter.com/Searx_engine">Twitter</a>
-
- <li><a href="https://github.com/searx/searx/issues">Issue Tracker</a>
- </ul><h3>Navigation</h3>
- <ul>
- <li><a href="../index.html">Overview</a>
- <ul>
- <li><a href="index.html">Tooling box <code class="docutils literal notranslate"><span class="pre">searx_extra</span></code> for developers and users</a>
- <ul>
- <li>Previous: <a href="index.html" title="previous chapter">Tooling box <code class="docutils literal notranslate"><span class="pre">searx_extra</span></code> for developers and users</a>
- <li>Next: <a href="../utils/index.html" title="next chapter">Admin’s tooling box</a></ul>
- </li>
- </ul>
- </li>
- </ul>
- <div id="searchbox" style="display: none" role="search">
- <h3 id="searchlabel">Quick search</h3>
- <div class="searchformwrapper">
- <form class="search" action="../search.html" method="get">
- <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
- <input type="submit" value="Go" />
- </form>
- </div>
- </div>
- <script>document.getElementById('searchbox').style.display = "block"</script>
- </div>
- </div>
- <div class="clearer"></div>
- </div>
-
- <div class="footer" role="contentinfo">
- © Copyright 2015-2022, Adam Tauber, Noémi Ványi.
- Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 5.3.0.
- </div>
- <script src="../_static/version_warning_offset.js"></script>
- </body>
- </html>
|