mirror of https://github.com/searxng/searxng.git
619 lines
60 KiB
HTML
619 lines
60 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.google — 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> »</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.google</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<h1>Source code for searx.engines.google</h1><div class="highlight"><pre>
|
|
<span></span><span class="c1"># SPDX-License-Identifier: AGPL-3.0-or-later</span>
|
|
<span class="sd">"""This is the implementation of the Google WEB engine. Some of this</span>
|
|
<span class="sd">implementations (manly the :py:obj:`get_google_info`) are shared by other</span>
|
|
<span class="sd">engines:</span>
|
|
|
|
<span class="sd">- :ref:`google images engine`</span>
|
|
<span class="sd">- :ref:`google news engine`</span>
|
|
<span class="sd">- :ref:`google videos engine`</span>
|
|
<span class="sd">- :ref:`google scholar engine`</span>
|
|
<span class="sd">- :ref:`google autocomplete`</span>
|
|
|
|
<span class="sd">"""</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">TYPE_CHECKING</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">re</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">lxml</span> <span class="kn">import</span> <span class="n">html</span>
|
|
<span class="kn">import</span> <span class="nn">babel</span>
|
|
<span class="kn">import</span> <span class="nn">babel.core</span>
|
|
<span class="kn">import</span> <span class="nn">babel.languages</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">searx.utils</span> <span class="kn">import</span> <span class="n">extract_text</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">eval_xpath_getindex</span>
|
|
<span class="kn">from</span> <span class="nn">searx.locales</span> <span class="kn">import</span> <span class="n">language_tag</span><span class="p">,</span> <span class="n">region_tag</span><span class="p">,</span> <span class="n">get_official_locales</span>
|
|
<span class="kn">from</span> <span class="nn">searx.network</span> <span class="kn">import</span> <span class="n">get</span> <span class="c1"># see https://github.com/searxng/searxng/issues/762</span>
|
|
<span class="kn">from</span> <span class="nn">searx.exceptions</span> <span class="kn">import</span> <span class="n">SearxEngineCaptchaException</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="k">if</span> <span class="n">TYPE_CHECKING</span><span class="p">:</span>
|
|
<span class="kn">import</span> <span class="nn">logging</span>
|
|
|
|
<span class="n">logger</span><span class="p">:</span> <span class="n">logging</span><span class="o">.</span><span class="n">Logger</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.google.com'</span><span class="p">,</span>
|
|
<span class="s2">"wikidata_id"</span><span class="p">:</span> <span class="s1">'Q9366'</span><span class="p">,</span>
|
|
<span class="s2">"official_api_documentation"</span><span class="p">:</span> <span class="s1">'https://developers.google.com/custom-search/'</span><span class="p">,</span>
|
|
<span class="s2">"use_official_api"</span><span class="p">:</span> <span class="kc">False</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">'HTML'</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="s1">'general'</span><span class="p">,</span> <span class="s1">'web'</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">50</span>
|
|
<span class="n">time_range_support</span> <span class="o">=</span> <span class="kc">True</span>
|
|
<span class="n">safesearch</span> <span class="o">=</span> <span class="kc">True</span>
|
|
|
|
<span class="n">time_range_dict</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'day'</span><span class="p">:</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'week'</span><span class="p">:</span> <span class="s1">'w'</span><span class="p">,</span> <span class="s1">'month'</span><span class="p">:</span> <span class="s1">'m'</span><span class="p">,</span> <span class="s1">'year'</span><span class="p">:</span> <span class="s1">'y'</span><span class="p">}</span>
|
|
|
|
<span class="c1"># Filter results. 0: None, 1: Moderate, 2: Strict</span>
|
|
<span class="n">filter_mapping</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">:</span> <span class="s1">'off'</span><span class="p">,</span> <span class="mi">1</span><span class="p">:</span> <span class="s1">'medium'</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span> <span class="s1">'high'</span><span class="p">}</span>
|
|
|
|
<span class="c1"># specific xpath variables</span>
|
|
<span class="c1"># ------------------------</span>
|
|
|
|
<span class="n">results_xpath</span> <span class="o">=</span> <span class="s1">'.//div[contains(@jscontroller, "SC7lYd")]'</span>
|
|
<span class="n">title_xpath</span> <span class="o">=</span> <span class="s1">'.//a/h3[1]'</span>
|
|
<span class="n">href_xpath</span> <span class="o">=</span> <span class="s1">'.//a[h3]/@href'</span>
|
|
<span class="n">content_xpath</span> <span class="o">=</span> <span class="s1">'.//div[contains(@data-sncf, "1")]'</span>
|
|
|
|
<span class="c1"># Suggestions are links placed in a *card-section*, we extract only the text</span>
|
|
<span class="c1"># from the links not the links itself.</span>
|
|
<span class="n">suggestion_xpath</span> <span class="o">=</span> <span class="s1">'//div[contains(@class, "EIaa9b")]//a'</span>
|
|
|
|
<span class="c1"># UI_ASYNC = 'use_ac:true,_fmt:html' # returns a HTTP 500 when user search for</span>
|
|
<span class="c1"># # celebrities like '!google natasha allegri'</span>
|
|
<span class="c1"># # or '!google chris evans'</span>
|
|
<span class="n">UI_ASYNC</span> <span class="o">=</span> <span class="s1">'use_ac:true,_fmt:prog'</span>
|
|
<span class="sd">"""Format of the response from UI's async request."""</span>
|
|
|
|
|
|
<div class="viewcode-block" id="get_google_info">
|
|
<a class="viewcode-back" href="../../../dev/engines/online/google.html#searx.engines.google.get_google_info">[docs]</a>
|
|
<span class="k">def</span> <span class="nf">get_google_info</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="n">eng_traits</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Composing various (language) properties for the google engines (:ref:`google</span>
|
|
<span class="sd"> API`).</span>
|
|
|
|
<span class="sd"> This function is called by the various google engines (:ref:`google web</span>
|
|
<span class="sd"> engine`, :ref:`google images engine`, :ref:`google news engine` and</span>
|
|
<span class="sd"> :ref:`google videos engine`).</span>
|
|
|
|
<span class="sd"> :param dict param: Request parameters of the engine. At least</span>
|
|
<span class="sd"> a ``searxng_locale`` key should be in the dictionary.</span>
|
|
|
|
<span class="sd"> :param eng_traits: Engine's traits fetched from google preferences</span>
|
|
<span class="sd"> (:py:obj:`searx.enginelib.traits.EngineTraits`)</span>
|
|
|
|
<span class="sd"> :rtype: dict</span>
|
|
<span class="sd"> :returns:</span>
|
|
<span class="sd"> Py-Dictionary with the key/value pairs:</span>
|
|
|
|
<span class="sd"> language:</span>
|
|
<span class="sd"> The language code that is used by google (e.g. ``lang_en`` or</span>
|
|
<span class="sd"> ``lang_zh-TW``)</span>
|
|
|
|
<span class="sd"> country:</span>
|
|
<span class="sd"> The country code that is used by google (e.g. ``US`` or ``TW``)</span>
|
|
|
|
<span class="sd"> locale:</span>
|
|
<span class="sd"> A instance of :py:obj:`babel.core.Locale` build from the</span>
|
|
<span class="sd"> ``searxng_locale`` value.</span>
|
|
|
|
<span class="sd"> subdomain:</span>
|
|
<span class="sd"> Google subdomain :py:obj:`google_domains` that fits to the country</span>
|
|
<span class="sd"> code.</span>
|
|
|
|
<span class="sd"> params:</span>
|
|
<span class="sd"> Py-Dictionary with additional request arguments (can be passed to</span>
|
|
<span class="sd"> :py:func:`urllib.parse.urlencode`).</span>
|
|
|
|
<span class="sd"> - ``hl`` parameter: specifies the interface language of user interface.</span>
|
|
<span class="sd"> - ``lr`` parameter: restricts search results to documents written in</span>
|
|
<span class="sd"> a particular language.</span>
|
|
<span class="sd"> - ``cr`` parameter: restricts search results to documents</span>
|
|
<span class="sd"> originating in a particular country.</span>
|
|
<span class="sd"> - ``ie`` parameter: sets the character encoding scheme that should</span>
|
|
<span class="sd"> be used to interpret the query string ('utf8').</span>
|
|
<span class="sd"> - ``oe`` parameter: sets the character encoding scheme that should</span>
|
|
<span class="sd"> be used to decode the XML result ('utf8').</span>
|
|
|
|
<span class="sd"> headers:</span>
|
|
<span class="sd"> Py-Dictionary with additional HTTP headers (can be passed to</span>
|
|
<span class="sd"> request's headers)</span>
|
|
|
|
<span class="sd"> - ``Accept: '*/*``</span>
|
|
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">ret_val</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="s1">'language'</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
|
|
<span class="s1">'country'</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
|
|
<span class="s1">'subdomain'</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
|
|
<span class="s1">'params'</span><span class="p">:</span> <span class="p">{},</span>
|
|
<span class="s1">'headers'</span><span class="p">:</span> <span class="p">{},</span>
|
|
<span class="s1">'cookies'</span><span class="p">:</span> <span class="p">{},</span>
|
|
<span class="s1">'locale'</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="n">sxng_locale</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'searxng_locale'</span><span class="p">,</span> <span class="s1">'all'</span><span class="p">)</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">locale</span> <span class="o">=</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">sxng_locale</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">core</span><span class="o">.</span><span class="n">UnknownLocaleError</span><span class="p">:</span>
|
|
<span class="n">locale</span> <span class="o">=</span> <span class="kc">None</span>
|
|
|
|
<span class="n">eng_lang</span> <span class="o">=</span> <span class="n">eng_traits</span><span class="o">.</span><span class="n">get_language</span><span class="p">(</span><span class="n">sxng_locale</span><span class="p">,</span> <span class="s1">'lang_en'</span><span class="p">)</span>
|
|
<span class="n">lang_code</span> <span class="o">=</span> <span class="n">eng_lang</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'_'</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># lang_zh-TW --> zh-TW / lang_en --> en</span>
|
|
<span class="n">country</span> <span class="o">=</span> <span class="n">eng_traits</span><span class="o">.</span><span class="n">get_region</span><span class="p">(</span><span class="n">sxng_locale</span><span class="p">,</span> <span class="n">eng_traits</span><span class="o">.</span><span class="n">all_locale</span><span class="p">)</span>
|
|
|
|
<span class="c1"># Test zh_hans & zh_hant --> in the topmost links in the result list of list</span>
|
|
<span class="c1"># TW and HK you should a find wiktionary.org zh_hant link. In the result</span>
|
|
<span class="c1"># list of zh-CN should not be no hant link instead you should find</span>
|
|
<span class="c1"># zh.m.wikipedia.org/zh somewhere in the top.</span>
|
|
|
|
<span class="c1"># '!go 日 :zh-TW' --> https://zh.m.wiktionary.org/zh-hant/%E6%97%A5</span>
|
|
<span class="c1"># '!go 日 :zh-CN' --> https://zh.m.wikipedia.org/zh/%E6%97%A5</span>
|
|
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'language'</span><span class="p">]</span> <span class="o">=</span> <span class="n">eng_lang</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'country'</span><span class="p">]</span> <span class="o">=</span> <span class="n">country</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'locale'</span><span class="p">]</span> <span class="o">=</span> <span class="n">locale</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'subdomain'</span><span class="p">]</span> <span class="o">=</span> <span class="n">eng_traits</span><span class="o">.</span><span class="n">custom</span><span class="p">[</span><span class="s1">'supported_domains'</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">country</span><span class="o">.</span><span class="n">upper</span><span class="p">(),</span> <span class="s1">'www.google.com'</span><span class="p">)</span>
|
|
|
|
<span class="c1"># hl parameter:</span>
|
|
<span class="c1"># The hl parameter specifies the interface language (host language) of</span>
|
|
<span class="c1"># your user interface. To improve the performance and the quality of your</span>
|
|
<span class="c1"># search results, you are strongly encouraged to set this parameter</span>
|
|
<span class="c1"># explicitly.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#hlsp</span>
|
|
<span class="c1"># The Interface Language:</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results_appendices#interfaceLanguages</span>
|
|
|
|
<span class="c1"># https://github.com/searxng/searxng/issues/2515#issuecomment-1607150817</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'hl'</span><span class="p">]</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">'</span><span class="si">{</span><span class="n">lang_code</span><span class="si">}</span><span class="s1">-</span><span class="si">{</span><span class="n">country</span><span class="si">}</span><span class="s1">'</span>
|
|
|
|
<span class="c1"># lr parameter:</span>
|
|
<span class="c1"># The lr (language restrict) parameter restricts search results to</span>
|
|
<span class="c1"># documents written in a particular language.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#lrsp</span>
|
|
<span class="c1"># Language Collection Values:</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results_appendices#languageCollections</span>
|
|
<span class="c1">#</span>
|
|
<span class="c1"># To select 'all' languages an empty 'lr' value is used.</span>
|
|
<span class="c1">#</span>
|
|
<span class="c1"># Different to other google services, Google Scholar supports to select more</span>
|
|
<span class="c1"># than one language. The languages are separated by a pipe '|' (logical OR).</span>
|
|
<span class="c1"># By example: &lr=lang_zh-TW%7Clang_de selects articles written in</span>
|
|
<span class="c1"># traditional chinese OR german language.</span>
|
|
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'lr'</span><span class="p">]</span> <span class="o">=</span> <span class="n">eng_lang</span>
|
|
<span class="k">if</span> <span class="n">sxng_locale</span> <span class="o">==</span> <span class="s1">'all'</span><span class="p">:</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'lr'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">''</span>
|
|
|
|
<span class="c1"># cr parameter:</span>
|
|
<span class="c1"># The cr parameter restricts search results to documents originating in a</span>
|
|
<span class="c1"># particular country.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#crsp</span>
|
|
|
|
<span class="c1"># specify a region (country) only if a region is given in the selected</span>
|
|
<span class="c1"># locale --> https://github.com/searxng/searxng/issues/2672</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'cr'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">''</span>
|
|
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">sxng_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="o">></span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'cr'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'country'</span> <span class="o">+</span> <span class="n">country</span>
|
|
|
|
<span class="c1"># gl parameter: (mandatory by Google News)</span>
|
|
<span class="c1"># The gl parameter value is a two-letter country code. For WebSearch</span>
|
|
<span class="c1"># results, the gl parameter boosts search results whose country of origin</span>
|
|
<span class="c1"># matches the parameter value. See the Country Codes section for a list of</span>
|
|
<span class="c1"># valid values.</span>
|
|
<span class="c1"># Specifying a gl parameter value in WebSearch requests should improve the</span>
|
|
<span class="c1"># relevance of results. This is particularly true for international</span>
|
|
<span class="c1"># customers and, even more specifically, for customers in English-speaking</span>
|
|
<span class="c1"># countries other than the United States.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#glsp</span>
|
|
|
|
<span class="c1"># https://github.com/searxng/searxng/issues/2515#issuecomment-1606294635</span>
|
|
<span class="c1"># ret_val['params']['gl'] = country</span>
|
|
|
|
<span class="c1"># ie parameter:</span>
|
|
<span class="c1"># The ie parameter sets the character encoding scheme that should be used</span>
|
|
<span class="c1"># to interpret the query string. The default ie value is latin1.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#iesp</span>
|
|
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'ie'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'utf8'</span>
|
|
|
|
<span class="c1"># oe parameter:</span>
|
|
<span class="c1"># The oe parameter sets the character encoding scheme that should be used</span>
|
|
<span class="c1"># to decode the XML result. The default oe value is latin1.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#oesp</span>
|
|
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'params'</span><span class="p">][</span><span class="s1">'oe'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'utf8'</span>
|
|
|
|
<span class="c1"># num parameter:</span>
|
|
<span class="c1"># The num parameter identifies the number of search results to return.</span>
|
|
<span class="c1"># The default num value is 10, and the maximum value is 20. If you request</span>
|
|
<span class="c1"># more than 20 results, only 20 results will be returned.</span>
|
|
<span class="c1"># https://developers.google.com/custom-search/docs/xml_results#numsp</span>
|
|
|
|
<span class="c1"># HINT: seems to have no effect (tested in google WEB & Images)</span>
|
|
<span class="c1"># ret_val['params']['num'] = 20</span>
|
|
|
|
<span class="c1"># HTTP headers</span>
|
|
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'headers'</span><span class="p">][</span><span class="s1">'Accept'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'*/*'</span>
|
|
|
|
<span class="c1"># Cookies</span>
|
|
|
|
<span class="c1"># - https://github.com/searxng/searxng/pull/1679#issuecomment-1235432746</span>
|
|
<span class="c1"># - https://github.com/searxng/searxng/issues/1555</span>
|
|
<span class="n">ret_val</span><span class="p">[</span><span class="s1">'cookies'</span><span class="p">][</span><span class="s1">'CONSENT'</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"YES+"</span>
|
|
|
|
<span class="k">return</span> <span class="n">ret_val</span></div>
|
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">detect_google_sorry</span><span class="p">(</span><span class="n">resp</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">url</span><span class="o">.</span><span class="n">host</span> <span class="o">==</span> <span class="s1">'sorry.google.com'</span> <span class="ow">or</span> <span class="n">resp</span><span class="o">.</span><span class="n">url</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">'/sorry'</span><span class="p">):</span>
|
|
<span class="k">raise</span> <span class="n">SearxEngineCaptchaException</span><span class="p">()</span>
|
|
|
|
|
|
<div class="viewcode-block" id="request">
|
|
<a class="viewcode-back" href="../../../dev/engines/online/google.html#searx.engines.google.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">"""Google search request"""</span>
|
|
<span class="c1"># pylint: disable=line-too-long</span>
|
|
<span class="n">offset</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="mi">10</span>
|
|
<span class="n">google_info</span> <span class="o">=</span> <span class="n">get_google_info</span><span class="p">(</span><span class="n">params</span><span class="p">,</span> <span class="n">traits</span><span class="p">)</span>
|
|
|
|
<span class="c1"># https://www.google.de/search?q=corona&hl=de&lr=lang_de&start=0&tbs=qdr%3Ad&safe=medium</span>
|
|
<span class="n">query_url</span> <span class="o">=</span> <span class="p">(</span>
|
|
<span class="s1">'https://'</span>
|
|
<span class="o">+</span> <span class="n">google_info</span><span class="p">[</span><span class="s1">'subdomain'</span><span class="p">]</span>
|
|
<span class="o">+</span> <span class="s1">'/search'</span>
|
|
<span class="o">+</span> <span class="s2">"?"</span>
|
|
<span class="o">+</span> <span class="n">urlencode</span><span class="p">(</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="o">**</span><span class="n">google_info</span><span class="p">[</span><span class="s1">'params'</span><span class="p">],</span>
|
|
<span class="s1">'filter'</span><span class="p">:</span> <span class="s1">'0'</span><span class="p">,</span>
|
|
<span class="s1">'start'</span><span class="p">:</span> <span class="n">offset</span><span class="p">,</span>
|
|
<span class="c1"># 'vet': '12ahUKEwik3ZbIzfn7AhXMX_EDHbUDBh0QxK8CegQIARAC..i',</span>
|
|
<span class="c1"># 'ved': '2ahUKEwik3ZbIzfn7AhXMX_EDHbUDBh0Q_skCegQIARAG',</span>
|
|
<span class="c1"># 'cs' : 1,</span>
|
|
<span class="c1"># 'sa': 'N',</span>
|
|
<span class="c1"># 'yv': 3,</span>
|
|
<span class="c1"># 'prmd': 'vin',</span>
|
|
<span class="c1"># 'ei': 'GASaY6TxOcy_xc8PtYeY6AE',</span>
|
|
<span class="c1"># 'sa': 'N',</span>
|
|
<span class="c1"># 'sstk': 'AcOHfVkD7sWCSAheZi-0tx_09XDO55gTWY0JNq3_V26cNN-c8lfD45aZYPI8s_Bqp8s57AHz5pxchDtAGCA_cikAWSjy9kw3kgg'</span>
|
|
<span class="c1"># formally known as use_mobile_ui</span>
|
|
<span class="s1">'asearch'</span><span class="p">:</span> <span class="s1">'arc'</span><span class="p">,</span>
|
|
<span class="s1">'async'</span><span class="p">:</span> <span class="n">UI_ASYNC</span><span class="p">,</span>
|
|
<span class="p">}</span>
|
|
<span class="p">)</span>
|
|
<span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">params</span><span class="p">[</span><span class="s1">'time_range'</span><span class="p">]</span> <span class="ow">in</span> <span class="n">time_range_dict</span><span class="p">:</span>
|
|
<span class="n">query_url</span> <span class="o">+=</span> <span class="s1">'&'</span> <span class="o">+</span> <span class="n">urlencode</span><span class="p">({</span><span class="s1">'tbs'</span><span class="p">:</span> <span class="s1">'qdr:'</span> <span class="o">+</span> <span class="n">time_range_dict</span><span class="p">[</span><span class="n">params</span><span class="p">[</span><span class="s1">'time_range'</span><span class="p">]]})</span>
|
|
<span class="k">if</span> <span class="n">params</span><span class="p">[</span><span class="s1">'safesearch'</span><span class="p">]:</span>
|
|
<span class="n">query_url</span> <span class="o">+=</span> <span class="s1">'&'</span> <span class="o">+</span> <span class="n">urlencode</span><span class="p">({</span><span class="s1">'safe'</span><span class="p">:</span> <span class="n">filter_mapping</span><span class="p">[</span><span class="n">params</span><span class="p">[</span><span class="s1">'safesearch'</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">query_url</span>
|
|
|
|
<span class="n">params</span><span class="p">[</span><span class="s1">'cookies'</span><span class="p">]</span> <span class="o">=</span> <span class="n">google_info</span><span class="p">[</span><span class="s1">'cookies'</span><span class="p">]</span>
|
|
<span class="n">params</span><span class="p">[</span><span class="s1">'headers'</span><span class="p">]</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">google_info</span><span class="p">[</span><span class="s1">'headers'</span><span class="p">])</span>
|
|
<span class="k">return</span> <span class="n">params</span></div>
|
|
|
|
|
|
|
|
<span class="c1"># =26;[3,"dimg_ZNMiZPCqE4apxc8P3a2tuAQ_137"]a87;data:image/jpeg;base64,/9j/4AAQSkZJRgABA</span>
|
|
<span class="c1"># ...6T+9Nl4cnD+gr9OK8I56/tX3l86nWYw//2Q==26;</span>
|
|
<span class="n">RE_DATA_IMAGE</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">'"(dimg_[^"]*)"[^;]*;(data:image[^;]*;[^;]*);'</span><span class="p">)</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">_parse_data_images</span><span class="p">(</span><span class="n">dom</span><span class="p">):</span>
|
|
<span class="n">data_image_map</span> <span class="o">=</span> <span class="p">{}</span>
|
|
<span class="k">for</span> <span class="n">img_id</span><span class="p">,</span> <span class="n">data_image</span> <span class="ow">in</span> <span class="n">RE_DATA_IMAGE</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">dom</span><span class="o">.</span><span class="n">text_content</span><span class="p">()):</span>
|
|
<span class="n">end_pos</span> <span class="o">=</span> <span class="n">data_image</span><span class="o">.</span><span class="n">rfind</span><span class="p">(</span><span class="s1">'='</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">end_pos</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
|
|
<span class="n">data_image</span> <span class="o">=</span> <span class="n">data_image</span><span class="p">[:</span> <span class="n">end_pos</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
|
|
<span class="n">data_image_map</span><span class="p">[</span><span class="n">img_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">data_image</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'data:image objects --> </span><span class="si">%s</span><span class="s1">'</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">data_image_map</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>
|
|
<span class="k">return</span> <span class="n">data_image_map</span>
|
|
|
|
|
|
<div class="viewcode-block" id="response">
|
|
<a class="viewcode-back" href="../../../dev/engines/online/google.html#searx.engines.google.response">[docs]</a>
|
|
<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="w"> </span><span class="sd">"""Get response from google's search request"""</span>
|
|
<span class="c1"># pylint: disable=too-many-branches, too-many-statements</span>
|
|
<span class="n">detect_google_sorry</span><span class="p">(</span><span class="n">resp</span><span class="p">)</span>
|
|
|
|
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="c1"># convert the text to dom</span>
|
|
<span class="n">dom</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="n">data_image_map</span> <span class="o">=</span> <span class="n">_parse_data_images</span><span class="p">(</span><span class="n">dom</span><span class="p">)</span>
|
|
|
|
<span class="c1"># results --> answer</span>
|
|
<span class="n">answer_list</span> <span class="o">=</span> <span class="n">eval_xpath</span><span class="p">(</span><span class="n">dom</span><span class="p">,</span> <span class="s1">'//div[contains(@class, "LGOjhe")]'</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">answer_list</span><span class="p">:</span>
|
|
<span class="k">for</span> <span class="n">bubble</span> <span class="ow">in</span> <span class="n">eval_xpath</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="s1">'.//div[@class="nnFGuf"]'</span><span class="p">):</span>
|
|
<span class="n">bubble</span><span class="o">.</span><span class="n">drop_tree</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">'answer'</span><span class="p">:</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">item</span><span class="p">),</span>
|
|
<span class="s1">'url'</span><span class="p">:</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">'../..//a/@href'</span><span class="p">)</span> <span class="o">+</span> <span class="p">[</span><span class="kc">None</span><span class="p">])[</span><span class="mi">0</span><span class="p">],</span>
|
|
<span class="p">}</span>
|
|
<span class="p">)</span>
|
|
|
|
<span class="c1"># parse results</span>
|
|
|
|
<span class="k">for</span> <span class="n">result</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="n">results_xpath</span><span class="p">):</span> <span class="c1"># pylint: disable=too-many-nested-blocks</span>
|
|
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">title_tag</span> <span class="o">=</span> <span class="n">eval_xpath_getindex</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">title_xpath</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">title_tag</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="c1"># this not one of the common google results *section*</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'ignoring item from the result_xpath list: missing title'</span><span class="p">)</span>
|
|
<span class="k">continue</span>
|
|
<span class="n">title</span> <span class="o">=</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">title_tag</span><span class="p">)</span>
|
|
|
|
<span class="n">url</span> <span class="o">=</span> <span class="n">eval_xpath_getindex</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">href_xpath</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">url</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'ignoring item from the result_xpath list: missing url of title "</span><span class="si">%s</span><span class="s1">"'</span><span class="p">,</span> <span class="n">title</span><span class="p">)</span>
|
|
<span class="k">continue</span>
|
|
|
|
<span class="n">content_nodes</span> <span class="o">=</span> <span class="n">eval_xpath</span><span class="p">(</span><span class="n">result</span><span class="p">,</span> <span class="n">content_xpath</span><span class="p">)</span>
|
|
<span class="n">content</span> <span class="o">=</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">content_nodes</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">content</span><span class="p">:</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">'ignoring item from the result_xpath list: missing content of title "</span><span class="si">%s</span><span class="s1">"'</span><span class="p">,</span> <span class="n">title</span><span class="p">)</span>
|
|
<span class="k">continue</span>
|
|
|
|
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">content_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">xpath</span><span class="p">(</span><span class="s1">'.//img/@src'</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">thumbnail</span><span class="p">:</span>
|
|
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">thumbnail</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
|
<span class="k">if</span> <span class="n">thumbnail</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">'data:image'</span><span class="p">):</span>
|
|
<span class="n">img_id</span> <span class="o">=</span> <span class="n">content_nodes</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">xpath</span><span class="p">(</span><span class="s1">'.//img/@id'</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">img_id</span><span class="p">:</span>
|
|
<span class="n">thumbnail</span> <span class="o">=</span> <span class="n">data_image_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">img_id</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
|
|
<span class="k">else</span><span class="p">:</span>
|
|
<span class="n">thumbnail</span> <span class="o">=</span> <span class="kc">None</span>
|
|
|
|
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">'url'</span><span class="p">:</span> <span class="n">url</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">'content'</span><span class="p">:</span> <span class="n">content</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="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span> <span class="c1"># pylint: disable=broad-except</span>
|
|
<span class="n">logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">exc_info</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
|
<span class="k">continue</span>
|
|
|
|
<span class="c1"># parse suggestion</span>
|
|
<span class="k">for</span> <span class="n">suggestion</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="n">suggestion_xpath</span><span class="p">):</span>
|
|
<span class="c1"># append suggestion</span>
|
|
<span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s1">'suggestion'</span><span class="p">:</span> <span class="n">extract_text</span><span class="p">(</span><span class="n">suggestion</span><span class="p">)})</span>
|
|
|
|
<span class="c1"># return results</span>
|
|
<span class="k">return</span> <span class="n">results</span></div>
|
|
|
|
|
|
|
|
<span class="c1"># get supported languages from their site</span>
|
|
|
|
|
|
<span class="n">skip_countries</span> <span class="o">=</span> <span class="p">[</span>
|
|
<span class="c1"># official language of google-country not in google-languages</span>
|
|
<span class="s1">'AL'</span><span class="p">,</span> <span class="c1"># Albanien (sq)</span>
|
|
<span class="s1">'AZ'</span><span class="p">,</span> <span class="c1"># Aserbaidschan (az)</span>
|
|
<span class="s1">'BD'</span><span class="p">,</span> <span class="c1"># Bangladesch (bn)</span>
|
|
<span class="s1">'BN'</span><span class="p">,</span> <span class="c1"># Brunei Darussalam (ms)</span>
|
|
<span class="s1">'BT'</span><span class="p">,</span> <span class="c1"># Bhutan (dz)</span>
|
|
<span class="s1">'ET'</span><span class="p">,</span> <span class="c1"># Äthiopien (am)</span>
|
|
<span class="s1">'GE'</span><span class="p">,</span> <span class="c1"># Georgien (ka, os)</span>
|
|
<span class="s1">'GL'</span><span class="p">,</span> <span class="c1"># Grönland (kl)</span>
|
|
<span class="s1">'KH'</span><span class="p">,</span> <span class="c1"># Kambodscha (km)</span>
|
|
<span class="s1">'LA'</span><span class="p">,</span> <span class="c1"># Laos (lo)</span>
|
|
<span class="s1">'LK'</span><span class="p">,</span> <span class="c1"># Sri Lanka (si, ta)</span>
|
|
<span class="s1">'ME'</span><span class="p">,</span> <span class="c1"># Montenegro (sr)</span>
|
|
<span class="s1">'MK'</span><span class="p">,</span> <span class="c1"># Nordmazedonien (mk, sq)</span>
|
|
<span class="s1">'MM'</span><span class="p">,</span> <span class="c1"># Myanmar (my)</span>
|
|
<span class="s1">'MN'</span><span class="p">,</span> <span class="c1"># Mongolei (mn)</span>
|
|
<span class="s1">'MV'</span><span class="p">,</span> <span class="c1"># Malediven (dv) // dv_MV is unknown by babel</span>
|
|
<span class="s1">'MY'</span><span class="p">,</span> <span class="c1"># Malaysia (ms)</span>
|
|
<span class="s1">'NP'</span><span class="p">,</span> <span class="c1"># Nepal (ne)</span>
|
|
<span class="s1">'TJ'</span><span class="p">,</span> <span class="c1"># Tadschikistan (tg)</span>
|
|
<span class="s1">'TM'</span><span class="p">,</span> <span class="c1"># Turkmenistan (tk)</span>
|
|
<span class="s1">'UZ'</span><span class="p">,</span> <span class="c1"># Usbekistan (uz)</span>
|
|
<span class="p">]</span>
|
|
|
|
|
|
<div class="viewcode-block" id="fetch_traits">
|
|
<a class="viewcode-back" href="../../../dev/engines/online/google.html#searx.engines.google.fetch_traits">[docs]</a>
|
|
<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="n">add_domains</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Fetch languages from Google."""</span>
|
|
<span class="c1"># pylint: disable=import-outside-toplevel, too-many-branches</span>
|
|
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">custom</span><span class="p">[</span><span class="s1">'supported_domains'</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
|
|
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="s1">'https://www.google.com/preferences'</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span> <span class="c1"># type: ignore</span>
|
|
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Response from Google's preferences is not OK."</span><span class="p">)</span>
|
|
|
|
<span class="n">dom</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="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'<?xml version="1.0" encoding="UTF-8"?>'</span><span class="p">,</span> <span class="s1">''</span><span class="p">))</span>
|
|
|
|
<span class="c1"># supported language codes</span>
|
|
|
|
<span class="n">lang_map</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'no'</span><span class="p">:</span> <span class="s1">'nb'</span><span class="p">}</span>
|
|
<span class="k">for</span> <span class="n">x</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="s2">"//select[@name='hl']/option"</span><span class="p">):</span>
|
|
<span class="n">eng_lang</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"value"</span><span class="p">)</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="n">locale</span> <span class="o">=</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">lang_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">eng_lang</span><span class="p">,</span> <span class="n">eng_lang</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">"INFO: google UI language </span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s</span><span class="s2">) is unknown by babel"</span> <span class="o">%</span> <span class="p">(</span><span class="n">eng_lang</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">"("</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()))</span>
|
|
<span class="k">continue</span>
|
|
<span class="n">sxng_lang</span> <span class="o">=</span> <span class="n">language_tag</span><span class="p">(</span><span class="n">locale</span><span class="p">)</span>
|
|
|
|
<span class="n">conflict</span> <span class="o">=</span> <span class="n">engine_traits</span><span class="o">.</span><span class="n">languages</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">sxng_lang</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_lang</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_lang</span><span class="p">,</span> <span class="n">conflict</span><span class="p">,</span> <span class="n">eng_lang</span><span class="p">))</span>
|
|
<span class="k">continue</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">languages</span><span class="p">[</span><span class="n">sxng_lang</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'lang_'</span> <span class="o">+</span> <span class="n">eng_lang</span>
|
|
|
|
<span class="c1"># alias languages</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">languages</span><span class="p">[</span><span class="s1">'zh'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'lang_zh-CN'</span>
|
|
|
|
<span class="c1"># supported region codes</span>
|
|
|
|
<span class="k">for</span> <span class="n">x</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="s2">"//select[@name='gl']/option"</span><span class="p">):</span>
|
|
<span class="n">eng_country</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">"value"</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="n">eng_country</span> <span class="ow">in</span> <span class="n">skip_countries</span><span class="p">:</span>
|
|
<span class="k">continue</span>
|
|
<span class="k">if</span> <span class="n">eng_country</span> <span class="o">==</span> <span class="s1">'ZZ'</span><span class="p">:</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">all_locale</span> <span class="o">=</span> <span class="s1">'ZZ'</span>
|
|
<span class="k">continue</span>
|
|
|
|
<span class="n">sxng_locales</span> <span class="o">=</span> <span class="n">get_official_locales</span><span class="p">(</span><span class="n">eng_country</span><span class="p">,</span> <span class="n">engine_traits</span><span class="o">.</span><span class="n">languages</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">regional</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
|
|
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">sxng_locales</span><span class="p">:</span>
|
|
<span class="nb">print</span><span class="p">(</span><span class="s2">"ERROR: can't map from google country </span><span class="si">%s</span><span class="s2"> (</span><span class="si">%s</span><span class="s2">) to a babel region."</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'data-name'</span><span class="p">),</span> <span class="n">eng_country</span><span class="p">))</span>
|
|
<span class="k">continue</span>
|
|
|
|
<span class="k">for</span> <span class="n">sxng_locale</span> <span class="ow">in</span> <span class="n">sxng_locales</span><span class="p">:</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">regions</span><span class="p">[</span><span class="n">region_tag</span><span class="p">(</span><span class="n">sxng_locale</span><span class="p">)]</span> <span class="o">=</span> <span class="n">eng_country</span>
|
|
|
|
<span class="c1"># alias regions</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">regions</span><span class="p">[</span><span class="s1">'zh-CN'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'HK'</span>
|
|
|
|
<span class="c1"># supported domains</span>
|
|
|
|
<span class="k">if</span> <span class="n">add_domains</span><span class="p">:</span>
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="s1">'https://www.google.com/supported_domains'</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span> <span class="c1"># type: ignore</span>
|
|
<span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"Response from https://www.google.com/supported_domains is not OK."</span><span class="p">)</span>
|
|
|
|
<span class="k">for</span> <span class="n">domain</span> <span class="ow">in</span> <span class="n">resp</span><span class="o">.</span><span class="n">text</span><span class="o">.</span><span class="n">split</span><span class="p">():</span> <span class="c1"># type: ignore</span>
|
|
<span class="n">domain</span> <span class="o">=</span> <span class="n">domain</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="ow">not</span> <span class="n">domain</span> <span class="ow">or</span> <span class="n">domain</span> <span class="ow">in</span> <span class="p">[</span>
|
|
<span class="s1">'.google.com'</span><span class="p">,</span>
|
|
<span class="p">]:</span>
|
|
<span class="k">continue</span>
|
|
<span class="n">region</span> <span class="o">=</span> <span class="n">domain</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">'.'</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">upper</span><span class="p">()</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">custom</span><span class="p">[</span><span class="s1">'supported_domains'</span><span class="p">][</span><span class="n">region</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'www'</span> <span class="o">+</span> <span class="n">domain</span> <span class="c1"># type: ignore</span>
|
|
<span class="k">if</span> <span class="n">region</span> <span class="o">==</span> <span class="s1">'HK'</span><span class="p">:</span>
|
|
<span class="c1"># There is no google.cn, we use .com.hk for zh-CN</span>
|
|
<span class="n">engine_traits</span><span class="o">.</span><span class="n">custom</span><span class="p">[</span><span class="s1">'supported_domains'</span><span class="p">][</span><span class="s1">'CN'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'www'</span> <span class="o">+</span> <span class="n">domain</span> <span class="c1"># type: ignore</span></div>
|
|
|
|
</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> |