summaryrefslogtreecommitdiff
path: root/searx/engines/duckduckgo.py
blob: 4ac2099ae80fe7968fdd63f18e459e464a26580b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
 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
 @todo        language support
              (the current used site does not support language-change)
"""

from urllib import urlencode
from lxml.html import fromstring
from searx.engines.xpath import extract_text

# engine dependent config
categories = ['general']
paging = True
language_support = True

# search-url
url = 'https://duckduckgo.com/html?{query}&s={offset}'

# specific xpath variables
result_xpath = '//div[@class="results_links results_links_deep web-result"]'  # noqa
url_xpath = './/a[@class="large"]/@href'
title_xpath = './/a[@class="large"]'
content_xpath = './/div[@class="snippet"]'


# do search-request
def request(query, params):
    offset = (params['pageno'] - 1) * 30

    if params['language'] == 'all':
        locale = 'en-us'
    else:
        locale = params['language'].replace('_', '-').lower()

    params['url'] = url.format(
        query=urlencode({'q': query, 'kl': locale}),
        offset=offset)

    return params


# get response from search-request
def response(resp):
    results = []

    doc = fromstring(resp.text)

    # parse results
    for r in doc.xpath(result_xpath):
        try:
            res_url = r.xpath(url_xpath)[-1]
        except:
            continue

        if not res_url:
            continue

        title = extract_text(r.xpath(title_xpath))
        content = extract_text(r.xpath(content_xpath))

        # append result
        results.append({'title': title,
                        'content': content,
                        'url': res_url})

    # return results
    return results