on master help sign in
[mod] duckduckgo engine: better support of the language preference
d2dac11Alexandre Flament
searx/engines/duckduckgo.py+10−8
@@ -5,7 +5,8 @@
5
5
6from lxml.html import fromstring
6from lxml.html import fromstring
7from json import loads
7from json import loads
8−from searx.utils import extract_text, match_language, eval_xpath
8+from searx.utils import extract_text, match_language, eval_xpath, dict_subset
9+from searx.poolrequests import get
9
10
10# about
11# about
11about = {
12about = {
@@ -35,6 +36,7 @@ language_aliases = {
35
36
36# search-url
37# search-url
37url = 'https://html.duckduckgo.com/html'
38url = 'https://html.duckduckgo.com/html'
39+url_ping = 'https://duckduckgo.com/t/sl_h'
38time_range_dict = {'day': 'd',
40time_range_dict = {'day': 'd',
39 'week': 'w',
41 'week': 'w',
40 'month': 'm'}
42 'month': 'm'}
@@ -65,27 +67,27 @@ def request(query, params):
65
67
66 params['url'] = url
68 params['url'] = url
67 params['method'] = 'POST'
69 params['method'] = 'POST'
68− params['data']['b'] = ''
69 params['data']['q'] = query
70 params['data']['q'] = query
70− params['data']['df'] = ''
71+ params['data']['b'] = ''
71
72
72 region_code = get_region_code(params['language'], supported_languages)
73 region_code = get_region_code(params['language'], supported_languages)
73 if region_code:
74 if region_code:
74 params['data']['kl'] = region_code
75 params['data']['kl'] = region_code
75 params['cookies']['kl'] = region_code
76 params['cookies']['kl'] = region_code
76− if params['time_range'] in time_range_dict:
77− params['data']['df'] = time_range_dict[params['time_range']]
78
77
78+ params['data']['df'] = time_range_dict.get(params['time_range'], '')
79 return params
79 return params
80
80
81
81
82# get response from search-request
82# get response from search-request
83def response(resp):
83def response(resp):
84− results = []
84+ # ping
85+ headers_ping = dict_subset(resp.request.headers, ['User-Agent', 'Accept-Encoding', 'Accept', 'Cookie'])
86+ get(url_ping, headers=headers_ping)
85
87
88+ # parse the response
89+ results = []
86 doc = fromstring(resp.text)
90 doc = fromstring(resp.text)
87−
88− # parse results
89 for i, r in enumerate(eval_xpath(doc, result_xpath)):
91 for i, r in enumerate(eval_xpath(doc, result_xpath)):
90 if i >= 30:
92 if i >= 30:
91 break
93 break