tracker_url_remover.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import flask
  11. from searx.search import SearchWithPlugins
  12. from searx.extended_types import SXNG_Request
  13. from searx.result_types import Result, LegacyResult
  14. from searx.plugins import PluginCfg
  15. log = logging.getLogger("searx.plugins.tracker_url_remover")
  16. class SXNGPlugin(Plugin):
  17. """Remove trackers arguments from the returned URL."""
  18. id = "tracker_url_remover"
  19. def __init__(self, plg_cfg: "PluginCfg") -> None:
  20. super().__init__(plg_cfg)
  21. self.info = PluginInfo(
  22. id=self.id,
  23. name=gettext("Tracker URL remover"),
  24. description=gettext("Remove trackers arguments from the returned URL"),
  25. preference_section="privacy",
  26. )
  27. def init(self, app: "flask.Flask") -> bool:
  28. TRACKER_PATTERNS.init()
  29. return True
  30. def on_result(self, request: "SXNG_Request", search: "SearchWithPlugins", result: Result) -> bool:
  31. result.filter_urls(self.filter_url_field)
  32. return True
  33. @classmethod
  34. def filter_url_field(cls, result: "Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
  35. """Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
  36. If URL should be modified, the returned string is the new URL to use."""
  37. if not url_src:
  38. log.debug("missing a URL in field %s", field_name)
  39. return True
  40. return TRACKER_PATTERNS.clean_url(url=url_src)