summaryrefslogtreecommitdiff
path: root/searx/engines/not_evil.py
blob: e84f153bd69e7bcef5327ddeab902ac553648226 (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
"""
 not Evil (Onions)

 @website     http://hss3uro2hsxfogfq.onion
 @provide-api yes (http://hss3uro2hsxfogfq.onion/api.htm)

 @using-api   no
 @results     HTML
 @stable      no
 @parse       url, title, content
"""

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

# engine dependent config
categories = ['onions']
paging = True
page_size = 20

# search-url
base_url = 'http://hss3uro2hsxfogfq.onion/'
search_url = 'index.php?{query}&hostLimit=20&start={pageno}&numRows={page_size}'

# specific xpath variables
results_xpath = '//*[@id="content"]/div/p'
url_xpath = './span[1]'
title_xpath = './a[1]'
content_xpath = './text()'


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

    params['url'] = base_url + search_url.format(pageno=offset,
                                                 query=urlencode({'q': query}),
                                                 page_size=page_size)

    return params


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

    # needed because otherwise requests guesses wrong encoding
    resp.encoding = 'utf8'
    dom = html.fromstring(resp.text)

    # parse results
    for result in dom.xpath(results_xpath):
        url = extract_text(result.xpath(url_xpath)[0])
        title = extract_text(result.xpath(title_xpath)[0])
        content = extract_text(result.xpath(content_xpath))

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

    return results