From 8fc4b8156affc9e7f00db0988ccd627b3eaae8a7 Mon Sep 17 00:00:00 2001 From: a01200356 Date: Fri, 3 Jun 2016 00:14:23 -0500 Subject: [PATCH] [fix] multilingual duckduckgo only works if both country and language are set --- searx/engines/duckduckgo.py | 21 ++++++++++++++------- tests/unit/engines/test_duckduckgo.py | 8 ++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/searx/engines/duckduckgo.py b/searx/engines/duckduckgo.py index 373ce1b2d..714c27228 100644 --- a/searx/engines/duckduckgo.py +++ b/searx/engines/duckduckgo.py @@ -11,8 +11,6 @@ @parse url, title, content @todo rewrite to api - @todo language support - (the current used site does not support language-change) """ from urllib import urlencode @@ -39,13 +37,22 @@ def request(query, params): offset = (params['pageno'] - 1) * 30 if params['language'] == 'all': - locale = 'en-us' + locale = None else: - locale = params['language'].replace('_', '-').lower() + locale = params['language'].split('_') + if len(locale) == 2: + # country code goes first + locale = locale[1].lower() + '-' + locale[0].lower() + else: + # doesn't actually do anything because ddg requires both country and language + locale = locale[0].lower() - params['url'] = url.format( - query=urlencode({'q': query, 'kl': locale}), - offset=offset) + 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 index 8f99dc9cb..90cdc9d9e 100644 --- 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)