logo

searx

Unnamed repository; edit this file 'description' to name the repository.
commit: 26c818193d11598550c28f8a72bf5835b2a95bf5
parent: 71de593a5b9025e360da32813e9efeaeaa4992b2
Author: Adam Tauber <asciimoo@gmail.com>
Date:   Wed, 30 Mar 2016 11:07:45 +0200

Merge pull request #528 from a01200356/autocomplete

[enh] multilingual autocomplete

Diffstat:

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 @@ -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 @@ -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 = []