not_evil.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. not Evil (Onions)
  4. """
  5. from urllib.parse import urlencode
  6. from lxml import html
  7. from searx.engines.xpath import extract_text
  8. # about
  9. about = {
  10. "website": 'http://hss3uro2hsxfogfq.onion',
  11. "wikidata_id": None,
  12. "official_api_documentation": 'http://hss3uro2hsxfogfq.onion/api.htm',
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'HTML',
  16. }
  17. # engine dependent config
  18. categories = ['onions']
  19. paging = True
  20. page_size = 20
  21. # search-url
  22. base_url = 'http://hss3uro2hsxfogfq.onion/'
  23. search_url = 'index.php?{query}&hostLimit=20&start={pageno}&numRows={page_size}'
  24. # specific xpath variables
  25. results_xpath = '//*[@id="content"]/div/p'
  26. url_xpath = './span[1]'
  27. title_xpath = './a[1]'
  28. content_xpath = './text()'
  29. # do search-request
  30. def request(query, params):
  31. offset = (params['pageno'] - 1) * page_size
  32. params['url'] = base_url + search_url.format(pageno=offset,
  33. query=urlencode({'q': query}),
  34. page_size=page_size)
  35. return params
  36. # get response from search-request
  37. def response(resp):
  38. results = []
  39. # needed because otherwise requests guesses wrong encoding
  40. resp.encoding = 'utf8'
  41. dom = html.fromstring(resp.text)
  42. # parse results
  43. for result in dom.xpath(results_xpath):
  44. url = extract_text(result.xpath(url_xpath)[0])
  45. title = extract_text(result.xpath(title_xpath)[0])
  46. content = extract_text(result.xpath(content_xpath))
  47. # append result
  48. results.append({'url': url,
  49. 'title': title,
  50. 'content': content,
  51. 'is_onion': True})
  52. return results