2021-01-13 10:31:25 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-05-02 13:45:17 +00:00
|
|
|
"""
|
|
|
|
DuckDuckGo (Web)
|
|
|
|
"""
|
2014-09-02 15:14:57 +00:00
|
|
|
|
2014-03-21 15:33:17 +00:00
|
|
|
from lxml.html import fromstring
|
2016-11-06 02:51:38 +00:00
|
|
|
from json import loads
|
2021-02-09 13:36:43 +00:00
|
|
|
from searx.utils import extract_text, match_language, eval_xpath, dict_subset
|
[httpx] replace searx.poolrequests by searx.network
settings.yml:
* outgoing.networks:
* can contains network definition
* propertiers: enable_http, verify, http2, max_connections, max_keepalive_connections,
keepalive_expiry, local_addresses, support_ipv4, support_ipv6, proxies, max_redirects, retries
* retries: 0 by default, number of times searx retries to send the HTTP request (using different IP & proxy each time)
* local_addresses can be "192.168.0.1/24" (it supports IPv6)
* support_ipv4 & support_ipv6: both True by default
see https://github.com/searx/searx/pull/1034
* each engine can define a "network" section:
* either a full network description
* either reference an existing network
* all HTTP requests of engine use the same HTTP configuration (it was not the case before, see proxy configuration in master)
2021-04-05 08:43:33 +00:00
|
|
|
from searx.network import get
|
2013-10-14 21:09:13 +00:00
|
|
|
|
2021-01-13 10:31:25 +00:00
|
|
|
# about
|
|
|
|
about = {
|
|
|
|
"website": 'https://duckduckgo.com/',
|
|
|
|
"wikidata_id": 'Q12805',
|
|
|
|
"official_api_documentation": 'https://duckduckgo.com/api',
|
|
|
|
"use_official_api": False,
|
|
|
|
"require_api_key": False,
|
|
|
|
"results": 'HTML',
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['general']
|
2020-10-09 13:01:40 +00:00
|
|
|
paging = False
|
2018-02-14 22:17:46 +00:00
|
|
|
supported_languages_url = 'https://duckduckgo.com/util/u172.js'
|
2016-07-18 14:15:37 +00:00
|
|
|
time_range_support = True
|
2013-10-14 21:09:13 +00:00
|
|
|
|
2018-03-01 04:30:48 +00:00
|
|
|
language_aliases = {
|
|
|
|
'ar-SA': 'ar-XA',
|
|
|
|
'es-419': 'es-XL',
|
|
|
|
'ja': 'jp-JP',
|
|
|
|
'ko': 'kr-KR',
|
|
|
|
'sl-SI': 'sl-SL',
|
|
|
|
'zh-TW': 'tzh-TW',
|
|
|
|
'zh-HK': 'tzh-HK'
|
|
|
|
}
|
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
# search-url
|
2020-10-09 13:01:40 +00:00
|
|
|
url = 'https://html.duckduckgo.com/html'
|
2021-02-09 13:36:43 +00:00
|
|
|
url_ping = 'https://duckduckgo.com/t/sl_h'
|
2016-07-18 14:15:37 +00:00
|
|
|
time_range_dict = {'day': 'd',
|
|
|
|
'week': 'w',
|
2021-03-24 23:25:36 +00:00
|
|
|
'month': 'm',
|
|
|
|
'year': 'y'}
|
2014-09-02 15:14:57 +00:00
|
|
|
|
|
|
|
# specific xpath variables
|
2016-03-22 02:19:13 +00:00
|
|
|
result_xpath = '//div[@class="result results_links results_links_deep web-result "]' # noqa
|
|
|
|
url_xpath = './/a[@class="result__a"]/@href'
|
|
|
|
title_xpath = './/a[@class="result__a"]'
|
|
|
|
content_xpath = './/a[@class="result__snippet"]'
|
2020-06-13 21:42:16 +00:00
|
|
|
correction_xpath = '//div[@id="did_you_mean"]//a'
|
2014-03-29 15:38:45 +00:00
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
|
2017-05-21 03:33:08 +00:00
|
|
|
# match query's language to a region code that duckduckgo will accept
|
2020-11-16 08:43:23 +00:00
|
|
|
def get_region_code(lang, lang_list=None):
|
2019-01-06 14:27:46 +00:00
|
|
|
if lang == 'all':
|
|
|
|
return None
|
|
|
|
|
2020-11-16 08:43:23 +00:00
|
|
|
lang_code = match_language(lang, lang_list or [], language_aliases, 'wt-WT')
|
2018-03-01 04:30:48 +00:00
|
|
|
lang_parts = lang_code.split('-')
|
|
|
|
|
|
|
|
# country code goes first
|
|
|
|
return lang_parts[1].lower() + '-' + lang_parts[0].lower()
|
2017-05-21 03:33:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def request(query, params):
|
2020-10-09 13:01:40 +00:00
|
|
|
if params['time_range'] is not None and params['time_range'] not in time_range_dict:
|
2017-05-21 03:33:08 +00:00
|
|
|
return params
|
|
|
|
|
2020-10-09 13:01:40 +00:00
|
|
|
params['url'] = url
|
|
|
|
params['method'] = 'POST'
|
|
|
|
params['data']['q'] = query
|
2021-02-09 13:36:43 +00:00
|
|
|
params['data']['b'] = ''
|
2016-06-03 05:14:23 +00:00
|
|
|
|
2018-03-01 04:30:48 +00:00
|
|
|
region_code = get_region_code(params['language'], supported_languages)
|
2020-10-09 13:01:40 +00:00
|
|
|
if region_code:
|
|
|
|
params['data']['kl'] = region_code
|
|
|
|
params['cookies']['kl'] = region_code
|
2016-07-18 14:15:37 +00:00
|
|
|
|
2021-02-09 11:07:19 +00:00
|
|
|
if params['time_range'] in time_range_dict:
|
|
|
|
params['data']['df'] = time_range_dict[params['time_range']]
|
|
|
|
|
|
|
|
params['allow_redirects'] = False
|
2013-10-14 21:09:13 +00:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
# get response from search-request
|
2013-10-14 21:09:13 +00:00
|
|
|
def response(resp):
|
2021-02-09 11:07:19 +00:00
|
|
|
if resp.status_code == 303:
|
|
|
|
return []
|
|
|
|
|
2021-02-09 13:36:43 +00:00
|
|
|
# ping
|
|
|
|
headers_ping = dict_subset(resp.request.headers, ['User-Agent', 'Accept-Encoding', 'Accept', 'Cookie'])
|
|
|
|
get(url_ping, headers=headers_ping)
|
2014-03-21 15:33:17 +00:00
|
|
|
|
2021-02-09 13:36:43 +00:00
|
|
|
# parse the response
|
|
|
|
results = []
|
2014-03-21 15:33:17 +00:00
|
|
|
doc = fromstring(resp.text)
|
2019-11-15 08:31:37 +00:00
|
|
|
for i, r in enumerate(eval_xpath(doc, result_xpath)):
|
2019-10-14 11:52:15 +00:00
|
|
|
if i >= 30:
|
|
|
|
break
|
2014-03-21 17:17:13 +00:00
|
|
|
try:
|
2019-11-15 08:31:37 +00:00
|
|
|
res_url = eval_xpath(r, url_xpath)[-1]
|
2014-03-21 17:17:13 +00:00
|
|
|
except:
|
|
|
|
continue
|
2014-09-02 15:14:57 +00:00
|
|
|
|
2014-03-21 15:33:17 +00:00
|
|
|
if not res_url:
|
2013-10-15 17:11:43 +00:00
|
|
|
continue
|
2014-09-02 15:14:57 +00:00
|
|
|
|
2019-11-15 08:31:37 +00:00
|
|
|
title = extract_text(eval_xpath(r, title_xpath))
|
|
|
|
content = extract_text(eval_xpath(r, content_xpath))
|
2014-09-02 15:14:57 +00:00
|
|
|
|
|
|
|
# append result
|
2014-03-21 15:33:17 +00:00
|
|
|
results.append({'title': title,
|
|
|
|
'content': content,
|
2015-09-07 21:13:04 +00:00
|
|
|
'url': res_url})
|
2014-03-21 15:33:17 +00:00
|
|
|
|
2020-06-13 21:42:16 +00:00
|
|
|
# parse correction
|
|
|
|
for correction in eval_xpath(doc, correction_xpath):
|
|
|
|
# append correction
|
|
|
|
results.append({'correction': extract_text(correction)})
|
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
# return results
|
2013-10-15 17:11:43 +00:00
|
|
|
return results
|
2016-11-06 02:51:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
# get supported languages from their site
|
2016-12-15 06:34:43 +00:00
|
|
|
def _fetch_supported_languages(resp):
|
2016-11-06 02:51:38 +00:00
|
|
|
|
|
|
|
# response is a js file with regions as an embedded object
|
2016-12-15 06:34:43 +00:00
|
|
|
response_page = resp.text
|
2016-11-06 02:51:38 +00:00
|
|
|
response_page = response_page[response_page.find('regions:{') + 8:]
|
|
|
|
response_page = response_page[:response_page.find('}') + 1]
|
|
|
|
|
|
|
|
regions_json = loads(response_page)
|
|
|
|
supported_languages = map((lambda x: x[3:] + '-' + x[:2].upper()), regions_json.keys())
|
|
|
|
|
2017-10-10 21:52:41 +00:00
|
|
|
return list(supported_languages)
|