diff options
Diffstat (limited to '_modules/searx/engines/qwant.html')
-rw-r--r-- | _modules/searx/engines/qwant.html | 471 |
1 files changed, 471 insertions, 0 deletions
diff --git a/_modules/searx/engines/qwant.html b/_modules/searx/engines/qwant.html new file mode 100644 index 000000000..b08ad409a --- /dev/null +++ b/_modules/searx/engines/qwant.html @@ -0,0 +1,471 @@ +<!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 — SearXNG Documentation (2025.1.6+6dab7fe78)</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" /> + <script src="../../../_static/documentation_options.js?v=ef740023"></script> + <script src="../../../_static/doctools.js?v=9a2dae69"></script> + <script src="../../../_static/sphinx_highlight.js?v=dc90522c"></script> + <script data-project="searxng" data-version="2025.1.6+6dab7fe78" 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 (2025.1.6+6dab7fe78)</a> »</li> + <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> »</li> + <li class="nav-item nav-item-2"><a href="../engines.html" accesskey="U">searx.engines</a> »</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">"""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'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"><settings engine>`:</span> + +<span class="sd">.. code:: yaml</span> + +<span class="sd"> - name: qwant</span> +<span class="sd"> qwant_categ: web-lite # alternatively use 'web'</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">"""</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">"website"</span><span class="p">:</span> <span class="s1">'https://www.qwant.com/'</span><span class="p">,</span> + <span class="s2">"wikidata_id"</span><span class="p">:</span> <span class="s1">'Q14657870'</span><span class="p">,</span> + <span class="s2">"official_api_documentation"</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span> + <span class="s2">"use_official_api"</span><span class="p">:</span> <span class="kc">True</span><span class="p">,</span> + <span class="s2">"require_api_key"</span><span class="p">:</span> <span class="kc">False</span><span class="p">,</span> + <span class="s2">"results"</span><span class="p">:</span> <span class="s1">'JSON'</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">"""5 pages maximum (``&p=5``): Trying to do more just results in an improper</span> +<span class="sd">redirect"""</span> + +<span class="n">qwant_categ</span> <span class="o">=</span> <span class="kc">None</span> +<span class="sd">"""One of ``web-lite`` (or ``web``), ``news``, ``images`` or ``videos``"""</span> + +<span class="n">safesearch</span> <span class="o">=</span> <span class="kc">True</span> +<span class="c1"># safe_search_map = {0: '&safesearch=0', 1: '&safesearch=1', 2: '&safesearch=2'}</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">'ca_ad'</span><span class="p">,</span> <span class="s1">'ca_es'</span><span class="p">,</span> <span class="s1">'ca_fr'</span><span class="p">,</span> <span class="s1">'co_fr'</span><span class="p">,</span> <span class="s1">'de_at'</span><span class="p">,</span> <span class="s1">'de_ch'</span><span class="p">,</span> <span class="s1">'de_de'</span><span class="p">,</span> <span class="s1">'en_au'</span><span class="p">,</span> + <span class="s1">'en_ca'</span><span class="p">,</span> <span class="s1">'en_gb'</span><span class="p">,</span> <span class="s1">'en_ie'</span><span class="p">,</span> <span class="s1">'en_my'</span><span class="p">,</span> <span class="s1">'en_nz'</span><span class="p">,</span> <span class="s1">'en_us'</span><span class="p">,</span> <span class="s1">'es_ad'</span><span class="p">,</span> <span class="s1">'es_ar'</span><span class="p">,</span> + <span class="s1">'es_cl'</span><span class="p">,</span> <span class="s1">'es_co'</span><span class="p">,</span> <span class="s1">'es_es'</span><span class="p">,</span> <span class="s1">'es_mx'</span><span class="p">,</span> <span class="s1">'es_pe'</span><span class="p">,</span> <span class="s1">'eu_es'</span><span class="p">,</span> <span class="s1">'eu_fr'</span><span class="p">,</span> <span class="s1">'fc_ca'</span><span class="p">,</span> + <span class="s1">'fr_ad'</span><span class="p">,</span> <span class="s1">'fr_be'</span><span class="p">,</span> <span class="s1">'fr_ca'</span><span class="p">,</span> <span class="s1">'fr_ch'</span><span class="p">,</span> <span class="s1">'fr_fr'</span><span class="p">,</span> <span class="s1">'it_ch'</span><span class="p">,</span> <span class="s1">'it_it'</span><span class="p">,</span> <span class="s1">'nl_be'</span><span class="p">,</span> + <span class="s1">'nl_nl'</span><span class="p">,</span> <span class="s1">'pt_ad'</span><span class="p">,</span> <span class="s1">'pt_pt'</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">'https://api.qwant.com/v3/search/'</span> +<span class="sd">"""URL of Qwant's API (JSON)"""</span> + +<span class="n">web_lite_url</span> <span class="o">=</span> <span class="s1">'https://lite.qwant.com/'</span> +<span class="sd">"""URL of Qwant-Lite (HTML)"""</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">"""Qwant search request"""</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">"searxng_locale"</span><span class="p">],</span> <span class="n">default</span><span class="o">=</span><span class="s1">'en_US'</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">'</span><span class="si">{</span><span class="n">qwant_categ</span><span class="si">}</span><span class="s1">?'</span> + <span class="n">args</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'q'</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">'raise_for_httperror'</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">'web-lite'</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">'?'</span> + <span class="n">args</span><span class="p">[</span><span class="s1">'locale'</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">'l'</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">'_'</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">'s'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">'safesearch'</span><span class="p">]</span> + <span class="n">args</span><span class="p">[</span><span class="s1">'p'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">'pageno'</span><span class="p">]</span> + + <span class="n">params</span><span class="p">[</span><span class="s1">'raise_for_httperror'</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">'images'</span><span class="p">:</span> + + <span class="n">args</span><span class="p">[</span><span class="s1">'locale'</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">'safesearch'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">'safesearch'</span><span class="p">]</span> + <span class="n">args</span><span class="p">[</span><span class="s1">'count'</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">'offset'</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">'pageno'</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">'count'</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">'locale'</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">'safesearch'</span><span class="p">]</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s1">'safesearch'</span><span class="p">]</span> + <span class="n">args</span><span class="p">[</span><span class="s1">'count'</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">'offset'</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">'pageno'</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">'count'</span><span class="p">]</span> + + <span class="n">params</span><span class="p">[</span><span class="s1">'url'</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">'web-lite'</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">"""Parse results from Qwant-Lite"""</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">'//section/article'</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">"./span[contains(@class, 'tooltip')]"</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">'url'</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">"./span[contains(@class, 'url partner')]"</span><span class="p">)),</span> + <span class="s1">'title'</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">'./h2/a'</span><span class="p">)),</span> + <span class="s1">'content'</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">'./p'</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">"""Parse results from Qwant's API"""</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">'data'</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">'status'</span><span class="p">)</span> <span class="o">!=</span> <span class="s1">'success'</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">'error_code'</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">"data"</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">"error_data"</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">"captchaUrl"</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">","</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">'message'</span><span class="p">,</span> <span class="p">[</span><span class="s1">'unknown'</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">"</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">)"</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">'web'</span><span class="p">:</span> + <span class="c1"># The WEB query contains a list named 'mainline'. This list can contain</span> + <span class="c1"># different result types (e.g. mainline[0]['type'] returns type of the</span> + <span class="c1"># result items in mainline[0]['items']</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">'result'</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">'items'</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">'mainline'</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 'mainline'</span> + <span class="c1"># in the response. The result items are directly in the list</span> + <span class="c1"># result['items'].</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">'result'</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">'items'</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">'type'</span><span class="p">:</span> <span class="n">qwant_categ</span><span class="p">,</span> <span class="s1">'items'</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">'type'</span><span class="p">,</span> <span class="s1">'web'</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">'ads'</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">'items'</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">'title'</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">'url'</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">'web'</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">'desc'</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">'title'</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span> + <span class="s1">'url'</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span> + <span class="s1">'content'</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">'news'</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">'date'</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">'media'</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">'pict'</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">'url'</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">'title'</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span> + <span class="s1">'url'</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span> + <span class="s1">'publishedDate'</span><span class="p">:</span> <span class="n">pub_date</span><span class="p">,</span> + <span class="s1">'thumbnail'</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">'images'</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">'thumbnail'</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">'media'</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">'title'</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span> + <span class="s1">'url'</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span> + <span class="s1">'template'</span><span class="p">:</span> <span class="s1">'images.html'</span><span class="p">,</span> + <span class="s1">'thumbnail_src'</span><span class="p">:</span> <span class="n">thumbnail</span><span class="p">,</span> + <span class="s1">'img_src'</span><span class="p">:</span> <span class="n">img_src</span><span class="p">,</span> + <span class="s1">'resolution'</span><span class="p">:</span> <span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">item</span><span class="p">[</span><span class="s1">'width'</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">'height'</span><span class="p">]</span><span class="si">}</span><span class="s2">"</span><span class="p">,</span> + <span class="s1">'img_format'</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">'thumb_type'</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">'videos'</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 'desc' 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">'desc'</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">'source'</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">'channel'</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">"</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2"> "</span> <span class="o">%</span> <span class="p">(</span><span class="n">gettext</span><span class="p">(</span><span class="s2">"Source"</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">"</span><span class="si">%s</span><span class="s2">: </span><span class="si">%s</span><span class="s2"> "</span> <span class="o">%</span> <span class="p">(</span><span class="n">gettext</span><span class="p">(</span><span class="s2">"Channel"</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">' // '</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">'duration'</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">'date'</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">'thumbnail'</span><span class="p">]</span> + <span class="c1"># from some locations (DE and others?) the s2 link do</span> + <span class="c1"># response a 'Please wait ..' 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">'https://s2.qwant.com'</span><span class="p">,</span> <span class="s1">'https://s1.qwant.com'</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">'title'</span><span class="p">:</span> <span class="n">title</span><span class="p">,</span> + <span class="s1">'url'</span><span class="p">:</span> <span class="n">res_url</span><span class="p">,</span> + <span class="s1">'content'</span><span class="p">:</span> <span class="n">content</span><span class="p">,</span> + <span class="s1">'iframe_src'</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">'publishedDate'</span><span class="p">:</span> <span class="n">pub_date</span><span class="p">,</span> + <span class="s1">'thumbnail'</span><span class="p">:</span> <span class="n">thumbnail</span><span class="p">,</span> + <span class="s1">'template'</span><span class="p">:</span> <span class="s1">'videos.html'</span><span class="p">,</span> + <span class="s1">'length'</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">'website'</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">'INITIAL_PROPS = '</span><span class="p">,</span> <span class="s1">'</script>'</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">'locales'</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">'langs'</span><span class="p">]:</span> + <span class="n">_locale</span> <span class="o">=</span> <span class="s2">"</span><span class="si">{lang}</span><span class="s2">_</span><span class="si">{country}</span><span class="s2">"</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">'news'</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">'_'</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">"ERROR: can't determine babel locale of quant's locale </span><span class="si">%s</span><span class="s2">"</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">"CONFLICT: babel </span><span class="si">%s</span><span class="s2"> --> </span><span class="si">%s</span><span class="s2">, </span><span class="si">%s</span><span class="s2">"</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"> + © Copyright SearXNG team. + </div> + </body> +</html>
\ No newline at end of file |