summaryrefslogtreecommitdiff
path: root/searx/engines/dictzone.py
blob: acd682911311818411e462cd3dbd6657c8b414ba (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
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
 Dictzone
"""

from lxml import html
from searx.utils import eval_xpath

# about
about = {
    "website": 'https://dictzone.com/',
    "wikidata_id": None,
    "official_api_documentation": None,
    "use_official_api": False,
    "require_api_key": False,
    "results": 'HTML',
}

engine_type = 'online_dictionary'
categories = ['general', 'translate']
url = 'https://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
weight = 100

results_xpath = './/table[@id="r"]/tr'
https_support = True


def request(query, params):  # pylint: disable=unused-argument
    params['url'] = url.format(from_lang=params['from_lang'][2], to_lang=params['to_lang'][2], query=params['query'])

    return params


def response(resp):
    dom = html.fromstring(resp.text)

    translations = []
    for result in eval_xpath(dom, results_xpath)[1:]:
        try:
            from_result, to_results_raw = eval_xpath(result, './td')
        except:  # pylint: disable=bare-except
            continue

        to_results = []
        for to_result in eval_xpath(to_results_raw, './p/a'):
            t = to_result.text_content()
            if t.strip():
                to_results.append(to_result.text_content())

        translations.append(
            {
                'text': f"{from_result.text_content()} - {'; '.join(to_results)}",
            }
        )

    if translations:
        result = {
            'answer': translations[0]['text'],
            'translations': translations,
            'answer_type': 'translations',
        }

    return [result]