mirror of https://github.com/searxng/searxng.git
[fix] duckduckgo images doesn't fail with countryless language
This commit is contained in:
parent
38d6ba4066
commit
405e5c8f24
File diff suppressed because one or more lines are too long
|
@ -42,7 +42,7 @@ content_xpath = './/a[@class="result__snippet"]'
|
||||||
|
|
||||||
|
|
||||||
# match query's language to a region code that duckduckgo will accept
|
# match query's language to a region code that duckduckgo will accept
|
||||||
def get_region_code(lang):
|
def get_region_code(lang, lang_list=None):
|
||||||
# custom fixes for languages
|
# custom fixes for languages
|
||||||
if lang == 'all':
|
if lang == 'all':
|
||||||
region_code = None
|
region_code = None
|
||||||
|
@ -66,7 +66,7 @@ def get_region_code(lang):
|
||||||
else:
|
else:
|
||||||
# tries to get a country code from language
|
# tries to get a country code from language
|
||||||
region_code = region_code[0].lower()
|
region_code = region_code[0].lower()
|
||||||
for lc in supported_languages:
|
for lc in (lang_list or supported_languages):
|
||||||
lc = lc.split('-')
|
lc = lc.split('-')
|
||||||
if region_code == lc[0]:
|
if region_code == lc[0]:
|
||||||
region_code = lc[1].lower() + '-' + lc[0].lower()
|
region_code = lc[1].lower() + '-' + lc[0].lower()
|
||||||
|
|
|
@ -52,7 +52,7 @@ def request(query, params):
|
||||||
|
|
||||||
safesearch = params['safesearch'] - 1
|
safesearch = params['safesearch'] - 1
|
||||||
|
|
||||||
region_code = get_region_code(params['language'])
|
region_code = get_region_code(params['language'], lang_list=supported_languages)
|
||||||
if region_code:
|
if region_code:
|
||||||
params['url'] = images_url.format(
|
params['url'] = images_url.format(
|
||||||
query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd)
|
query=urlencode({'q': query, 'l': region_code}), offset=offset, safesearch=safesearch, vqd=vqd)
|
||||||
|
|
|
@ -8,6 +8,8 @@ from searx.testing import SearxTestCase
|
||||||
class TestDuckduckgoImagesEngine(SearxTestCase):
|
class TestDuckduckgoImagesEngine(SearxTestCase):
|
||||||
|
|
||||||
def test_request(self):
|
def test_request(self):
|
||||||
|
duckduckgo_images.supported_languages = ['de-CH', 'en-US']
|
||||||
|
|
||||||
query = 'test_query'
|
query = 'test_query'
|
||||||
dicto = defaultdict(dict)
|
dicto = defaultdict(dict)
|
||||||
dicto['is_test'] = True
|
dicto['is_test'] = True
|
||||||
|
@ -22,14 +24,16 @@ class TestDuckduckgoImagesEngine(SearxTestCase):
|
||||||
self.assertIn('p=-1', params['url'])
|
self.assertIn('p=-1', params['url'])
|
||||||
self.assertIn('vqd=12345', params['url'])
|
self.assertIn('vqd=12345', params['url'])
|
||||||
|
|
||||||
# test paging and safe search
|
# test paging, safe search and language
|
||||||
dicto['pageno'] = 2
|
dicto['pageno'] = 2
|
||||||
dicto['safesearch'] = 2
|
dicto['safesearch'] = 2
|
||||||
|
dicto['language'] = 'de'
|
||||||
params = duckduckgo_images.request(query, dicto)
|
params = duckduckgo_images.request(query, dicto)
|
||||||
self.assertIn('url', params)
|
self.assertIn('url', params)
|
||||||
self.assertIn(query, params['url'])
|
self.assertIn(query, params['url'])
|
||||||
self.assertIn('s=50', params['url'])
|
self.assertIn('s=50', params['url'])
|
||||||
self.assertIn('p=1', params['url'])
|
self.assertIn('p=1', params['url'])
|
||||||
|
self.assertIn('ch-de', params['url'])
|
||||||
|
|
||||||
def test_response(self):
|
def test_response(self):
|
||||||
self.assertRaises(AttributeError, duckduckgo_images.response, None)
|
self.assertRaises(AttributeError, duckduckgo_images.response, None)
|
||||||
|
|
Loading…
Reference in New Issue