@@ -0,0 +1,77 @@
1+# SPDX-License-Identifier: AGPL-3.0-or-later
2+"""Wordnik (general)
3+
4+"""
5+
6+from lxml.html import fromstring
7+from searx import logger
8+from searx.utils import extract_text
9+from searx.raise_for_httperror import raise_for_httperror
10+
11+logger = logger.getChild('Wordnik engine')
12+
13+# about
14+about = {
15+ "website": 'https://www.wordnik.com',
16+ "wikidata_id": 'Q8034401',
17+ "official_api_documentation": None,
18+ "use_official_api": False,
19+ "require_api_key": False,
20+ "results": 'HTML',
21+}
22+
23+categories = ['general']
24+paging = False
25+
26+URL = 'https://www.wordnik.com'
27+SEARCH_URL = URL + '/words/{query}'
28+
29+
30+def request(query, params):
31+ params['url'] = SEARCH_URL.format(query=query)
32+ logger.debug(f"query_url --> {params['url']}")
33+ return params
34+
35+
36+def response(resp):
37+ results = []
38+
39+ raise_for_httperror(resp)
40+ dom = fromstring(resp.text)
41+ word = extract_text(dom.xpath('//*[@id="headword"]/text()'))
42+
43+ definitions = []
44+ for src in dom.xpath('//*[@id="define"]//h3[@class="source"]'):
45+ src_text = extract_text(src).strip()
46+ if src_text.startswith('from '):
47+ src_text = src_text[5:]
48+
49+ src_defs = []
50+ for def_item in src.xpath('following-sibling::ul[1]/li'):
51+ def_abbr = extract_text(def_item.xpath('.//abbr')).strip()
52+ def_text = extract_text(def_item).strip()
53+ if def_abbr:
54+ def_text = def_text[len(def_abbr):].strip()
55+ src_defs.append((def_abbr, def_text))
56+
57+ definitions.append((src_text, src_defs))
58+
59+ if not definitions:
60+ return results
61+
62+ infobox = ''
63+ for src_text, src_defs in definitions:
64+ infobox += f"<small>{src_text}</small>"
65+ infobox += "<ul>"
66+ for def_abbr, def_text in src_defs:
67+ if def_abbr:
68+ def_abbr += ": "
69+ infobox += f"<li><i>{def_abbr}</i> {def_text}</li>"
70+ infobox += "</ul>"
71+
72+ results.append({
73+ 'infobox': word,
74+ 'content': infobox,
75+ })
76+
77+ return results