From 94cb3a7f11d252dc8dabd6bce4d7f4d67a1ececd Mon Sep 17 00:00:00 2001 From: a01200356 Date: Tue, 29 Mar 2016 18:53:31 -0600 Subject: [PATCH] [enh] multilingual autocomplete implemented for wikipedia, qwant and google --- searx/autocomplete.py | 20 ++++++++++---------- searx/webapp.py | 10 ++++++++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/searx/autocomplete.py b/searx/autocomplete.py index 94913e8d8..527104041 100644 --- a/searx/autocomplete.py +++ b/searx/autocomplete.py @@ -110,7 +110,7 @@ def searx_bang(full_query): return list(result_set) -def dbpedia(query): +def dbpedia(query, lang): # dbpedia autocompleter, no HTTPS autocomplete_url = 'http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?' @@ -126,7 +126,7 @@ def dbpedia(query): return results -def duckduckgo(query): +def duckduckgo(query, lang): # duckduckgo autocompleter url = 'https://ac.duckduckgo.com/ac/?{0}&type=list' @@ -136,11 +136,11 @@ def duckduckgo(query): return [] -def google(query): +def google(query, lang): # google autocompleter autocomplete_url = 'https://suggestqueries.google.com/complete/search?client=toolbar&' - response = get(autocomplete_url + urlencode(dict(q=query))) + response = get(autocomplete_url + urlencode(dict(hl=lang, q=query))) results = [] @@ -151,8 +151,8 @@ def google(query): return results -def startpage(query): - # wikipedia autocompleter +def startpage(query, lang): + # startpage autocompleter url = 'https://startpage.com/do/suggest?{query}' resp = get(url.format(query=urlencode({'query': query}))).text.split('\n') @@ -161,11 +161,11 @@ def startpage(query): return [] -def qwant(query): +def qwant(query, lang): # qwant autocompleter (additional parameter : lang=en_en&count=xxx ) url = 'https://api.qwant.com/api/suggest?{query}' - resp = get(url.format(query=urlencode({'q': query}))) + resp = get(url.format(query=urlencode({'q': query, 'lang': lang}))) results = [] @@ -178,9 +178,9 @@ def qwant(query): return results -def wikipedia(query): +def wikipedia(query, lang): # wikipedia autocompleter - url = 'https://en.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json' + url = 'https://' + lang + '.wikipedia.org/w/api.php?action=opensearch&{0}&limit=10&namespace=0&format=json' resp = loads(get(url.format(urlencode(dict(search=query)))).text) if len(resp) > 1: diff --git a/searx/webapp.py b/searx/webapp.py index 66ba65a29..150490faf 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -492,7 +492,7 @@ def autocompleter(): if not query.getSearchQuery(): return '', 400 - # run autocompleter + # get autocompleter completer = autocomplete_backends.get(request.cookies.get('autocomplete', settings['search']['autocomplete'])) # parse searx specific autocompleter results like !bang @@ -500,8 +500,14 @@ def autocompleter(): # normal autocompletion results only appear if max 3 inner results returned if len(raw_results) <= 3 and completer: + # get language from cookie + language = request.cookies.get('language') + if not language or language == 'all': + language = 'en' + else: + language = language.split('_')[0] # run autocompletion - raw_results.extend(completer(query.getSearchQuery())) + raw_results.extend(completer(query.getSearchQuery(), language)) # parse results (write :language and !engine back to result string) results = []