.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
3
4from __future__ import annotations
5import typing
6
7import re
8from urllib.parse import urlunparse, parse_qsl, urlencode
9
10from flask_babel import gettext
11
12from searx.plugins import Plugin, PluginInfo
13
14if typing.TYPE_CHECKING:
15 from searx.search import SearchWithPlugins
16 from searx.extended_types import SXNG_Request
17 from searx.result_types import Result
18 from searx.plugins import PluginCfg
19
20regexes = {
21 re.compile(r'utm_[^&]+'),
22 re.compile(r'(wkey|wemail)[^&]*'),
23 re.compile(r'(_hsenc|_hsmi|hsCtaTracking|__hssc|__hstc|__hsfp)[^&]*'),
24 re.compile(r'&$'),
25}
26
27
29 """Remove trackers arguments from the returned URL"""
30
31 id = "tracker_url_remover"
32
33 def __init__(self, plg_cfg: "PluginCfg") -> None:
34 super().__init__(plg_cfg)
36 id=self.id,
37 name=gettext("Tracker URL remover"),
38 description=gettext("Remove trackers arguments from the returned URL"),
39 preference_section="privacy",
40 )
41
43 self, request: "SXNG_Request", search: "SearchWithPlugins", result: Result
44 ) -> bool: # pylint: disable=unused-argument
45 if not result.parsed_url:
46 return True
47
48 parsed_query: list[tuple[str, str]] = parse_qsl(result.parsed_url.query)
49 for name_value in list(parsed_query):
50 param_name = name_value[0]
51 for reg in regexes:
52 if reg.match(param_name):
53 parsed_query.remove(name_value)
54 result.parsed_url = result.parsed_url._replace(query=urlencode(parsed_query))
55 result.url = urlunparse(result.parsed_url)
56 break
57
58 return True
bool on_result(self, "SXNG_Request" request, "SearchWithPlugins" search, Result result)