Commit 30cdec37 authored by Alexandre Flament's avatar Alexandre Flament Committed by GitHub

Merge pull request #1565 from MarcAbonce/fix_duck_images

[fix] DuckDuckGo Images requests
parents 34d85c8c 3e1c2153
...@@ -35,9 +35,12 @@ site_url = 'https://duckduckgo.com/?{query}&iar=images&iax=1&ia=images' ...@@ -35,9 +35,12 @@ site_url = 'https://duckduckgo.com/?{query}&iar=images&iax=1&ia=images'
# run query in site to get vqd number needed for requesting images # run query in site to get vqd number needed for requesting images
# TODO: find a way to get this number without an extra request (is it a hash of the query?) # TODO: find a way to get this number without an extra request (is it a hash of the query?)
def get_vqd(query): def get_vqd(query, headers):
res = get(site_url.format(query=urlencode({'q': query}))) query_url = site_url.format(query=urlencode({'q': query}))
res = get(query_url, headers=headers)
content = res.text content = res.text
if content.find('vqd=\'') == -1:
raise Exception('Request failed')
vqd = content[content.find('vqd=\'') + 5:] vqd = content[content.find('vqd=\'') + 5:]
vqd = vqd[:vqd.find('\'')] vqd = vqd[:vqd.find('\'')]
return vqd return vqd
...@@ -47,7 +50,7 @@ def get_vqd(query): ...@@ -47,7 +50,7 @@ def get_vqd(query):
def request(query, params): def request(query, params):
# to avoid running actual external requests when testing # to avoid running actual external requests when testing
if 'is_test' not in params: if 'is_test' not in params:
vqd = get_vqd(query) vqd = get_vqd(query, params['headers'])
else: else:
vqd = '12345' vqd = '12345'
...@@ -74,7 +77,7 @@ def response(resp): ...@@ -74,7 +77,7 @@ def response(resp):
try: try:
res_json = loads(content) res_json = loads(content)
except: except:
return [] raise Exception('Cannot parse results')
# parse results # parse results
for result in res_json['results']: for result in res_json['results']:
......
...@@ -41,7 +41,7 @@ class TestDuckduckgoImagesEngine(SearxTestCase): ...@@ -41,7 +41,7 @@ class TestDuckduckgoImagesEngine(SearxTestCase):
self.assertRaises(AttributeError, duckduckgo_images.response, '[]') self.assertRaises(AttributeError, duckduckgo_images.response, '[]')
response = mock.Mock(text='If this error persists, please let us know: ops@duckduckgo.com') response = mock.Mock(text='If this error persists, please let us know: ops@duckduckgo.com')
self.assertEqual(duckduckgo_images.response(response), []) self.assertRaises(Exception, duckduckgo_images.response, response)
json = u""" json = u"""
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment