2015-05-02 15:45:17 +02: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 17:14:57 +02:00
|
|
|
|
2013-10-23 23:55:37 +02:00
|
|
|
from urllib import urlencode
|
2014-03-21 16:33:17 +01:00
|
|
|
from lxml.html import fromstring
|
2015-02-02 17:55:39 +01:00
|
|
|
from searx.engines.xpath import extract_text
|
2013-10-14 23:09:13 +02:00
|
|
|
|
2014-09-02 17:14:57 +02:00
|
|
|
# engine dependent config
|
|
|
|
categories = ['general']
|
|
|
|
paging = True
|
2014-09-07 22:52:13 +02:00
|
|
|
language_support = True
|
2016-08-06 06:34:56 +02:00
|
|
|
supported_languages = ["es-AR", "en-AU", "de-AT", "fr-BE", "nl-BE", "pt-BR", "bg-BG", "en-CA", "fr-CA", "ca-CT",
|
|
|
|
"es-CL", "zh-CN", "es-CO", "hr-HR", "cs-CZ", "da-DK", "et-EE", "fi-FI", "fr-FR", "de-DE",
|
|
|
|
"el-GR", "tzh-HK", "hu-HU", "en-IN", "id-ID", "en-ID", "en-IE", "he-IL", "it-IT", "jp-JP",
|
|
|
|
"kr-KR", "es-XL", "lv-LV", "lt-LT", "ms-MY", "en-MY", "es-MX", "nl-NL", "en-NZ", "no-NO",
|
|
|
|
"es-PE", "en-PH", "tl-PH", "pl-PL", "pt-PT", "ro-RO", "ru-RU", "ar-XA", "en-XA", "en-SG",
|
|
|
|
"sk-SK", "sl-SL", "en-ZA", "es-ES", "ca-ES", "sv-SE", "de-CH", "fr-CH", "it-CH", "tzh-TW",
|
|
|
|
"th-TH", "tr-TR", "uk-UA", "en-UK", "en-US", "es-US", "vi-VN"]
|
2016-07-18 16:15:37 +02:00
|
|
|
time_range_support = True
|
2013-10-14 23:09:13 +02:00
|
|
|
|
2014-09-02 17:14:57 +02:00
|
|
|
# search-url
|
|
|
|
url = 'https://duckduckgo.com/html?{query}&s={offset}'
|
2016-07-18 16:15:37 +02:00
|
|
|
time_range_url = '&df={range}'
|
|
|
|
|
|
|
|
time_range_dict = {'day': 'd',
|
|
|
|
'week': 'w',
|
|
|
|
'month': 'm'}
|
2014-09-02 17:14:57 +02:00
|
|
|
|
|
|
|
# specific xpath variables
|
2016-03-22 03:19:13 +01: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 16:38:45 +01:00
|
|
|
|
2014-09-02 17:14:57 +02:00
|
|
|
|
|
|
|
# do search-request
|
2013-10-14 23:09:13 +02:00
|
|
|
def request(query, params):
|
2016-12-11 16:41:14 +01:00
|
|
|
if params['time_range'] and params['time_range'] not in time_range_dict:
|
|
|
|
return params
|
|
|
|
|
2014-01-30 01:03:19 +01:00
|
|
|
offset = (params['pageno'] - 1) * 30
|
2014-09-02 17:14:57 +02:00
|
|
|
|
2016-08-06 06:34:56 +02:00
|
|
|
# custom fixes for languages
|
2014-09-07 22:52:13 +02:00
|
|
|
if params['language'] == 'all':
|
2016-06-03 07:14:23 +02:00
|
|
|
locale = None
|
2016-08-06 06:34:56 +02:00
|
|
|
elif params['language'][:2] == 'ja':
|
|
|
|
locale = 'jp-jp'
|
|
|
|
elif params['language'] == 'zh-TW':
|
|
|
|
locale = 'tw-tzh'
|
|
|
|
elif params['language'] == 'zh-HK':
|
|
|
|
locale = 'hk-tzh'
|
|
|
|
elif params['language'][-2:] == 'SA':
|
|
|
|
locale = 'xa' + params['language'].split('-')[0]
|
|
|
|
elif params['language'][-2:] == 'GB':
|
|
|
|
locale = 'uk' + params['language'].split('-')[0]
|
|
|
|
elif params['language'] == 'es-419':
|
|
|
|
locale = 'xl-es'
|
2014-09-07 22:52:13 +02:00
|
|
|
else:
|
2016-08-06 06:34:56 +02:00
|
|
|
locale = params['language'].split('-')
|
2016-06-03 07:14:23 +02:00
|
|
|
if len(locale) == 2:
|
|
|
|
# country code goes first
|
|
|
|
locale = locale[1].lower() + '-' + locale[0].lower()
|
|
|
|
else:
|
2016-07-14 23:36:52 +02:00
|
|
|
# tries to get a country code from language
|
2016-06-03 07:14:23 +02:00
|
|
|
locale = locale[0].lower()
|
2016-10-30 03:04:01 +01:00
|
|
|
for lc in supported_languages:
|
2016-08-06 06:34:56 +02:00
|
|
|
lc = lc.split('-')
|
2016-07-14 23:36:52 +02:00
|
|
|
if locale == lc[0]:
|
|
|
|
locale = lc[1].lower() + '-' + lc[0].lower()
|
|
|
|
break
|
2016-06-03 07:14:23 +02:00
|
|
|
|
|
|
|
if locale:
|
|
|
|
params['url'] = url.format(
|
|
|
|
query=urlencode({'q': query, 'kl': locale}), offset=offset)
|
|
|
|
else:
|
|
|
|
params['url'] = url.format(
|
|
|
|
query=urlencode({'q': query}), offset=offset)
|
2014-09-02 17:14:57 +02:00
|
|
|
|
2016-07-26 00:22:05 +02:00
|
|
|
if params['time_range'] in time_range_dict:
|
2016-07-18 16:15:37 +02:00
|
|
|
params['url'] += time_range_url.format(range=time_range_dict[params['time_range']])
|
|
|
|
|
2013-10-14 23:09:13 +02:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
2014-09-02 17:14:57 +02:00
|
|
|
# get response from search-request
|
2013-10-14 23:09:13 +02:00
|
|
|
def response(resp):
|
2013-10-15 19:11:43 +02:00
|
|
|
results = []
|
2014-03-21 16:33:17 +01:00
|
|
|
|
|
|
|
doc = fromstring(resp.text)
|
|
|
|
|
2014-09-02 18:12:42 +02:00
|
|
|
# parse results
|
2014-03-21 16:33:17 +01:00
|
|
|
for r in doc.xpath(result_xpath):
|
2014-03-21 18:17:13 +01:00
|
|
|
try:
|
|
|
|
res_url = r.xpath(url_xpath)[-1]
|
|
|
|
except:
|
|
|
|
continue
|
2014-09-02 17:14:57 +02:00
|
|
|
|
2014-03-21 16:33:17 +01:00
|
|
|
if not res_url:
|
2013-10-15 19:11:43 +02:00
|
|
|
continue
|
2014-09-02 17:14:57 +02:00
|
|
|
|
2015-02-02 17:55:39 +01:00
|
|
|
title = extract_text(r.xpath(title_xpath))
|
|
|
|
content = extract_text(r.xpath(content_xpath))
|
2014-09-02 17:14:57 +02:00
|
|
|
|
|
|
|
# append result
|
2014-03-21 16:33:17 +01:00
|
|
|
results.append({'title': title,
|
|
|
|
'content': content,
|
2015-09-07 23:13:04 +02:00
|
|
|
'url': res_url})
|
2014-03-21 16:33:17 +01:00
|
|
|
|
2014-09-02 17:14:57 +02:00
|
|
|
# return results
|
2013-10-15 19:11:43 +02:00
|
|
|
return results
|