searxng/searx/engines/duckduckgo.py

144 lines
4.1 KiB
Python
Raw Normal View History

"""
DuckDuckGo (Web)
@website https://duckduckgo.com/
@provide-api yes (https://duckduckgo.com/api),
but not all results from search-site
@using-api no
@results HTML (using search portal)
@stable no (HTML can change)
@parse url, title, content
@todo rewrite to api
"""
2014-03-21 15:33:17 +00:00
from lxml.html import fromstring
from json import loads
2015-02-02 16:55:39 +00:00
from searx.engines.xpath import extract_text
2017-07-21 14:23:20 +00:00
from searx.poolrequests import get
2016-11-30 17:43:03 +00:00
from searx.url_utils import urlencode
from searx.utils import match_language
2013-10-14 21:09:13 +00:00
# engine dependent config
categories = ['general']
paging = True
language_support = True
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
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'
}
# search-url
url = 'https://duckduckgo.com/html?{query}&s={offset}&dc={dc_param}'
2016-07-18 14:15:37 +00:00
time_range_url = '&df={range}'
time_range_dict = {'day': 'd',
'week': 'w',
'month': 'm'}
# specific xpath variables
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"]'
2014-03-29 15:38:45 +00:00
2017-05-21 03:33:08 +00:00
# match query's language to a region code that duckduckgo will accept
def get_region_code(lang, lang_list=[]):
if lang == 'all':
return None
lang_code = match_language(lang, lang_list, language_aliases, 'wt-WT')
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):
2019-10-14 11:52:15 +00:00
if params['time_range'] not in (None, 'None', '') and params['time_range'] not in time_range_dict:
2017-05-21 03:33:08 +00:00
return params
offset = (params['pageno'] - 1) * 30
region_code = get_region_code(params['language'], supported_languages)
2019-10-14 11:52:15 +00:00
params['url'] = 'https://duckduckgo.com/html/'
if params['pageno'] > 1:
params['method'] = 'POST'
params['data']['q'] = query
params['data']['s'] = offset
params['data']['dc'] = 30
params['data']['nextParams'] = ''
params['data']['v'] = 'l'
params['data']['o'] = 'json'
params['data']['api'] = '/d.js'
if params['time_range'] in time_range_dict:
params['data']['df'] = time_range_dict[params['time_range']]
if region_code:
params['data']['kl'] = region_code
else:
2019-10-14 11:52:15 +00:00
if region_code:
params['url'] = url.format(
query=urlencode({'q': query, 'kl': region_code}), offset=offset, dc_param=offset)
else:
params['url'] = url.format(
query=urlencode({'q': query}), offset=offset, dc_param=offset)
2019-10-14 11:52:15 +00:00
if params['time_range'] in time_range_dict:
params['url'] += time_range_url.format(range=time_range_dict[params['time_range']])
2016-07-18 14:15:37 +00:00
2013-10-14 21:09:13 +00:00
return params
# get response from search-request
2013-10-14 21:09:13 +00:00
def response(resp):
2013-10-15 17:11:43 +00:00
results = []
2014-03-21 15:33:17 +00:00
doc = fromstring(resp.text)
2014-09-02 16:12:42 +00:00
# parse results
2019-10-14 11:52:15 +00:00
for i,r in enumerate(doc.xpath(result_xpath)):
if i >= 30:
break
2014-03-21 17:17:13 +00:00
try:
res_url = r.xpath(url_xpath)[-1]
except:
continue
2014-03-21 15:33:17 +00:00
if not res_url:
2013-10-15 17:11:43 +00:00
continue
2015-02-02 16:55:39 +00:00
title = extract_text(r.xpath(title_xpath))
content = extract_text(r.xpath(content_xpath))
# 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
# return results
2013-10-15 17:11:43 +00:00
return results
# get supported languages from their site
def _fetch_supported_languages(resp):
# response is a js file with regions as an embedded object
response_page = resp.text
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())
return list(supported_languages)