searxng/_modules/searx/engines/qwant.html

472 lines
48 KiB
HTML

<!DOCTYPE html>
<html lang="en" data-content_root="../../../">
<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.engines.qwant &#8212; SearXNG Documentation (2024.10.31+fa108c140)</title>
<link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=4f649999" />
<link rel="stylesheet" type="text/css" href="../../../_static/searxng.css?v=52e4ff28" />
<link rel="stylesheet" type="text/css" href="../../../_static/autodoc_pydantic.css" />
<script src="../../../_static/documentation_options.js?v=b20e5003"></script>
<script src="../../../_static/doctools.js?v=9a2dae69"></script>
<script src="../../../_static/sphinx_highlight.js?v=dc90522c"></script>
<script data-project="searxng" data-version="2024.10.31+fa108c140" src="../../../_static/describe_version.js?v=fa7f30d0"></script>
<link rel="index" title="Index" href="../../../genindex.html" />
<link rel="search" title="Search" href="../../../search.html" />
</head><body>
<div class="related" role="navigation" aria-label="Related">
<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="nav-item nav-item-0"><a href="../../../index.html">SearXNG Documentation (2024.10.31+fa108c140)</a> &#187;</li>
<li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="../engines.html" accesskey="U">searx.engines</a> &#187;</li>
<li class="nav-item nav-item-this"><a href="">searx.engines.qwant</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<h1>Source code for searx.engines.qwant</h1><div class="highlight"><pre>
<span></span><span class="c1"># SPDX-License-Identifier: AGPL-3.0-or-later</span>
<span class="sd">&quot;&quot;&quot;This engine uses the Qwant API (https://api.qwant.com/v3) to implement Qwant</span>
<span class="sd">-Web, -News, -Images and -Videos. The API is undocumented but can be reverse</span>
<span class="sd">engineered by reading the network log of https://www.qwant.com/ queries.</span>
<span class="sd">For Qwant&#39;s *web-search* two alternatives are implemented:</span>
<span class="sd">- ``web``: uses the :py:obj:`api_url` which returns a JSON structure</span>
<span class="sd">- ``web-lite``: uses the :py:obj:`web_lite_url` which returns a HTML page</span>
<span class="sd">Configuration</span>
<span class="sd">=============</span>
<span class="sd">The engine has the following additional settings:</span>
<span class="sd">- :py:obj:`qwant_categ`</span>
<span class="sd">This implementation is used by different qwant engines in the :ref:`settings.yml</span>
<span class="sd">&lt;settings engine&gt;`:</span>
<span class="sd">.. code:: yaml</span>
<span class="sd"> - name: qwant</span>
<span class="sd"> qwant_categ: web-lite # alternatively use &#39;web&#39;</span>
<span class="sd"> ...</span>
<span class="sd"> - name: qwant news</span>
<span class="sd"> qwant_categ: news</span>
<span class="sd"> ...</span>
<span class="sd"> - name: qwant images</span>
<span class="sd"> qwant_categ: images</span>
<span class="sd"> ...</span>
<span class="sd"> - name: qwant videos</span>
<span class="sd"> qwant_categ: videos</span>
<span class="sd"> ...</span>
<span class="sd">Implementations</span>
<span class="sd">===============</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">datetime</span><span class="p">,</span>
<span class="n">timedelta</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">json</span> <span class="kn">import</span> <span class="n">loads</span>
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlencode</span>
<span class="kn">from</span> <span class="nn">flask_babel</span> <span class="kn">import</span> <span class="n">gettext</span>
<span class="kn">import</span> <span class="nn">babel</span>
<span class="kn">import</span> <span class="nn">lxml</span>
<span class="kn">from</span> <span class="nn">searx.exceptions</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">SearxEngineAPIException</span><span class="p">,</span>
<span class="n">SearxEngineTooManyRequestsException</span><span class="p">,</span>
<span class="n">SearxEngineCaptchaException</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">searx.network</span> <span class="kn">import</span> <span class="n">raise_for_httperror</span>
<span class="kn">from</span> <span class="nn">searx.enginelib.traits</span> <span class="kn">import</span> <span class="n">EngineTraits</span>
<span class="kn">from</span> <span class="nn">searx.utils</span> <span class="kn">import</span> <span class="p">(</span>
<span class="n">eval_xpath</span><span class="p">,</span>
<span class="n">eval_xpath_list</span><span class="p">,</span>
<span class="n">extract_text</span><span class="p">,</span>
<span class="n">get_embeded_stream_url</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">traits</span><span class="p">:</span> <span class="n">EngineTraits</span>
<span class="c1"># about</span>
<span class="n">about</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">&quot;website&quot;</span><span class="p">:</span> <span class="s1">&#39;https://www.qwant.com/&#39;</span><span class="p">,</span>
<span class="s2">&quot;wikidata_id&quot;</span><span class="p">:</span> <span class="s1">&#39;Q14657870&#39;</span><span class="p">,</span>
<span class="s2">&quot;official_api_documentation&quot;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
<span class="s2">&quot;use_official_api&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span>
<span class="s2">&quot;require_api_key&quot;</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span>
<span class="s2">&quot;results&quot;</span><span class="p">:</span> <span class="s1">&#39;JSON&#39;</span><span class="p">,</span>
<span class="p">}</span>
<span class="c1"># engine dependent config</span>
<span class="n">categories</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">paging</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">max_page</span> <span class="o">=</span> <span class="mi">5</span>
<span class="sd">&quot;&quot;&quot;5 pages maximum (``&amp;p=5``): Trying to do more just results in an improper</span>
<span class="sd">redirect&quot;&quot;&quot;</span>
<span class="n">qwant_categ</span> <span class="o">=</span> <span class="kc">None</span>
<span class="sd">&quot;&quot;&quot;One of ``web-lite`` (or ``web``), ``news``, ``images`` or ``videos``&quot;&quot;&quot;</span>
<span class="n">safesearch</span> <span class="o">=</span> <span class="kc">True</span>
<span class="c1"># safe_search_map = {0: &#39;&amp;safesearch=0&#39;, 1: &#39;&amp;safesearch=1&#39;, 2: &#39;&amp;safesearch=2&#39;}</span>
<span class="c1"># fmt: off</span>
<span class="n">qwant_news_locales</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">&#39;ca_ad&#39;</span><span class="p">,</span> <span class="s1">&#39;ca_es&#39;</span><span class="p">,</span> <span class="s1">&#39;ca_fr&#39;</span><span class="p">,</span> <span class="s1">&#39;co_fr&#39;</span><span class="p">,</span> <span class="s1">&#39;de_at&#39;</span><span class="p">,</span> <span class="s1">&#39;de_ch&#39;</span><span class="p">,</span> <span class="s1">&#39;de_de&#39;</span><span class="p">,</span> <span class="s1">&#39;en_au&#39;</span><span class="p">,</span>
<span class="s1">&#39;en_ca&#39;</span><span class="p">,</span> <span class="s1">&#39;en_gb&#39;</span><span class="p">,</span> <span class="s1">&#39;en_ie&#39;</span><span class="p">,</span> <span class="s1">&#39;en_my&#39;</span><span class="p">,</span> <span class="s1">&#39;en_nz&#39;</span><span class="p">,</span> <span class="s1">&#39;en_us&#39;</span><span class="p">,</span> <span class="s1">&#39;es_ad&#39;</span><span class="p">,</span> <span class="s1">&#39;es_ar&#39;</span><span class="p">,</span>
<span class="s1">&#39;es_cl&#39;</span><span class="p">,</span> <span class="s1">&#39;es_co&#39;</span><span class="p">,</span> <span class="s1">&#39;es_es&#39;</span><span class="p">,</span> <span class="s1">&#39;es_mx&#39;</span><span class="p">,</span> <span class="s1">&#39;es_pe&#39;</span><span class="p">,</span> <span class="s1">&#39;eu_es&#39;</span><span class="p">,</span> <span class="s1">&#39;eu_fr&#39;</span><span class="p">,</span> <span class="s1">&#39;fc_ca&#39;</span><span class="p">,</span>
<span class="s1">&#39;fr_ad&#39;</span><span class="p">,</span> <span class="s1">&#39;fr_be&#39;</span><span class="p">,</span> <span class="s1">&#39;fr_ca&#39;</span><span class="p">,</span> <span class="s1">&#39;fr_ch&#39;</span><span class="p">,</span> <span class="s1">&#39;fr_fr&#39;</span><span class="p">,</span> <span class="s1">&#39;it_ch&#39;</span><span class="p">,</span> <span class="s1">&#39;it_it&#39;</span><span class="p">,</span> <span class="s1">&#39;nl_be&#39;</span><span class="p">,</span>
<span class="s1">&#39;nl_nl&#39;</span><span class="p">,</span> <span class="s1">&#39;pt_ad&#39;</span><span class="p">,</span> <span class="s1">&#39;pt_pt&#39;</span><span class="p">,</span>
<span class="p">]</span>
<span class="c1"># fmt: on</span>
<span class="c1"># search-url</span>
<span class="n">api_url</span> <span class="o">=</span> <span class="s1">&#39;https://api.qwant.com/v3/search/&#39;</span>
<span class="sd">&quot;&quot;&quot;URL of Qwant&#39;s API (JSON)&quot;&quot;&quot;</span>
<span class="n">web_lite_url</span> <span class="o">=</span> <span class="s1">&#39;https://lite.qwant.com/&#39;</span>
<span class="sd">&quot;&quot;&quot;URL of Qwant-Lite (HTML)&quot;&quot;&quot;</span>
<div class="viewcode-block" id="request">
<a class="viewcode-back" href="../../../dev/engines/online/qwant.html#searx.engines.qwant.request">[docs]</a>
<span class="k">def</span> <span class="nf">request</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Qwant search request&quot;&quot;&quot;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">query</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">None</span>
<span class="n">q_locale</span> <span class="o">=</span> <span class="n">traits</span><span class="o">.</span><span class="n">get_region</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="s2">&quot;searxng_locale&quot;</span><span class="p">],</span> <span class="n">default</span><span class="o">=</span><span class="s1">&#39;en_US&#39;</span><span class="p">)</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">api_url</span> <span class="o">+</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">qwant_categ</span><span class="si">}</span><span class="s1">?&#39;</span>
<span class="n">args</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;q&#39;</span><span class="p">:</span> <span class="n">query</span><span class="p">}</span>
<span class="n">params</span><span class="p">[</span><span class="s1">&#39;raise_for_httperror&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">if</span> <span class="n">qwant_categ</span> <span class="o">==</span> <span class="s1">&#39;web-lite&#39;</span><span class="p">:</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">web_lite_url</span> <span class="o">+</span> <span class="s1">&#39;?&#39;</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;locale&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">q_locale</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;l&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">q_locale</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;s&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">&#39;safesearch&#39;</span><span class="p">]</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;p&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">&#39;pageno&#39;</span><span class="p">]</span>
<span class="n">params</span><span class="p">[</span><span class="s1">&#39;raise_for_httperror&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">elif</span> <span class="n">qwant_categ</span> <span class="o">==</span> <span class="s1">&#39;images&#39;</span><span class="p">:</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;locale&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">q_locale</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;safesearch&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">&#39;safesearch&#39;</span><span class="p">]</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;count&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">50</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;pageno&#39;</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">args</span><span class="p">[</span><span class="s1">&#39;count&#39;</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span> <span class="c1"># web, news, videos</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;locale&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">q_locale</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;safesearch&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">&#39;safesearch&#39;</span><span class="p">]</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;count&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">10</span>
<span class="n">args</span><span class="p">[</span><span class="s1">&#39;offset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="s1">&#39;pageno&#39;</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="n">args</span><span class="p">[</span><span class="s1">&#39;count&#39;</span><span class="p">]</span>
<span class="n">params</span><span class="p">[</span><span class="s1">&#39;url&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">url</span> <span class="o">+</span> <span class="n">urlencode</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
<span class="k">return</span> <span class="n">params</span></div>
<span class="k">def</span> <span class="nf">response</span><span class="p">(</span><span class="n">resp</span><span class="p">):</span>
<span class="k">if</span> <span class="n">qwant_categ</span> <span class="o">==</span> <span class="s1">&#39;web-lite&#39;</span><span class="p">:</span>
<span class="k">return</span> <span class="n">parse_web_lite</span><span class="p">(</span><span class="n">resp</span><span class="p">)</span>
<span class="k">return</span> <span class="n">parse_web_api</span><span class="p">(</span><span class="n">resp</span><span class="p">)</span>
<div class="viewcode-block" id="parse_web_lite">
<a class="viewcode-back" href="../../../dev/engines/online/qwant.html#searx.engines.qwant.parse_web_lite">[docs]</a>
<span class="k">def</span> <span class="nf">parse_web_lite</span><span class="p">(</span><span class="n">resp</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Parse results from Qwant-Lite&quot;&quot;&quot;</span>
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">dom</span> <span class="o">=</span> <span class="n">lxml</span><span class="o">.</span><span class="n">html</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">eval_xpath_list</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s1">&#39;//section/article&#39;</span><span class="p">):</span>
<span class="k">if</span> <span class="n">eval_xpath</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s2">&quot;./span[contains(@class, &#39;tooltip&#39;)]&quot;</span><span class="p">):</span>
<span class="c1"># ignore randomly interspersed advertising adds</span>
<span class="k">continue</span>
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">eval_xpath</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s2">&quot;./span[contains(@class, &#39;url partner&#39;)]&quot;</span><span class="p">)),</span>
<span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">eval_xpath</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s1">&#39;./h2/a&#39;</span><span class="p">)),</span>
<span class="s1">&#39;content&#39;</span><span class="p">:</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">eval_xpath</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s1">&#39;./p&#39;</span><span class="p">)),</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">results</span></div>
<div class="viewcode-block" id="parse_web_api">
<a class="viewcode-back" href="../../../dev/engines/online/qwant.html#searx.engines.qwant.parse_web_api">[docs]</a>
<span class="k">def</span> <span class="nf">parse_web_api</span><span class="p">(</span><span class="n">resp</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Parse results from Qwant&#39;s API&quot;&quot;&quot;</span>
<span class="c1"># pylint: disable=too-many-locals, too-many-branches, too-many-statements</span>
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># load JSON result</span>
<span class="n">search_results</span> <span class="o">=</span> <span class="n">loads</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">search_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="c1"># check for an API error</span>
<span class="k">if</span> <span class="n">search_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;status&#39;</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">&#39;success&#39;</span><span class="p">:</span>
<span class="n">error_code</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;error_code&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">error_code</span> <span class="o">==</span> <span class="mi">24</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">SearxEngineTooManyRequestsException</span><span class="p">()</span>
<span class="k">if</span> <span class="n">search_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;data&quot;</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;error_data&quot;</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;captchaUrl&quot;</span><span class="p">)</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">SearxEngineCaptchaException</span><span class="p">()</span>
<span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;,&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;message&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;unknown&#39;</span><span class="p">]))</span>
<span class="k">raise</span> <span class="n">SearxEngineAPIException</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">msg</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">error_code</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">)</span>
<span class="c1"># raise for other errors</span>
<span class="n">raise_for_httperror</span><span class="p">(</span><span class="n">resp</span><span class="p">)</span>
<span class="k">if</span> <span class="n">qwant_categ</span> <span class="o">==</span> <span class="s1">&#39;web&#39;</span><span class="p">:</span>
<span class="c1"># The WEB query contains a list named &#39;mainline&#39;. This list can contain</span>
<span class="c1"># different result types (e.g. mainline[0][&#39;type&#39;] returns type of the</span>
<span class="c1"># result items in mainline[0][&#39;items&#39;]</span>
<span class="n">mainline</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;result&#39;</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;items&#39;</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;mainline&#39;</span><span class="p">,</span> <span class="p">{})</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># Queries on News, Images and Videos do not have a list named &#39;mainline&#39;</span>
<span class="c1"># in the response. The result items are directly in the list</span>
<span class="c1"># result[&#39;items&#39;].</span>
<span class="n">mainline</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;result&#39;</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;items&#39;</span><span class="p">,</span> <span class="p">[])</span>
<span class="n">mainline</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">{</span><span class="s1">&#39;type&#39;</span><span class="p">:</span> <span class="n">qwant_categ</span><span class="p">,</span> <span class="s1">&#39;items&#39;</span><span class="p">:</span> <span class="n">mainline</span><span class="p">},</span>
<span class="p">]</span>
<span class="c1"># return empty array if there are no results</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">mainline</span><span class="p">:</span>
<span class="k">return</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">mainline</span><span class="p">:</span>
<span class="n">mainline_type</span> <span class="o">=</span> <span class="n">row</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">,</span> <span class="s1">&#39;web&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">mainline_type</span> <span class="o">!=</span> <span class="n">qwant_categ</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="n">mainline_type</span> <span class="o">==</span> <span class="s1">&#39;ads&#39;</span><span class="p">:</span>
<span class="c1"># ignore adds</span>
<span class="k">continue</span>
<span class="n">mainline_items</span> <span class="o">=</span> <span class="n">row</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;items&#39;</span><span class="p">,</span> <span class="p">[])</span>
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">mainline_items</span><span class="p">:</span>
<span class="n">title</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">res_url</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="k">if</span> <span class="n">mainline_type</span> <span class="o">==</span> <span class="s1">&#39;web&#39;</span><span class="p">:</span>
<span class="n">content</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;desc&#39;</span><span class="p">]</span>
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span>
<span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span>
<span class="s1">&#39;content&#39;</span><span class="p">:</span> <span class="n">content</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="k">elif</span> <span class="n">mainline_type</span> <span class="o">==</span> <span class="s1">&#39;news&#39;</span><span class="p">:</span>
<span class="n">pub_date</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">pub_date</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">pub_date</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">pub_date</span><span class="p">)</span>
<span class="n">news_media</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;media&#39;</span><span class="p">,</span> <span class="p">[])</span>
<span class="n">thumbnail</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">if</span> <span class="n">news_media</span><span class="p">:</span>
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">news_media</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;pict&#39;</span><span class="p">,</span> <span class="p">{})</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;url&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span>
<span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span>
<span class="s1">&#39;publishedDate&#39;</span><span class="p">:</span> <span class="n">pub_date</span><span class="p">,</span>
<span class="s1">&#39;thumbnail&#39;</span><span class="p">:</span> <span class="n">thumbnail</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="k">elif</span> <span class="n">mainline_type</span> <span class="o">==</span> <span class="s1">&#39;images&#39;</span><span class="p">:</span>
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;thumbnail&#39;</span><span class="p">]</span>
<span class="n">img_src</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;media&#39;</span><span class="p">]</span>
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span>
<span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span>
<span class="s1">&#39;template&#39;</span><span class="p">:</span> <span class="s1">&#39;images.html&#39;</span><span class="p">,</span>
<span class="s1">&#39;thumbnail_src&#39;</span><span class="p">:</span> <span class="n">thumbnail</span><span class="p">,</span>
<span class="s1">&#39;img_src&#39;</span><span class="p">:</span> <span class="n">img_src</span><span class="p">,</span>
<span class="s1">&#39;resolution&#39;</span><span class="p">:</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">item</span><span class="p">[</span><span class="s1">&#39;width&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2"> x </span><span class="si">{</span><span class="n">item</span><span class="p">[</span><span class="s1">&#39;height&#39;</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span>
<span class="s1">&#39;img_format&#39;</span><span class="p">:</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;thumb_type&#39;</span><span class="p">),</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="k">elif</span> <span class="n">mainline_type</span> <span class="o">==</span> <span class="s1">&#39;videos&#39;</span><span class="p">:</span>
<span class="c1"># some videos do not have a description: while qwant-video</span>
<span class="c1"># returns an empty string, such video from a qwant-web query</span>
<span class="c1"># miss the &#39;desc&#39; key.</span>
<span class="n">d</span><span class="p">,</span> <span class="n">s</span><span class="p">,</span> <span class="n">c</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;desc&#39;</span><span class="p">),</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;source&#39;</span><span class="p">),</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;channel&#39;</span><span class="p">)</span>
<span class="n">content_parts</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">if</span> <span class="n">d</span><span class="p">:</span>
<span class="n">content_parts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>
<span class="k">if</span> <span class="n">s</span><span class="p">:</span>
<span class="n">content_parts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2"> &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">gettext</span><span class="p">(</span><span class="s2">&quot;Source&quot;</span><span class="p">),</span> <span class="n">s</span><span class="p">))</span>
<span class="k">if</span> <span class="n">c</span><span class="p">:</span>
<span class="n">content_parts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2"> &quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">gettext</span><span class="p">(</span><span class="s2">&quot;Channel&quot;</span><span class="p">),</span> <span class="n">c</span><span class="p">))</span>
<span class="n">content</span> <span class="o">=</span> <span class="s1">&#39; // &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">content_parts</span><span class="p">)</span>
<span class="n">length</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;duration&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">length</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">length</span> <span class="o">=</span> <span class="n">timedelta</span><span class="p">(</span><span class="n">milliseconds</span><span class="o">=</span><span class="n">length</span><span class="p">)</span>
<span class="n">pub_date</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span>
<span class="k">if</span> <span class="n">pub_date</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">pub_date</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">pub_date</span><span class="p">)</span>
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">item</span><span class="p">[</span><span class="s1">&#39;thumbnail&#39;</span><span class="p">]</span>
<span class="c1"># from some locations (DE and others?) the s2 link do</span>
<span class="c1"># response a &#39;Please wait ..&#39; but does not deliver the thumbnail</span>
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">thumbnail</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;https://s2.qwant.com&#39;</span><span class="p">,</span> <span class="s1">&#39;https://s1.qwant.com&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
<span class="p">{</span>
<span class="s1">&#39;title&#39;</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span>
<span class="s1">&#39;url&#39;</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span>
<span class="s1">&#39;content&#39;</span><span class="p">:</span> <span class="n">content</span><span class="p">,</span>
<span class="s1">&#39;iframe_src&#39;</span><span class="p">:</span> <span class="n">get_embeded_stream_url</span><span class="p">(</span><span class="n">res_url</span><span class="p">),</span>
<span class="s1">&#39;publishedDate&#39;</span><span class="p">:</span> <span class="n">pub_date</span><span class="p">,</span>
<span class="s1">&#39;thumbnail&#39;</span><span class="p">:</span> <span class="n">thumbnail</span><span class="p">,</span>
<span class="s1">&#39;template&#39;</span><span class="p">:</span> <span class="s1">&#39;videos.html&#39;</span><span class="p">,</span>
<span class="s1">&#39;length&#39;</span><span class="p">:</span> <span class="n">length</span><span class="p">,</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="k">return</span> <span class="n">results</span></div>
<span class="k">def</span> <span class="nf">fetch_traits</span><span class="p">(</span><span class="n">engine_traits</span><span class="p">:</span> <span class="n">EngineTraits</span><span class="p">):</span>
<span class="c1"># pylint: disable=import-outside-toplevel</span>
<span class="kn">from</span> <span class="nn">searx</span> <span class="kn">import</span> <span class="n">network</span>
<span class="kn">from</span> <span class="nn">searx.locales</span> <span class="kn">import</span> <span class="n">region_tag</span>
<span class="kn">from</span> <span class="nn">searx.utils</span> <span class="kn">import</span> <span class="n">extr</span>
<span class="n">resp</span> <span class="o">=</span> <span class="n">network</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">about</span><span class="p">[</span><span class="s1">&#39;website&#39;</span><span class="p">])</span>
<span class="n">json_string</span> <span class="o">=</span> <span class="n">extr</span><span class="p">(</span><span class="n">resp</span><span class="o">.</span><span class="n">text</span><span class="p">,</span> <span class="s1">&#39;INITIAL_PROPS = &#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;/script&gt;&#39;</span><span class="p">)</span>
<span class="n">q_initial_props</span> <span class="o">=</span> <span class="n">loads</span><span class="p">(</span><span class="n">json_string</span><span class="p">)</span>
<span class="n">q_locales</span> <span class="o">=</span> <span class="n">q_initial_props</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;locales&#39;</span><span class="p">)</span>
<span class="n">eng_tag_list</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<span class="k">for</span> <span class="n">country</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">q_locales</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">for</span> <span class="n">lang</span> <span class="ow">in</span> <span class="n">v</span><span class="p">[</span><span class="s1">&#39;langs&#39;</span><span class="p">]:</span>
<span class="n">_locale</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">{lang}</span><span class="s2">_</span><span class="si">{country}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">lang</span><span class="o">=</span><span class="n">lang</span><span class="p">,</span> <span class="n">country</span><span class="o">=</span><span class="n">country</span><span class="p">)</span>
<span class="k">if</span> <span class="n">qwant_categ</span> <span class="o">==</span> <span class="s1">&#39;news&#39;</span> <span class="ow">and</span> <span class="n">_locale</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">qwant_news_locales</span><span class="p">:</span>
<span class="c1"># qwant-news does not support all locales from qwant-web:</span>
<span class="k">continue</span>
<span class="n">eng_tag_list</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">_locale</span><span class="p">)</span>
<span class="k">for</span> <span class="n">eng_tag</span> <span class="ow">in</span> <span class="n">eng_tag_list</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">sxng_tag</span> <span class="o">=</span> <span class="n">region_tag</span><span class="p">(</span><span class="n">babel</span><span class="o">.</span><span class="n">Locale</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="n">eng_tag</span><span class="p">,</span> <span class="n">sep</span><span class="o">=</span><span class="s1">&#39;_&#39;</span><span class="p">))</span>
<span class="k">except</span> <span class="n">babel</span><span class="o">.</span><span class="n">UnknownLocaleError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;ERROR: can&#39;t determine babel locale of quant&#39;s locale </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">eng_tag</span><span class="p">)</span>
<span class="k">continue</span>
<span class="n">conflict</span> <span class="o">=</span> <span class="n">engine_traits</span><span class="o">.</span><span class="n">regions</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sxng_tag</span><span class="p">)</span>
<span class="k">if</span> <span class="n">conflict</span><span class="p">:</span>
<span class="k">if</span> <span class="n">conflict</span> <span class="o">!=</span> <span class="n">eng_tag</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;CONFLICT: babel </span><span class="si">%s</span><span class="s2"> --&gt; </span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">sxng_tag</span><span class="p">,</span> <span class="n">conflict</span><span class="p">,</span> <span class="n">eng_tag</span><span class="p">))</span>
<span class="k">continue</span>
<span class="n">engine_traits</span><span class="o">.</span><span class="n">regions</span><span class="p">[</span><span class="n">sxng_tag</span><span class="p">]</span> <span class="o">=</span> <span class="n">eng_tag</span>
</pre></div>
<div class="clearer"></div>
</div>
</div>
</div>
<span id="sidebar-top"></span>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="../../../index.html">
<img class="logo" src="../../../_static/searxng-wordmark.svg" alt="Logo of SearXNG"/>
</a></p>
<h3><a href="../../../index.html">Table of Contents</a></h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../user/index.html">User information</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../own-instance.html">Why use a private instance?</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../admin/index.html">Administrator documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../dev/index.html">Developer documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../utils/index.html">DevOps tooling box</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../src/index.html">Source-Code</a></li>
</ul>
<h3>Project Links</h3>
<ul>
<li><a href="https://github.com/searxng/searxng/tree/master">Source</a>
<li><a href="https://github.com/searxng/searxng/wiki">Wiki</a>
<li><a href="https://searx.space">Public instances</a>
<li><a href="https://github.com/searxng/searxng/issues">Issue Tracker</a>
</ul><h3>Navigation</h3>
<ul>
<li><a href="../../../index.html">Overview</a>
<ul>
<li><a href="../../index.html">Module code</a>
<ul>
<li><a href="../engines.html">searx.engines</a>
</ul>
</li></ul>
</li>
</ul>
</li>
</ul>
<search 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>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer" role="contentinfo">
&#169; Copyright SearXNG team.
</div>
</body>
</html>