.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
27https_support = True
28
29
30def request(query, params): # pylint: disable=unused-argument
31
32 from_lang = params["from_lang"][2] # "english"
33 to_lang = params["to_lang"][2] # "german"
34 query = params["query"]
35
36 params["url"] = f"{base_url}/{from_lang}-{to_lang}-dictionary/{urllib.parse.quote_plus(query)}"
37 return params
38
39
41 for x in ["./i", "./span", "./button"]:
42 for n in node.xpath(x):
43 n.getparent().remove(n)
44
45
46def response(resp) -> EngineResults:
47 results = EngineResults()
48
49 item_list = []
50
51 if not resp.ok:
52 return results
53
54 dom = html.fromstring(resp.text)
55
56 for result in eval_xpath(dom, ".//table[@id='r']//tr"):
57
58 # each row is an Translations.Item
59
60 td_list = result.xpath("./td")
61 if len(td_list) != 2:
62 # ignore header columns "tr/th"
63 continue
64
65 col_from, col_to = td_list
66 _clean_up_node(col_from)
67
68 text = f"{extract_text(col_from)}"
69
70 synonyms = []
71 p_list = col_to.xpath(".//p")
72
73 for i, p_item in enumerate(p_list):
74
75 smpl: str = extract_text(p_list[i].xpath("./i[@class='smpl']")) # type: ignore
76 _clean_up_node(p_item)
77 p_text: str = extract_text(p_item) # type: ignore
78
79 if smpl:
80 p_text += " // " + smpl
81
82 if i == 0:
83 text += f" : {p_text}"
84 continue
85
86 synonyms.append(p_text)
87
88 item = results.types.Translations.Item(text=text, synonyms=synonyms)
89 item_list.append(item)
90
91 # the "autotranslate" of dictzone is loaded by the JS from URL:
92 # https://dictzone.com/trans/hello%20world/en_de
93
94 from_lang = resp.search_params["from_lang"][1] # "en"
95 to_lang = resp.search_params["to_lang"][1] # "de"
96 query = resp.search_params["query"]
97
98 # works only sometimes?
99 autotranslate = http_get(f"{base_url}/trans/{query}/{from_lang}_{to_lang}", timeout=1.0)
100 if autotranslate.ok and autotranslate.text:
101 item_list.insert(0, results.types.Translations.Item(text=autotranslate.text))
102
103 if item_list:
104 results.add(results.types.Translations(translations=item_list, url=resp.search_params["url"]))
105 return results
request(query, params)
Definition dictzone.py:30
EngineResults response(resp)
Definition dictzone.py:46