dictzone.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. Dictzone
  3. @website https://dictzone.com/
  4. @provide-api no
  5. @using-api no
  6. @results HTML (using search portal)
  7. @stable no (HTML can change)
  8. @parse url, title, content
  9. """
  10. import re
  11. from urlparse import urljoin
  12. from lxml import html
  13. from cgi import escape
  14. from searx.engines.xpath import extract_text
  15. from searx.utils import is_valid_lang
  16. categories = ['general']
  17. url = 'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
  18. weight = 100
  19. parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I)
  20. results_xpath = './/table[@id="r"]/tr'
  21. def request(query, params):
  22. m = parser_re.match(unicode(query, 'utf8'))
  23. if not m:
  24. return params
  25. from_lang, to_lang, query = m.groups()
  26. from_lang = is_valid_lang(from_lang)
  27. to_lang = is_valid_lang(to_lang)
  28. if not from_lang or not to_lang:
  29. return params
  30. params['url'] = url.format(from_lang=from_lang[2],
  31. to_lang=to_lang[2],
  32. query=query)
  33. return params
  34. def response(resp):
  35. results = []
  36. dom = html.fromstring(resp.text)
  37. for k, result in enumerate(dom.xpath(results_xpath)[1:]):
  38. try:
  39. from_result, to_results_raw = result.xpath('./td')
  40. except:
  41. continue
  42. to_results = []
  43. for to_result in to_results_raw.xpath('./p/a'):
  44. t = to_result.text_content()
  45. if t.strip():
  46. to_results.append(to_result.text_content())
  47. results.append({
  48. 'url': urljoin(resp.url, '?%d' % k),
  49. 'title': escape(from_result.text_content()),
  50. 'content': escape('; '.join(to_results))
  51. })
  52. return results