Przeglądaj źródła

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

marc 7 lat temu
rodzic
commit
405e5c8f24

+ 1
- 1
searx/data/engines_languages.json
Plik diff jest za duży
Wyświetl plik


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

@@ -42,7 +42,7 @@ content_xpath = './/a[@class="result__snippet"]'
42 42
 
43 43
 
44 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 46
     # custom fixes for languages
47 47
     if lang == 'all':
48 48
         region_code = None
@@ -66,7 +66,7 @@ def get_region_code(lang):
66 66
         else:
67 67
             # tries to get a country code from language
68 68
             region_code = region_code[0].lower()
69
-            for lc in supported_languages:
69
+            for lc in (lang_list or supported_languages):
70 70
                 lc = lc.split('-')
71 71
                 if region_code == lc[0]:
72 72
                     region_code = lc[1].lower() + '-' + lc[0].lower()

+ 1
- 1
searx/engines/duckduckgo_images.py Wyświetl plik

@@ -52,7 +52,7 @@ def request(query, params):
52 52
 
53 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 56
     if region_code:
57 57
         params['url'] = images_url.format(
58 58
             query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd)

+ 5
- 1
tests/unit/engines/test_duckduckgo_images.py Wyświetl plik

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