logo

searx

Unnamed repository; edit this file 'description' to name the repository.
commit: 8f2a3d241f51c3da8fdd31014d5569ac11c18021
parent: 3fd405dcd3fd46d5b6f2fe1df625eedf0f1fbe02
Author: Adam Tauber <asciimoo@gmail.com>
Date:   Fri, 15 Jul 2016 12:56:09 +0200

Merge pull request #623 from a01200356/duckduckgo

[fix] multilingual duckduckgo

Diffstat:

searx/engines/duckduckgo.py | 30++++++++++++++++++++++--------
tests/unit/engines/test_duckduckgo.py | 8++------
2 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/searx/engines/duckduckgo.py b/searx/engines/duckduckgo.py @@ -11,13 +11,12 @@ @parse url, title, content @todo rewrite to api - @todo language support - (the current used site does not support language-change) """ from urllib import urlencode from lxml.html import fromstring from searx.engines.xpath import extract_text +from searx.languages import language_codes # engine dependent config categories = ['general'] @@ -39,13 +38,28 @@ def request(query, params): offset = (params['pageno'] - 1) * 30 if params['language'] == 'all': - locale = 'en-us' + locale = None else: - locale = params['language'].replace('_', '-').lower() - - params['url'] = url.format( - query=urlencode({'q': query, 'kl': locale}), - offset=offset) + locale = params['language'].split('_') + if len(locale) == 2: + # country code goes first + locale = locale[1].lower() + '-' + locale[0].lower() + else: + # tries to get a country code from language + locale = locale[0].lower() + lang_codes = [x[0] for x in language_codes] + for lc in lang_codes: + lc = lc.split('_') + if locale == lc[0]: + locale = lc[1].lower() + '-' + lc[0].lower() + break + + if locale: + params['url'] = url.format( + query=urlencode({'q': query, 'kl': locale}), offset=offset) + else: + params['url'] = url.format( + query=urlencode({'q': query}), offset=offset) return params diff --git a/tests/unit/engines/test_duckduckgo.py b/tests/unit/engines/test_duckduckgo.py @@ -11,16 +11,12 @@ class TestDuckduckgoEngine(SearxTestCase): query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 - dicto['language'] = 'fr_FR' + dicto['language'] = 'de_CH' params = duckduckgo.request(query, dicto) self.assertIn('url', params) self.assertIn(query, params['url']) self.assertIn('duckduckgo.com', params['url']) - self.assertIn('fr-fr', params['url']) - - dicto['language'] = 'all' - params = duckduckgo.request(query, dicto) - self.assertIn('en-us', params['url']) + self.assertIn('ch-de', params['url']) def test_response(self): self.assertRaises(AttributeError, duckduckgo.response, None)