wordnik.py 1.8 KB

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