summaryrefslogtreecommitdiff
path: root/searx/autocomplete.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarit.de>2022-10-28 19:12:59 +0200
committerMarkus Heiser <markus.heiser@darmarit.de>2023-03-24 10:37:42 +0100
commit858aa3e6043a5102aec1b05e94ef1d65059f8898 (patch)
tree18513c6ddb9a4dd7262023747e85c417325c03ba /searx/autocomplete.py
parente0a6ca96cc071c7b02fb024d34a8902e636d0653 (diff)
downloadsearxng-858aa3e6043a5102aec1b05e94ef1d65059f8898.tar.gz
searxng-858aa3e6043a5102aec1b05e94ef1d65059f8898.zip
[mod] wikipedia & wikidata: upgrade to data_type: traits_v1
BTW this fix an issue in wikipedia: SearXNG's locales zh-TW and zh-HK are now using language `zh-classical` from wikipedia (and not `zh`). Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/autocomplete.py')
-rw-r--r--searx/autocomplete.py31
1 files changed, 24 insertions, 7 deletions
diff --git a/searx/autocomplete.py b/searx/autocomplete.py
index 848600e57..53e19905c 100644
--- a/searx/autocomplete.py
+++ b/searx/autocomplete.py
@@ -143,14 +143,31 @@ def qwant(query, sxng_locale):
return results
-def wikipedia(query, lang):
- # wikipedia autocompleter
- url = 'https://' + lang + '.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json'
+def wikipedia(query, sxng_locale):
+ """Autocomplete from Wikipedia. Supports Wikipedia's languages (aka netloc)."""
+ results = []
+ eng_traits = engines['wikipedia'].traits
+ wiki_lang = eng_traits.get_language(sxng_locale, 'en')
+ wiki_netloc = eng_traits.custom['wiki_netloc'].get(wiki_lang, 'en.wikipedia.org')
+
+ url = 'https://{wiki_netloc}/w/api.php?{args}'
+ args = urlencode(
+ {
+ 'action': 'opensearch',
+ 'format': 'json',
+ 'formatversion': '2',
+ 'search': query,
+ 'namespace': '0',
+ 'limit': '10',
+ }
+ )
+ resp = get(url.format(args=args, wiki_netloc=wiki_netloc))
+ if resp.ok:
+ data = resp.json()
+ if len(data) > 1:
+ results = data[1]
- resp = loads(get(url.format(urlencode(dict(search=query)))).text)
- if len(resp) > 1:
- return resp[1]
- return []
+ return results
def yandex(query, _lang):