tracker_url_remover.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring, unused-argument
  3. from __future__ import annotations
  4. import logging
  5. import typing
  6. from flask_babel import gettext
  7. from searx.data import TRACKER_PATTERNS
  8. from . import Plugin, PluginInfo
  9. if typing.TYPE_CHECKING:
  10. from searx.search import SearchWithPlugins
  11. from searx.extended_types import SXNG_Request
  12. from searx.result_types import Result, LegacyResult
  13. from searx.plugins import PluginCfg
  14. log = logging.getLogger("searx.plugins.tracker_url_remover")
  15. class SXNGPlugin(Plugin):
  16. """Remove trackers arguments from the returned URL."""
  17. id = "tracker_url_remover"
  18. def __init__(self, plg_cfg: "PluginCfg") -> None:
  19. super().__init__(plg_cfg)
  20. self.info = PluginInfo(
  21. id=self.id,
  22. name=gettext("Tracker URL remover"),
  23. description=gettext("Remove trackers arguments from the returned URL"),
  24. preference_section="privacy",
  25. )
  26. def on_result(self, request: "SXNG_Request", search: "SearchWithPlugins", result: Result) -> bool:
  27. result.filter_urls(self.filter_url_field)
  28. return True
  29. @classmethod
  30. def filter_url_field(cls, result: "Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
  31. """Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
  32. If URL should be modified, the returned string is the new URL to use."""
  33. if not url_src:
  34. log.debug("missing a URL in field %s", field_name)
  35. return True
  36. return TRACKER_PATTERNS.clean_url(url=url_src)