.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
wordnik.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Wordnik (general)"""
3
4from lxml.html import fromstring
5from searx.utils import extract_text
6
7from searx.result_types import EngineResults
8
9# about
10about = {
11 "website": 'https://www.wordnik.com',
12 "wikidata_id": 'Q8034401',
13 "official_api_documentation": None,
14 "use_official_api": False,
15 "require_api_key": False,
16 "results": 'HTML',
17}
18
19categories = ['dictionaries', 'define']
20paging = False
21
22
23def request(query, params):
24 params['url'] = f"https://www.wordnik.com/words/{query}"
25 return params
26
27
28def response(resp):
29 results = EngineResults()
30
31 dom = fromstring(resp.text)
32
33 for src in dom.xpath('//*[@id="define"]//h3[@class="source"]'):
34 item = results.types.Translations.Item(text="")
35 for def_item in src.xpath('following-sibling::ul[1]/li'):
36 def_abbr = extract_text(def_item.xpath('.//abbr')).strip()
37 def_text = extract_text(def_item).strip()
38 if def_abbr:
39 def_text = def_text[len(def_abbr) :].strip()
40
41 # use first result as summary
42 if not item.text:
43 item.text = def_text
44 item.definitions.append(def_text)
45
46 results.add(results.types.Translations(translations=[item], url=resp.search_params["url"]))
47
48 return results
request(query, params)
Definition wordnik.py:23