Przeglądaj źródła

[fix] multilingual duckduckgo

only works if both country and language are set
a01200356 8 lat temu
rodzic
commit
8fc4b8156a

+ 15
- 8
searx/engines/duckduckgo.py Wyświetl plik

11
  @parse       url, title, content
11
  @parse       url, title, content
12
 
12
 
13
  @todo        rewrite to api
13
  @todo        rewrite to api
14
- @todo        language support
15
-              (the current used site does not support language-change)
16
 """
14
 """
17
 
15
 
18
 from urllib import urlencode
16
 from urllib import urlencode
39
     offset = (params['pageno'] - 1) * 30
37
     offset = (params['pageno'] - 1) * 30
40
 
38
 
41
     if params['language'] == 'all':
39
     if params['language'] == 'all':
42
-        locale = 'en-us'
40
+        locale = None
43
     else:
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
     return params
57
     return params
51
 
58
 

+ 2
- 6
tests/unit/engines/test_duckduckgo.py Wyświetl plik

11
         query = 'test_query'
11
         query = 'test_query'
12
         dicto = defaultdict(dict)
12
         dicto = defaultdict(dict)
13
         dicto['pageno'] = 1
13
         dicto['pageno'] = 1
14
-        dicto['language'] = 'fr_FR'
14
+        dicto['language'] = 'de_CH'
15
         params = duckduckgo.request(query, dicto)
15
         params = duckduckgo.request(query, dicto)
16
         self.assertIn('url', params)
16
         self.assertIn('url', params)
17
         self.assertIn(query, params['url'])
17
         self.assertIn(query, params['url'])
18
         self.assertIn('duckduckgo.com', params['url'])
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
     def test_response(self):
21
     def test_response(self):
26
         self.assertRaises(AttributeError, duckduckgo.response, None)
22
         self.assertRaises(AttributeError, duckduckgo.response, None)