.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
dictzone.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3 Dictzone
4"""
5
6import urllib.parse
7from lxml import html
8
9from searx.utils import eval_xpath, extract_text
10from searx.result_types import EngineResults
11from searx.network import get as http_get # https://github.com/searxng/searxng/issues/762
12
13# about
14about = {
15 "website": 'https://dictzone.com/',
16 "wikidata_id": None,
17 "official_api_documentation": None,
18 "use_official_api": False,
19 "require_api_key": False,
20 "results": 'HTML',
21}
22
23engine_type = 'online_dictionary'
24categories = ['general', 'translate']
25base_url = "https://dictzone.com"
26weight = 100
27
28
29def request(query, params): # pylint: disable=unused-argument
30
31 from_lang = params["from_lang"][2] # "english"
32 to_lang = params["to_lang"][2] # "german"
33 query = params["query"]
34
35 params["url"] = f"{base_url}/{from_lang}-{to_lang}-dictionary/{urllib.parse.quote_plus(query)}"
36 return params
37
38
40 for x in ["./i", "./span", "./button"]:
41 for n in node.xpath(x):
42 n.getparent().remove(n)
43
44
45def response(resp) -> EngineResults:
46 results = EngineResults()
47
48 item_list = []
49
50 if not resp.ok:
51 return results
52
53 dom = html.fromstring(resp.text)
54
55 for result in eval_xpath(dom, ".//table[@id='r']//tr"):
56
57 # each row is an Translations.Item
58
59 td_list = result.xpath("./td")
60 if len(td_list) != 2:
61 # ignore header columns "tr/th"
62 continue
63
64 col_from, col_to = td_list
65 _clean_up_node(col_from)
66
67 text = f"{extract_text(col_from)}"
68
69 synonyms = []
70 p_list = col_to.xpath(".//p")
71
72 for i, p_item in enumerate(p_list):
73
74 smpl: str = extract_text(p_list[i].xpath("./i[@class='smpl']")) # type: ignore
75 _clean_up_node(p_item)
76 p_text: str = extract_text(p_item) # type: ignore
77
78 if smpl:
79 p_text += " // " + smpl
80
81 if i == 0:
82 text += f" : {p_text}"
83 continue
84
85 synonyms.append(p_text)
86
87 item = results.types.Translations.Item(text=text, synonyms=synonyms)
88 item_list.append(item)
89
90 # the "autotranslate" of dictzone is loaded by the JS from URL:
91 # https://dictzone.com/trans/hello%20world/en_de
92
93 from_lang = resp.search_params["from_lang"][1] # "en"
94 to_lang = resp.search_params["to_lang"][1] # "de"
95 query = resp.search_params["query"]
96
97 # works only sometimes?
98 autotranslate = http_get(f"{base_url}/trans/{query}/{from_lang}_{to_lang}", timeout=1.0)
99 if autotranslate.ok and autotranslate.text:
100 item_list.insert(0, results.types.Translations.Item(text=autotranslate.text))
101
102 if item_list:
103 results.add(results.types.Translations(translations=item_list, url=resp.search_params["url"]))
104 return results
request(query, params)
Definition dictzone.py:29
EngineResults response(resp)
Definition dictzone.py:45