Browse Source

Merge pull request #623 from a01200356/duckduckgo

[fix] multilingual duckduckgo
Adam Tauber 8 years ago
parent
commit
8f2a3d241f
2 changed files with 24 additions and 14 deletions
  1. 22
    8
      searx/engines/duckduckgo.py
  2. 2
    6
      tests/unit/engines/test_duckduckgo.py

+ 22
- 8
searx/engines/duckduckgo.py View File

@@ -11,13 +11,12 @@
11 11
  @parse       url, title, content
12 12
 
13 13
  @todo        rewrite to api
14
- @todo        language support
15
-              (the current used site does not support language-change)
16 14
 """
17 15
 
18 16
 from urllib import urlencode
19 17
 from lxml.html import fromstring
20 18
 from searx.engines.xpath import extract_text
19
+from searx.languages import language_codes
21 20
 
22 21
 # engine dependent config
23 22
 categories = ['general']
@@ -39,13 +38,28 @@ def request(query, params):
39 38
     offset = (params['pageno'] - 1) * 30
40 39
 
41 40
     if params['language'] == 'all':
42
-        locale = 'en-us'
41
+        locale = None
43 42
     else:
44
-        locale = params['language'].replace('_', '-').lower()
45
-
46
-    params['url'] = url.format(
47
-        query=urlencode({'q': query, 'kl': locale}),
48
-        offset=offset)
43
+        locale = params['language'].split('_')
44
+        if len(locale) == 2:
45
+            # country code goes first
46
+            locale = locale[1].lower() + '-' + locale[0].lower()
47
+        else:
48
+            # tries to get a country code from language
49
+            locale = locale[0].lower()
50
+            lang_codes = [x[0] for x in language_codes]
51
+            for lc in lang_codes:
52
+                lc = lc.split('_')
53
+                if locale == lc[0]:
54
+                    locale = lc[1].lower() + '-' + lc[0].lower()
55
+                    break
56
+
57
+    if locale:
58
+        params['url'] = url.format(
59
+            query=urlencode({'q': query, 'kl': locale}), offset=offset)
60
+    else:
61
+        params['url'] = url.format(
62
+            query=urlencode({'q': query}), offset=offset)
49 63
 
50 64
     return params
51 65
 

+ 2
- 6
tests/unit/engines/test_duckduckgo.py View File

@@ -11,16 +11,12 @@ class TestDuckduckgoEngine(SearxTestCase):
11 11
         query = 'test_query'
12 12
         dicto = defaultdict(dict)
13 13
         dicto['pageno'] = 1
14
-        dicto['language'] = 'fr_FR'
14
+        dicto['language'] = 'de_CH'
15 15
         params = duckduckgo.request(query, dicto)
16 16
         self.assertIn('url', params)
17 17
         self.assertIn(query, params['url'])
18 18
         self.assertIn('duckduckgo.com', params['url'])
19
-        self.assertIn('fr-fr', params['url'])
20
-
21
-        dicto['language'] = 'all'
22
-        params = duckduckgo.request(query, dicto)
23
-        self.assertIn('en-us', params['url'])
19
+        self.assertIn('ch-de', params['url'])
24 20
 
25 21
     def test_response(self):
26 22
         self.assertRaises(AttributeError, duckduckgo.response, None)