dictzone.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.utils import is_valid_lang
  15. categories = ['general']
  16. url = u'http://dictzone.com/{from_lang}-{to_lang}-dictionary/{query}'
  17. weight = 100
  18. parser_re = re.compile(u'.*?([a-z]+)-([a-z]+) ([^ ]+)$', re.I)
  19. results_xpath = './/table[@id="r"]/tr'
  20. def request(query, params):
  21. m = parser_re.match(unicode(query, 'utf8'))
  22. if not m:
  23. return params
  24. from_lang, to_lang, query = m.groups()
  25. from_lang = is_valid_lang(from_lang)
  26. to_lang = is_valid_lang(to_lang)
  27. if not from_lang or not to_lang:
  28. return params
  29. params['url'] = url.format(from_lang=from_lang[2],
  30. to_lang=to_lang[2],
  31. query=query)
  32. return params
  33. def response(resp):
  34. results = []
  35. dom = html.fromstring(resp.text)
  36. for k, result in enumerate(dom.xpath(results_xpath)[1:]):
  37. try:
  38. from_result, to_results_raw = result.xpath('./td')
  39. except:
  40. continue
  41. to_results = []
  42. for to_result in to_results_raw.xpath('./p/a'):
  43. t = to_result.text_content()
  44. if t.strip():
  45. to_results.append(to_result.text_content())
  46. results.append({
  47. 'url': urljoin(resp.url, '?%d' % k),
  48. 'title': escape(from_result.text_content()),
  49. 'content': escape('; '.join(to_results))
  50. })
  51. return results