hostname_replace.py 853 B

1234567891011121314151617181920212223242526272829
  1. import re
  2. from urllib.parse import urlunparse
  3. from searx import settings
  4. from searx.plugins import logger
  5. from flask_babel import gettext
  6. name = gettext('Hostname replace')
  7. description = gettext('Rewrite result hostnames')
  8. default_on = False
  9. preference_section = 'general'
  10. plugin_id = 'hostname_replace'
  11. parsed = 'parsed_url'
  12. replacements = {re.compile(p): r for (p, r) in settings[plugin_id].items()} if plugin_id in settings else {}
  13. logger = logger.getChild(plugin_id)
  14. def on_result(request, search, result):
  15. if parsed not in result:
  16. return True
  17. for (pattern, replacement) in replacements.items():
  18. if pattern.search(result[parsed].netloc):
  19. result[parsed] = result[parsed]._replace(netloc=pattern.sub(replacement, result[parsed].netloc))
  20. result['url'] = urlunparse(result[parsed])
  21. return True