Просмотр исходного кода

[fix] multilingual duckduckgo

only works if both country and language are set
a01200356 8 лет назад
Родитель
Сommit
8fc4b8156a
2 измененных файлов: 17 добавлений и 14 удалений
  1. 15
    8
      searx/engines/duckduckgo.py
  2. 2
    6
      tests/unit/engines/test_duckduckgo.py

+ 15
- 8
searx/engines/duckduckgo.py Просмотреть файл

@@ -11,8 +11,6 @@
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
@@ -39,13 +37,22 @@ def request(query, params):
39 37
     offset = (params['pageno'] - 1) * 30
40 38
 
41 39
     if params['language'] == 'all':
42
-        locale = 'en-us'
40
+        locale = None
43 41
     else:
44
-        locale = params['language'].replace('_', '-').lower()
45
-
46
-    params['url'] = url.format(
47
-        query=urlencode({'q': query, 'kl': locale}),
48
-        offset=offset)
42
+        locale = params['language'].split('_')
43
+        if len(locale) == 2:
44
+            # country code goes first
45
+            locale = locale[1].lower() + '-' + locale[0].lower()
46
+        else:
47
+            # doesn't actually do anything because ddg requires both country and language
48
+            locale = locale[0].lower()
49
+
50
+    if locale:
51
+        params['url'] = url.format(
52
+            query=urlencode({'q': query, 'kl': locale}), offset=offset)
53
+    else:
54
+        params['url'] = url.format(
55
+            query=urlencode({'q': query}), offset=offset)
49 56
 
50 57
     return params
51 58
 

+ 2
- 6
tests/unit/engines/test_duckduckgo.py Просмотреть файл

@@ -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)