2021-04-08 09:58:00 +10:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2025-07-24 07:42:31 +02:00
|
|
|
"""Wordnik (general)"""
|
2021-04-08 09:58:00 +10:00
|
|
|
|
|
|
|
|
from lxml.html import fromstring
|
|
|
|
|
from searx.utils import extract_text
|
2025-07-24 07:42:31 +02:00
|
|
|
|
|
|
|
|
from searx.result_types import EngineResults
|
2021-04-08 09:58:00 +10:00
|
|
|
|
|
|
|
|
# about
|
|
|
|
|
about = {
|
|
|
|
|
"website": 'https://www.wordnik.com',
|
|
|
|
|
"wikidata_id": 'Q8034401',
|
|
|
|
|
"official_api_documentation": None,
|
|
|
|
|
"use_official_api": False,
|
|
|
|
|
"require_api_key": False,
|
|
|
|
|
"results": 'HTML',
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-24 07:42:31 +02:00
|
|
|
categories = ['dictionaries', 'define']
|
2021-04-08 09:58:00 +10:00
|
|
|
paging = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def request(query, params):
|
2024-11-26 09:56:41 +01:00
|
|
|
params['url'] = f"https://www.wordnik.com/words/{query}"
|
2021-04-08 09:58:00 +10:00
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
2025-07-24 07:42:31 +02:00
|
|
|
results = EngineResults()
|
2021-04-08 09:58:00 +10:00
|
|
|
|
|
|
|
|
dom = fromstring(resp.text)
|
|
|
|
|
|
|
|
|
|
for src in dom.xpath('//*[@id="define"]//h3[@class="source"]'):
|
2025-07-24 07:42:31 +02:00
|
|
|
item = results.types.Translations.Item(text="")
|
2021-04-08 09:58:00 +10:00
|
|
|
for def_item in src.xpath('following-sibling::ul[1]/li'):
|
|
|
|
|
def_abbr = extract_text(def_item.xpath('.//abbr')).strip()
|
|
|
|
|
def_text = extract_text(def_item).strip()
|
|
|
|
|
if def_abbr:
|
2021-12-27 09:26:22 +01:00
|
|
|
def_text = def_text[len(def_abbr) :].strip()
|
2021-04-08 09:58:00 +10:00
|
|
|
|
2025-07-24 07:42:31 +02:00
|
|
|
# use first result as summary
|
|
|
|
|
if not item.text:
|
|
|
|
|
item.text = def_text
|
|
|
|
|
item.definitions.append(def_text)
|
2021-04-08 09:58:00 +10:00
|
|
|
|
2025-07-24 07:42:31 +02:00
|
|
|
results.add(results.types.Translations(translations=[item], url=resp.search_params["url"]))
|
2021-04-08 09:58:00 +10:00
|
|
|
|
|
|
|
|
return results
|