.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
4import re
5from urllib.parse import urlunparse, parse_qsl, urlencode
6
7from flask_babel import gettext
8
9regexes = {
10 re.compile(r'utm_[^&]+'),
11 re.compile(r'(wkey|wemail)[^&]*'),
12 re.compile(r'(_hsenc|_hsmi|hsCtaTracking|__hssc|__hstc|__hsfp)[^&]*'),
13 re.compile(r'&$'),
14}
15
16name = gettext('Tracker URL remover')
17description = gettext('Remove trackers arguments from the returned URL')
18default_on = True
19preference_section = 'privacy'
20
21
22def on_result(_request, _search, result):
23 if 'parsed_url' not in result:
24 return True
25
26 query = result['parsed_url'].query
27
28 if query == "":
29 return True
30 parsed_query = parse_qsl(query)
31
32 changes = 0
33 for i, (param_name, _) in enumerate(list(parsed_query)):
34 for reg in regexes:
35 if reg.match(param_name):
36 parsed_query.pop(i - changes)
37 changes += 1
38 result['parsed_url'] = result['parsed_url']._replace(query=urlencode(parsed_query))
39 result['url'] = urlunparse(result['parsed_url'])
40 break
41
42 return True
on_result(_request, _search, result)