.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
4from __future__ import annotations
5
6import logging
7import typing
8
9from flask_babel import gettext
10
11from searx.data import TRACKER_PATTERNS
12
13from . import Plugin, PluginInfo
14
15if typing.TYPE_CHECKING:
16 import flask
17 from searx.search import SearchWithPlugins
18 from searx.extended_types import SXNG_Request
19 from searx.result_types import Result, LegacyResult
20 from searx.plugins import PluginCfg
21
22
23log = logging.getLogger("searx.plugins.tracker_url_remover")
24
25
27 """Remove trackers arguments from the returned URL."""
28
29 id = "tracker_url_remover"
30
31 def __init__(self, plg_cfg: "PluginCfg") -> None:
32
33 super().__init__(plg_cfg)
35 id=self.id,
36 name=gettext("Tracker URL remover"),
37 description=gettext("Remove trackers arguments from the returned URL"),
38 preference_section="privacy",
39 )
40
41 def init(self, app: "flask.Flask") -> bool:
42 TRACKER_PATTERNS.init()
43 return True
44
45 def on_result(self, request: "SXNG_Request", search: "SearchWithPlugins", result: Result) -> bool:
46
47 result.filter_urls(self.filter_url_field)
48 return True
49
50 @classmethod
51 def filter_url_field(cls, result: "Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
52 """Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
53 If URL should be modified, the returned string is the new URL to use."""
54
55 if not url_src:
56 log.debug("missing a URL in field %s", field_name)
57 return True
58
59 return TRACKER_PATTERNS.clean_url(url=url_src)
bool on_result(self, "SXNG_Request" request, "SearchWithPlugins" search, Result result)