Skip to content

Commit

Permalink
[feat] autocompleter: implementation of stract (beta)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro authored and dalf committed Jan 7, 2024
1 parent 621e131 commit 3dea7e6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/dev/search_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Parameters

``autocomplete`` : default from :ref:`settings search`
[ ``google``, ``dbpedia``, ``duckduckgo``, ``mwmbl``, ``startpage``,
``wikipedia``, ``swisscows``, ``qwant`` ]
``wikipedia``, ``stract``, ``swisscows``, ``qwant`` ]

Service which completes words as you type.

Expand Down
28 changes: 25 additions & 3 deletions searx/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# pylint: disable=use-dict-literal

import json
from urllib.parse import urlencode
from urllib.parse import urlencode, quote_plus

import lxml
from httpx import HTTPError
Expand All @@ -16,17 +16,26 @@
engines,
google,
)
from searx.network import get as http_get
from searx.network import get as http_get, post as http_post
from searx.exceptions import SearxEngineResponseException


def get(*args, **kwargs):
def update_kwargs(**kwargs):
if 'timeout' not in kwargs:
kwargs['timeout'] = settings['outgoing']['request_timeout']
kwargs['raise_for_httperror'] = True


def get(*args, **kwargs):
update_kwargs(**kwargs)
return http_get(*args, **kwargs)


def post(*args, **kwargs):
update_kwargs(**kwargs)
return http_post(*args, **kwargs)


def brave(query, _lang):
# brave search autocompleter
url = 'https://search.brave.com/api/suggest?'
Expand Down Expand Up @@ -145,6 +154,18 @@ def seznam(query, _lang):
]


def stract(query, _lang):
# stract autocompleter (beta)
url = f"https://stract.com/beta/api/autosuggest?q={quote_plus(query)}"

resp = post(url)

if not resp.ok:
return []

return [suggestion['raw'] for suggestion in resp.json()]


def startpage(query, sxng_locale):
"""Autocomplete from Startpage. Supports Startpage's languages"""
lui = engines['startpage'].traits.get_language(sxng_locale, 'english')
Expand Down Expand Up @@ -223,6 +244,7 @@ def yandex(query, _lang):
'mwmbl': mwmbl,
'seznam': seznam,
'startpage': startpage,
'stract': stract,
'swisscows': swisscows,
'qwant': qwant,
'wikipedia': wikipedia,
Expand Down
2 changes: 1 addition & 1 deletion searx/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ search:
# Filter results. 0: None, 1: Moderate, 2: Strict
safe_search: 0
# Existing autocomplete backends: "dbpedia", "duckduckgo", "google", "yandex", "mwmbl",
# "seznam", "startpage", "swisscows", "qwant", "wikipedia" - leave blank to turn it off
# "seznam", "startpage", "stract", "swisscows", "qwant", "wikipedia" - leave blank to turn it off
# by default.
autocomplete: ""
# minimun characters to type before autocompleter starts
Expand Down

0 comments on commit 3dea7e6

Please sign in to comment.