mirror of https://github.com/searxng/searxng.git
374 lines
40 KiB
HTML
374 lines
40 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.autocomplete — SearXNG Documentation (2024.11.22+b8f1a329d)</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=16d153d8"></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.11.22+b8f1a329d" 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.11.22+b8f1a329d)</a> »</li>
|
|
<li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> »</li>
|
|
<li class="nav-item nav-item-this"><a href="">searx.autocomplete</a></li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body" role="main">
|
|
|
|
<h1>Source code for searx.autocomplete</h1><div class="highlight"><pre>
|
|
<span></span><span class="c1"># SPDX-License-Identifier: AGPL-3.0-or-later</span>
|
|
<span class="sd">"""This module implements functions needed for the autocompleter.</span>
|
|
|
|
<span class="sd">"""</span>
|
|
<span class="c1"># pylint: disable=use-dict-literal</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">json</span>
|
|
<span class="kn">import</span> <span class="nn">html</span>
|
|
<span class="kn">from</span> <span class="nn">urllib.parse</span> <span class="kn">import</span> <span class="n">urlencode</span><span class="p">,</span> <span class="n">quote_plus</span>
|
|
|
|
<span class="kn">import</span> <span class="nn">lxml</span>
|
|
<span class="kn">from</span> <span class="nn">httpx</span> <span class="kn">import</span> <span class="n">HTTPError</span>
|
|
|
|
<span class="kn">from</span> <span class="nn">searx</span> <span class="kn">import</span> <span class="n">settings</span>
|
|
<span class="kn">from</span> <span class="nn">searx.engines</span> <span class="kn">import</span> <span class="p">(</span>
|
|
<span class="n">engines</span><span class="p">,</span>
|
|
<span class="n">google</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">get</span> <span class="k">as</span> <span class="n">http_get</span><span class="p">,</span> <span class="n">post</span> <span class="k">as</span> <span class="n">http_post</span>
|
|
<span class="kn">from</span> <span class="nn">searx.exceptions</span> <span class="kn">import</span> <span class="n">SearxEngineResponseException</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">update_kwargs</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="k">if</span> <span class="s1">'timeout'</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
|
|
<span class="n">kwargs</span><span class="p">[</span><span class="s1">'timeout'</span><span class="p">]</span> <span class="o">=</span> <span class="n">settings</span><span class="p">[</span><span class="s1">'outgoing'</span><span class="p">][</span><span class="s1">'request_timeout'</span><span class="p">]</span>
|
|
<span class="n">kwargs</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">def</span> <span class="nf">get</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="n">update_kwargs</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">http_get</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">post</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
|
|
<span class="n">update_kwargs</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">http_post</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">brave</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="c1"># brave search autocompleter</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://search.brave.com/api/suggest?'</span>
|
|
<span class="n">url</span> <span class="o">+=</span> <span class="n">urlencode</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">country</span> <span class="o">=</span> <span class="s1">'all'</span>
|
|
<span class="c1"># if lang in _brave:</span>
|
|
<span class="c1"># country = lang</span>
|
|
<span class="n">kwargs</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'cookies'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'country'</span><span class="p">:</span> <span class="n">country</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="n">url</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
|
|
|
|
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
|
|
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">data</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="n">item</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">results</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">dbpedia</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="c1"># dbpedia autocompleter, no HTTPS</span>
|
|
<span class="n">autocomplete_url</span> <span class="o">=</span> <span class="s1">'https://lookup.dbpedia.org/api/search.asmx/KeywordSearch?'</span>
|
|
|
|
<span class="n">response</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="n">autocomplete_url</span> <span class="o">+</span> <span class="n">urlencode</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">QueryString</span><span class="o">=</span><span class="n">query</span><span class="p">)))</span>
|
|
|
|
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="k">if</span> <span class="n">response</span><span class="o">.</span><span class="n">ok</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">etree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">content</span><span class="p">)</span>
|
|
<span class="n">results</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">xpath</span><span class="p">(</span><span class="s1">'//Result/Label//text()'</span><span class="p">)</span>
|
|
|
|
<span class="k">return</span> <span class="n">results</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">duckduckgo</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Autocomplete from DuckDuckGo. Supports DuckDuckGo's languages"""</span>
|
|
|
|
<span class="n">traits</span> <span class="o">=</span> <span class="n">engines</span><span class="p">[</span><span class="s1">'duckduckgo'</span><span class="p">]</span><span class="o">.</span><span class="n">traits</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="s1">'kl'</span><span class="p">:</span> <span class="n">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">traits</span><span class="o">.</span><span class="n">all_locale</span><span class="p">),</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://duckduckgo.com/ac/?type=list&'</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="n">resp</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
|
|
|
|
<span class="n">ret_val</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
|
|
<span class="n">j</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">j</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="o">=</span> <span class="n">j</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="k">return</span> <span class="n">ret_val</span>
|
|
|
|
|
|
<div class="viewcode-block" id="google_complete">
|
|
<a class="viewcode-back" href="../../dev/engines/online/google.html#searx.autocomplete.google_complete">[docs]</a>
|
|
<span class="k">def</span> <span class="nf">google_complete</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Autocomplete from Google. Supports Google's languages and subdomains</span>
|
|
<span class="sd"> (:py:obj:`searx.engines.google.get_google_info`) by using the async REST</span>
|
|
<span class="sd"> API::</span>
|
|
|
|
<span class="sd"> https://{subdomain}/complete/search?{args}</span>
|
|
|
|
<span class="sd"> """</span>
|
|
|
|
<span class="n">google_info</span> <span class="o">=</span> <span class="n">google</span><span class="o">.</span><span class="n">get_google_info</span><span class="p">({</span><span class="s1">'searxng_locale'</span><span class="p">:</span> <span class="n">sxng_locale</span><span class="p">},</span> <span class="n">engines</span><span class="p">[</span><span class="s1">'google'</span><span class="p">]</span><span class="o">.</span><span class="n">traits</span><span class="p">)</span>
|
|
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://</span><span class="si">{subdomain}</span><span class="s1">/complete/search?</span><span class="si">{args}</span><span class="s1">'</span>
|
|
<span class="n">args</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="s1">'client'</span><span class="p">:</span> <span class="s1">'gws-wiz'</span><span class="p">,</span>
|
|
<span class="s1">'hl'</span><span class="p">:</span> <span class="n">google_info</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="p">}</span>
|
|
<span class="p">)</span>
|
|
<span class="n">results</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="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">subdomain</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="n">args</span><span class="o">=</span><span class="n">args</span><span class="p">))</span>
|
|
<span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
|
|
<span class="n">json_txt</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">text</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">find</span><span class="p">(</span><span class="s1">'['</span><span class="p">)</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">find</span><span class="p">(</span><span class="s1">']'</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">json_txt</span><span class="p">)</span>
|
|
<span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="mi">0</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="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">item</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">text_content</span><span class="p">())</span>
|
|
<span class="k">return</span> <span class="n">results</span></div>
|
|
|
|
|
|
|
|
<div class="viewcode-block" id="mwmbl">
|
|
<a class="viewcode-back" href="../../dev/engines/online/mwmbl.html#searx.autocomplete.mwmbl">[docs]</a>
|
|
<span class="k">def</span> <span class="nf">mwmbl</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Autocomplete from Mwmbl_."""</span>
|
|
|
|
<span class="c1"># mwmbl autocompleter</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://api.mwmbl.org/search/complete?</span><span class="si">{query}</span><span class="s1">'</span>
|
|
|
|
<span class="n">results</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">query</span><span class="o">=</span><span class="n">urlencode</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">json</span><span class="p">()[</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
<span class="c1"># results starting with `go:` are direct urls and not useful for auto completion</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="n">result</span> <span class="k">for</span> <span class="n">result</span> <span class="ow">in</span> <span class="n">results</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">result</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">"go: "</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">result</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">"search: "</span><span class="p">)]</span></div>
|
|
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">seznam</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="c1"># seznam search autocompleter</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://suggest.seznam.cz/fulltext/cs?</span><span class="si">{query}</span><span class="s1">'</span>
|
|
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span>
|
|
<span class="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
|
|
<span class="n">query</span><span class="o">=</span><span class="n">urlencode</span><span class="p">(</span>
|
|
<span class="p">{</span><span class="s1">'phrase'</span><span class="p">:</span> <span class="n">query</span><span class="p">,</span> <span class="s1">'cursorPosition'</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="n">query</span><span class="p">),</span> <span class="s1">'format'</span><span class="p">:</span> <span class="s1">'json-2'</span><span class="p">,</span> <span class="s1">'highlight'</span><span class="p">:</span> <span class="s1">'1'</span><span class="p">,</span> <span class="s1">'count'</span><span class="p">:</span> <span class="s1">'6'</span><span class="p">}</span>
|
|
<span class="p">)</span>
|
|
<span class="p">)</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="k">return</span> <span class="p">[]</span>
|
|
|
|
<span class="n">data</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="p">[</span>
|
|
<span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">part</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'text'</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span> <span class="k">for</span> <span class="n">part</span> <span class="ow">in</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'text'</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">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="k">if</span> <span class="n">item</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'itemType'</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span> <span class="o">==</span> <span class="s1">'ItemType.TEXT'</span>
|
|
<span class="p">]</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">stract</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="c1"># stract autocompleter (beta)</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">"https://stract.com/beta/api/autosuggest?q=</span><span class="si">{</span><span class="n">quote_plus</span><span class="p">(</span><span class="n">query</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span>
|
|
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">post</span><span class="p">(</span><span class="n">url</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="k">return</span> <span class="p">[]</span>
|
|
|
|
<span class="k">return</span> <span class="p">[</span><span class="n">html</span><span class="o">.</span><span class="n">unescape</span><span class="p">(</span><span class="n">suggestion</span><span class="p">[</span><span class="s1">'raw'</span><span class="p">])</span> <span class="k">for</span> <span class="n">suggestion</span> <span class="ow">in</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()]</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">startpage</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Autocomplete from Startpage. Supports Startpage's languages"""</span>
|
|
<span class="n">lui</span> <span class="o">=</span> <span class="n">engines</span><span class="p">[</span><span class="s1">'startpage'</span><span class="p">]</span><span class="o">.</span><span class="n">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">'english'</span><span class="p">)</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://startpage.com/suggestions?</span><span class="si">{query}</span><span class="s1">'</span>
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">query</span><span class="o">=</span><span class="n">urlencode</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="s1">'segment'</span><span class="p">:</span> <span class="s1">'startpage.udog'</span><span class="p">,</span> <span class="s1">'lui'</span><span class="p">:</span> <span class="n">lui</span><span class="p">})))</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
|
|
<span class="k">return</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="s1">'text'</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'suggestions'</span><span class="p">,</span> <span class="p">[])</span> <span class="k">if</span> <span class="s1">'text'</span> <span class="ow">in</span> <span class="n">e</span><span class="p">]</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">swisscows</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="c1"># swisscows autocompleter</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://swisscows.ch/api/suggest?</span><span class="si">{query}</span><span class="s1">&itemsCount=5'</span>
|
|
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">query</span><span class="o">=</span><span class="n">urlencode</span><span class="p">({</span><span class="s1">'query'</span><span class="p">:</span> <span class="n">query</span><span class="p">})))</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
|
|
<span class="k">return</span> <span class="n">resp</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">qwant</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Autocomplete from Qwant. Supports Qwant's regions."""</span>
|
|
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
|
|
|
|
<span class="n">locale</span> <span class="o">=</span> <span class="n">engines</span><span class="p">[</span><span class="s1">'qwant'</span><span class="p">]</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">sxng_locale</span><span class="p">,</span> <span class="s1">'en_US'</span><span class="p">)</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://api.qwant.com/v3/suggest?</span><span class="si">{query}</span><span class="s1">'</span>
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">query</span><span class="o">=</span><span class="n">urlencode</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="s1">'locale'</span><span class="p">:</span> <span class="n">locale</span><span class="p">,</span> <span class="s1">'version'</span><span class="p">:</span> <span class="s1">'2'</span><span class="p">})))</span>
|
|
|
|
<span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="n">data</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="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">data</span><span class="p">[</span><span class="s1">'data'</span><span class="p">][</span><span class="s1">'items'</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="n">item</span><span class="p">[</span><span class="s1">'value'</span><span class="p">])</span>
|
|
|
|
<span class="k">return</span> <span class="n">results</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">wikipedia</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">):</span>
|
|
<span class="w"> </span><span class="sd">"""Autocomplete from Wikipedia. Supports Wikipedia's languages (aka netloc)."""</span>
|
|
<span class="n">results</span> <span class="o">=</span> <span class="p">[]</span>
|
|
<span class="n">eng_traits</span> <span class="o">=</span> <span class="n">engines</span><span class="p">[</span><span class="s1">'wikipedia'</span><span class="p">]</span><span class="o">.</span><span class="n">traits</span>
|
|
<span class="n">wiki_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">'en'</span><span class="p">)</span>
|
|
<span class="n">wiki_netloc</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">'wiki_netloc'</span><span class="p">]</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">wiki_lang</span><span class="p">,</span> <span class="s1">'en.wikipedia.org'</span><span class="p">)</span>
|
|
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://</span><span class="si">{wiki_netloc}</span><span class="s1">/w/api.php?</span><span class="si">{args}</span><span class="s1">'</span>
|
|
<span class="n">args</span> <span class="o">=</span> <span class="n">urlencode</span><span class="p">(</span>
|
|
<span class="p">{</span>
|
|
<span class="s1">'action'</span><span class="p">:</span> <span class="s1">'opensearch'</span><span class="p">,</span>
|
|
<span class="s1">'format'</span><span class="p">:</span> <span class="s1">'json'</span><span class="p">,</span>
|
|
<span class="s1">'formatversion'</span><span class="p">:</span> <span class="s1">'2'</span><span class="p">,</span>
|
|
<span class="s1">'search'</span><span class="p">:</span> <span class="n">query</span><span class="p">,</span>
|
|
<span class="s1">'namespace'</span><span class="p">:</span> <span class="s1">'0'</span><span class="p">,</span>
|
|
<span class="s1">'limit'</span><span class="p">:</span> <span class="s1">'10'</span><span class="p">,</span>
|
|
<span class="p">}</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="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">args</span><span class="o">=</span><span class="n">args</span><span class="p">,</span> <span class="n">wiki_netloc</span><span class="o">=</span><span class="n">wiki_netloc</span><span class="p">))</span>
|
|
<span class="k">if</span> <span class="n">resp</span><span class="o">.</span><span class="n">ok</span><span class="p">:</span>
|
|
<span class="n">data</span> <span class="o">=</span> <span class="n">resp</span><span class="o">.</span><span class="n">json</span><span class="p">()</span>
|
|
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="n">results</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
|
|
<span class="k">return</span> <span class="n">results</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">yandex</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">_lang</span><span class="p">):</span>
|
|
<span class="c1"># yandex autocompleter</span>
|
|
<span class="n">url</span> <span class="o">=</span> <span class="s2">"https://suggest.yandex.com/suggest-ff.cgi?</span><span class="si">{0}</span><span class="s2">"</span>
|
|
|
|
<span class="n">resp</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">urlencode</span><span class="p">(</span><span class="nb">dict</span><span class="p">(</span><span class="n">part</span><span class="o">=</span><span class="n">query</span><span class="p">))))</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">resp</span><span class="p">)</span> <span class="o">></span> <span class="mi">1</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">resp</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
|
<span class="k">return</span> <span class="p">[]</span>
|
|
|
|
|
|
<span class="n">backends</span> <span class="o">=</span> <span class="p">{</span>
|
|
<span class="s1">'dbpedia'</span><span class="p">:</span> <span class="n">dbpedia</span><span class="p">,</span>
|
|
<span class="s1">'duckduckgo'</span><span class="p">:</span> <span class="n">duckduckgo</span><span class="p">,</span>
|
|
<span class="s1">'google'</span><span class="p">:</span> <span class="n">google_complete</span><span class="p">,</span>
|
|
<span class="s1">'mwmbl'</span><span class="p">:</span> <span class="n">mwmbl</span><span class="p">,</span>
|
|
<span class="s1">'seznam'</span><span class="p">:</span> <span class="n">seznam</span><span class="p">,</span>
|
|
<span class="s1">'startpage'</span><span class="p">:</span> <span class="n">startpage</span><span class="p">,</span>
|
|
<span class="s1">'stract'</span><span class="p">:</span> <span class="n">stract</span><span class="p">,</span>
|
|
<span class="s1">'swisscows'</span><span class="p">:</span> <span class="n">swisscows</span><span class="p">,</span>
|
|
<span class="s1">'qwant'</span><span class="p">:</span> <span class="n">qwant</span><span class="p">,</span>
|
|
<span class="s1">'wikipedia'</span><span class="p">:</span> <span class="n">wikipedia</span><span class="p">,</span>
|
|
<span class="s1">'brave'</span><span class="p">:</span> <span class="n">brave</span><span class="p">,</span>
|
|
<span class="s1">'yandex'</span><span class="p">:</span> <span class="n">yandex</span><span class="p">,</span>
|
|
<span class="p">}</span>
|
|
|
|
|
|
<span class="k">def</span> <span class="nf">search_autocomplete</span><span class="p">(</span><span class="n">backend_name</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">):</span>
|
|
<span class="n">backend</span> <span class="o">=</span> <span class="n">backends</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">backend_name</span><span class="p">)</span>
|
|
<span class="k">if</span> <span class="n">backend</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="p">[]</span>
|
|
<span class="k">try</span><span class="p">:</span>
|
|
<span class="k">return</span> <span class="n">backend</span><span class="p">(</span><span class="n">query</span><span class="p">,</span> <span class="n">sxng_locale</span><span class="p">)</span>
|
|
<span class="k">except</span> <span class="p">(</span><span class="n">HTTPError</span><span class="p">,</span> <span class="n">SearxEngineResponseException</span><span class="p">):</span>
|
|
<span class="k">return</span> <span class="p">[]</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>
|
|
</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> |