diff options
| author | Bnyro <bnyro@tutanota.com> | 2026-03-03 12:27:37 +0100 |
|---|---|---|
| committer | Bnyro <bnyro@tutanota.com> | 2026-03-05 20:56:02 +0100 |
| commit | 56e565a58252af15b371b5ad6eda5a9bf4bd374d (patch) | |
| tree | 10c4586721441c805a7cc9b9c4d80c51e207c560 /searx/autocomplete.py | |
| parent | 380f1c4a491d48f41215aa870d805d0e832021ed (diff) | |
[feat] autocomplete: add bing autocompleter
Diffstat (limited to 'searx/autocomplete.py')
| -rw-r--r-- | searx/autocomplete.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/searx/autocomplete.py b/searx/autocomplete.py index 9612c85e8..8b7285376 100644 --- a/searx/autocomplete.py +++ b/searx/autocomplete.py @@ -1,6 +1,9 @@ # SPDX-License-Identifier: AGPL-3.0-or-later """This module implements functions needed for the autocompleter.""" + # pylint: disable=use-dict-literal +import string +import random import json import typing as t @@ -53,6 +56,26 @@ def baidu(query: str, _sxng_locale: str) -> list[str]: return results +def bing(query: str, _sxng_locale: str) -> list[str]: + # bing search autocompleter + base_url = "https://www.bing.com/AS/Suggestions?" + # cvid has to be a 32 character long string consisting of numbers and uppsercase characters + cvid = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(32)) + response = get(base_url + urlencode({'qry': query, 'csr': 1, 'cvid': cvid})) + results: list[str] = [] + + if response.ok: + data: dict[str, t.Any] = response.json() + if 's' in data: + for item in data['s']: + completion: str = item['q'] + # bing uses PUA unicode characters to highlight parts of the query + # we have to remove these manually (U+E000 and U+E001) + completion = completion.replace("\ue000", "").replace("\ue001", "") + results.append(completion) + return results + + def brave(query: str, _sxng_locale: str) -> list[str]: # brave search autocompleter url = 'https://search.brave.com/api/suggest?' @@ -331,6 +354,7 @@ def yandex(query: str, _sxng_locale: str) -> list[str]: backends: dict[str, t.Callable[[str, str], list[str]]] = { '360search': qihu360search, 'baidu': baidu, + 'bing': bing, 'brave': brave, 'dbpedia': dbpedia, 'duckduckgo': duckduckgo, |
