2015-05-02 13:45:17 +00:00
|
|
|
"""
|
|
|
|
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-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
|
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
|
2013-10-14 21:09:13 +00:00
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['general']
|
|
|
|
paging = True
|
2014-09-07 20:52:13 +00:00
|
|
|
language_support = True
|
2016-11-06 02:51:38 +00:00
|
|
|
supported_languages_url = 'https://duckduckgo.com/d2030.js'
|
2016-07-18 14:15:37 +00:00
|
|
|
time_range_support = True
|
2013-10-14 21:09:13 +00:00
|
|
|
|
2014-09-02 15:14:57 +00:00
|
|
|
# search-url
|
2017-02-01 19:43:22 +00:00
|
|
|
url = 'https://duckduckgo.com/html?{query}&s={offset}&api=/d.js&o=json&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'}
|
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"]'
|
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
|
2017-07-05 03:29:06 +00:00
|
|
|
def get_region_code(lang, lang_list=None):
|
2016-08-06 04:34:56 +00:00
|
|
|
# custom fixes for languages
|
2017-05-21 03:33:08 +00:00
|
|
|
if lang == 'all':
|
|
|
|
region_code = None
|
|
|
|
elif lang[:2] == 'ja':
|
|
|
|
region_code = 'jp-jp'
|
|
|
|
elif lang[:2] == 'sl':
|
|
|
|
region_code = 'sl-sl'
|
|
|
|
elif lang == 'zh-TW':
|
|
|
|
region_code = 'tw-tzh'
|
|
|
|
elif lang == 'zh-HK':
|
|
|
|
region_code = 'hk-tzh'
|
|
|
|
elif lang[-2:] == 'SA':
|
|
|
|
region_code = 'xa-' + lang.split('-')[0]
|
|
|
|
elif lang[-2:] == 'GB':
|
|
|
|
region_code = 'uk-' + lang.split('-')[0]
|
2014-09-07 20:52:13 +00:00
|
|
|
else:
|
2017-05-21 03:33:08 +00:00
|
|
|
region_code = lang.split('-')
|
|
|
|
if len(region_code) == 2:
|
2016-06-03 05:14:23 +00:00
|
|
|
# country code goes first
|
2017-05-21 03:33:08 +00:00
|
|
|
region_code = region_code[1].lower() + '-' + region_code[0].lower()
|
2016-06-03 05:14:23 +00:00
|
|
|
else:
|
2016-07-14 21:36:52 +00:00
|
|
|
# tries to get a country code from language
|
2017-05-21 03:33:08 +00:00
|
|
|
region_code = region_code[0].lower()
|
2017-07-05 03:29:06 +00:00
|
|
|
for lc in (lang_list or supported_languages):
|
2016-08-06 04:34:56 +00:00
|
|
|
lc = lc.split('-')
|
2017-05-21 03:33:08 +00:00
|
|
|
if region_code == lc[0]:
|
|
|
|
region_code = lc[1].lower() + '-' + lc[0].lower()
|
2016-07-14 21:36:52 +00:00
|
|
|
break
|
2017-05-21 03:33:08 +00:00
|
|
|
return region_code
|
|
|
|
|
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
|
|
|
if params['time_range'] and params['time_range'] not in time_range_dict:
|
|
|
|
return params
|
|
|
|
|
2017-05-28 01:55:23 +00:00
|
|
|
offset = (params['pageno'] - 1) * 30
|
2016-06-03 05:14:23 +00:00
|
|
|
|
2017-05-21 03:33:08 +00:00
|
|
|
region_code = get_region_code(params['language'])
|
|
|
|
if region_code:
|
2016-06-03 05:14:23 +00:00
|
|
|
params['url'] = url.format(
|
2017-05-28 01:55:23 +00:00
|
|
|
query=urlencode({'q': query, 'kl': region_code}), offset=offset, dc_param=offset)
|
2016-06-03 05:14:23 +00:00
|
|
|
else:
|
|
|
|
params['url'] = url.format(
|
2017-05-28 01:55:23 +00:00
|
|
|
query=urlencode({'q': query}), offset=offset, dc_param=offset)
|
2014-09-02 15:14:57 +00:00
|
|
|
|
2016-07-25 22:22:05 +00:00
|
|
|
if params['time_range'] in time_range_dict:
|
2016-07-18 14:15:37 +00:00
|
|
|
params['url'] += time_range_url.format(range=time_range_dict[params['time_range']])
|
|
|
|
|
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):
|
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
|
2014-03-21 15:33:17 +00:00
|
|
|
for r in doc.xpath(result_xpath):
|
2014-03-21 17:17:13 +00:00
|
|
|
try:
|
|
|
|
res_url = r.xpath(url_xpath)[-1]
|
|
|
|
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
|
|
|
|
2015-02-02 16:55:39 +00:00
|
|
|
title = extract_text(r.xpath(title_xpath))
|
|
|
|
content = extract_text(r.xpath(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
|
|
|
|
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())
|
|
|
|
|
|
|
|
return supported_languages
|