.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
tracker_url_remover.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2# pylint: disable=missing-module-docstring, unused-argument
3
4import logging
5import typing as t
6
7from flask_babel import gettext # pyright: ignore[reportUnknownVariableType]
8
9from searx.data import TRACKER_PATTERNS
10
11from . import Plugin, PluginInfo
12
13if t.TYPE_CHECKING:
14 import flask
15 from searx.search import SearchWithPlugins
16 from searx.extended_types import SXNG_Request
17 from searx.result_types import Result, LegacyResult # pyright: ignore[reportPrivateLocalImportUsage]
18 from searx.plugins import PluginCfg
19
20
21log = logging.getLogger("searx.plugins.tracker_url_remover")
22
23
24@t.final
26 """Remove trackers arguments from the returned URL."""
27
28 id = "tracker_url_remover"
29
30 def __init__(self, plg_cfg: "PluginCfg") -> None:
31
32 super().__init__(plg_cfg)
34 id=self.id,
35 name=gettext("Tracker URL remover"),
36 description=gettext("Remove trackers arguments from the returned URL"),
37 preference_section="privacy",
38 )
39
40 def init(self, app: "flask.Flask") -> bool:
41 TRACKER_PATTERNS.init()
42 return True
43
44 def on_result(self, request: "SXNG_Request", search: "SearchWithPlugins", result: "Result") -> bool:
45
46 result.filter_urls(self.filter_url_field)
47 return True
48
49 @classmethod
50 def filter_url_field(cls, result: "Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
51 """Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
52 If URL should be modified, the returned string is the new URL to use."""
53
54 if not url_src:
55 log.debug("missing a URL in field %s", field_name)
56 return True
57
58 return TRACKER_PATTERNS.clean_url(url=url_src)
bool on_result(self, "SXNG_Request" request, "SearchWithPlugins" search, "Result" result)