1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/searx_ynh.git synced 2024-09-03 20:16:30 +02:00
searx_ynh/sources/searx/engines/duckduckgo.py

77 lines
1.8 KiB
Python
Raw Normal View History

2014-12-01 12:26:38 +01:00
## DuckDuckGo (Web)
2014-12-13 13:56:02 +01:00
#
2014-12-01 12:26:38 +01:00
# @website https://duckduckgo.com/
2014-12-13 13:56:02 +01:00
# @provide-api yes (https://duckduckgo.com/api),
# but not all results from search-site
#
2014-12-01 12:26:38 +01:00
# @using-api no
# @results HTML (using search portal)
# @stable no (HTML can change)
# @parse url, title, content
#
# @todo rewrite to api
2014-12-13 13:56:02 +01:00
# @todo language support
# (the current used site does not support language-change)
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
from urllib import urlencode
from lxml.html import fromstring
2015-02-17 12:45:54 +01:00
from searx.engines.xpath import extract_text
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# engine dependent config
categories = ['general']
paging = True
language_support = True
# search-url
2014-05-10 11:56:19 +02:00
url = 'https://duckduckgo.com/html?{query}&s={offset}'
2014-12-01 12:26:38 +01:00
# specific xpath variables
result_xpath = '//div[@class="results_links results_links_deep web-result"]' # noqa
url_xpath = './/a[@class="large"]/@href'
2015-02-17 12:45:54 +01:00
title_xpath = './/a[@class="large"]'
content_xpath = './/div[@class="snippet"]'
2014-05-10 11:56:19 +02:00
2014-12-01 12:26:38 +01:00
# do search-request
2014-05-10 11:56:19 +02:00
def request(query, params):
offset = (params['pageno'] - 1) * 30
2014-12-01 12:26:38 +01:00
if params['language'] == 'all':
locale = 'en-us'
else:
2014-12-13 13:56:02 +01:00
locale = params['language'].replace('_', '-').lower()
2014-12-01 12:26:38 +01:00
params['url'] = url.format(
query=urlencode({'q': query, 'kl': locale}),
offset=offset)
2014-05-10 11:56:19 +02:00
return params
2014-12-01 12:26:38 +01:00
# get response from search-request
2014-05-10 11:56:19 +02:00
def response(resp):
results = []
doc = fromstring(resp.text)
2014-12-01 12:26:38 +01:00
# parse results
2014-05-10 11:56:19 +02:00
for r in doc.xpath(result_xpath):
try:
res_url = r.xpath(url_xpath)[-1]
except:
continue
2014-12-01 12:26:38 +01:00
2014-05-10 11:56:19 +02:00
if not res_url:
continue
2014-12-01 12:26:38 +01:00
2015-02-17 12:45:54 +01:00
title = extract_text(r.xpath(title_xpath))
content = extract_text(r.xpath(content_xpath))
2014-12-01 12:26:38 +01:00
# append result
2014-05-10 11:56:19 +02:00
results.append({'title': title,
'content': content,
'url': res_url})
2014-12-01 12:26:38 +01:00
# return results
2014-05-10 11:56:19 +02:00
return results