Browse Source

[fix] duckduckgo images doesn't fail with countryless language

marc 7 years ago
parent
commit
405e5c8f24

+ 1
- 1
searx/data/engines_languages.json
File diff suppressed because it is too large
View File


+ 2
- 2
searx/engines/duckduckgo.py View File

42
 
42
 
43
 
43
 
44
 # match query's language to a region code that duckduckgo will accept
44
 # match query's language to a region code that duckduckgo will accept
45
-def get_region_code(lang):
45
+def get_region_code(lang, lang_list=None):
46
     # custom fixes for languages
46
     # custom fixes for languages
47
     if lang == 'all':
47
     if lang == 'all':
48
         region_code = None
48
         region_code = None
66
         else:
66
         else:
67
             # tries to get a country code from language
67
             # tries to get a country code from language
68
             region_code = region_code[0].lower()
68
             region_code = region_code[0].lower()
69
-            for lc in supported_languages:
69
+            for lc in (lang_list or supported_languages):
70
                 lc = lc.split('-')
70
                 lc = lc.split('-')
71
                 if region_code == lc[0]:
71
                 if region_code == lc[0]:
72
                     region_code = lc[1].lower() + '-' + lc[0].lower()
72
                     region_code = lc[1].lower() + '-' + lc[0].lower()

+ 1
- 1
searx/engines/duckduckgo_images.py View File

52
 
52
 
53
     safesearch = params['safesearch'] - 1
53
     safesearch = params['safesearch'] - 1
54
 
54
 
55
-    region_code = get_region_code(params['language'])
55
+    region_code = get_region_code(params['language'], lang_list=supported_languages)
56
     if region_code:
56
     if region_code:
57
         params['url'] = images_url.format(
57
         params['url'] = images_url.format(
58
             query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd)
58
             query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd)

+ 5
- 1
tests/unit/engines/test_duckduckgo_images.py View File

8
 class TestDuckduckgoImagesEngine(SearxTestCase):
8
 class TestDuckduckgoImagesEngine(SearxTestCase):
9
 
9
 
10
     def test_request(self):
10
     def test_request(self):
11
+        duckduckgo_images.supported_languages = ['de-CH', 'en-US']
12
+
11
         query = 'test_query'
13
         query = 'test_query'
12
         dicto = defaultdict(dict)
14
         dicto = defaultdict(dict)
13
         dicto['is_test'] = True
15
         dicto['is_test'] = True
22
         self.assertIn('p=-1', params['url'])
24
         self.assertIn('p=-1', params['url'])
23
         self.assertIn('vqd=12345', params['url'])
25
         self.assertIn('vqd=12345', params['url'])
24
 
26
 
25
-        # test paging and safe search
27
+        # test paging, safe search and language
26
         dicto['pageno'] = 2
28
         dicto['pageno'] = 2
27
         dicto['safesearch'] = 2
29
         dicto['safesearch'] = 2
30
+        dicto['language'] = 'de'
28
         params = duckduckgo_images.request(query, dicto)
31
         params = duckduckgo_images.request(query, dicto)
29
         self.assertIn('url', params)
32
         self.assertIn('url', params)
30
         self.assertIn(query, params['url'])
33
         self.assertIn(query, params['url'])
31
         self.assertIn('s=50', params['url'])
34
         self.assertIn('s=50', params['url'])
32
         self.assertIn('p=1', params['url'])
35
         self.assertIn('p=1', params['url'])
36
+        self.assertIn('ch-de', params['url'])
33
 
37
 
34
     def test_response(self):
38
     def test_response(self):
35
         self.assertRaises(AttributeError, duckduckgo_images.response, None)
39
         self.assertRaises(AttributeError, duckduckgo_images.response, None)