dictzone.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Dictzone
  4. """
  5. from urllib.parse import urljoin
  6. from lxml import html
  7. from searx.utils import eval_xpath
  8. # about
  9. about = {
  10. "website": 'https://dictzone.com/',
  11. "wikidata_id": None,
  12. "official_api_documentation": None,
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'HTML',
  16. }
  17. engine_type = 'online_dictionary'
  18. categories = ['general']
  19. url = 'https://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
  20. weight = 100
  21. results_xpath = './/table[@id="r"]/tr'
  22. https_support = True
  23. def request(query, params):
  24. params['url'] = url.format(from_lang=params['from_lang'][2],
  25. to_lang=params['to_lang'][2],
  26. query=params['query'])
  27. return params
  28. def response(resp):
  29. results = []
  30. dom = html.fromstring(resp.text)
  31. for k, result in enumerate(eval_xpath(dom, results_xpath)[1:]):
  32. try:
  33. from_result, to_results_raw = eval_xpath(result, './td')
  34. except:
  35. continue
  36. to_results = []
  37. for to_result in eval_xpath(to_results_raw, './p/a'):
  38. t = to_result.text_content()
  39. if t.strip():
  40. to_results.append(to_result.text_content())
  41. results.append({
  42. 'url': urljoin(str(resp.url), '?%d' % k),
  43. 'title': from_result.text_content(),
  44. 'content': '; '.join(to_results)
  45. })
  46. return results