ahmia_filter.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. '''
  2. SPDX-License-Identifier: AGPL-3.0-or-later
  3. '''
  4. from hashlib import md5
  5. from os.path import join
  6. from urllib.parse import urlparse
  7. from searx import searx_dir
  8. name = "Ahmia blacklist"
  9. description = "Filter out onion results that appear in Ahmia's blacklist. (See https://ahmia.fi/blacklist)"
  10. default_on = True
  11. preference_section = 'onions'
  12. ahmia_blacklist = None
  13. def get_ahmia_blacklist():
  14. global ahmia_blacklist
  15. if not ahmia_blacklist:
  16. with open(join(join(searx_dir, "data"), "ahmia_blacklist.txt"), 'r') as f:
  17. ahmia_blacklist = f.read().split()
  18. return ahmia_blacklist
  19. def not_blacklisted(result):
  20. if not result.get('is_onion'):
  21. return True
  22. result_hash = md5(urlparse(result.get('url')).hostname.encode()).hexdigest()
  23. return result_hash not in get_ahmia_blacklist()
  24. def post_search(request, search):
  25. filtered_results = list(filter(not_blacklisted, search.result_container._merged_results))
  26. search.result_container._merged_results = filtered_results
  27. return True