.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.data.tracker_patterns.TrackerPatternsDB Class Reference

Classes

class  Fields

Public Member Functions

 __init__ (self)
 init (self)
 load (self)
 add (self, RuleType rule)
Iterator[RuleTyperules (self)
Iterator[RuleTypeiter_clear_list (self)
bool|str clean_url (self, str url)

Public Attributes

 cache = get_cache()

Static Public Attributes

str ctx_name = "data_tracker_patterns"
str json_file = pathlib.Path(__file__).parent / "tracker_patterns.json"
list CLEAR_LIST_URL

Detailed Description

Definition at line 21 of file tracker_patterns.py.

Constructor & Destructor Documentation

◆ __init__()

searx.data.tracker_patterns.TrackerPatternsDB.__init__ ( self)

Definition at line 40 of file tracker_patterns.py.

40 def __init__(self):
41 self.cache = get_cache()
42

Member Function Documentation

◆ add()

searx.data.tracker_patterns.TrackerPatternsDB.add ( self,
RuleType rule )

Definition at line 56 of file tracker_patterns.py.

56 def add(self, rule: RuleType):
57 self.cache.set(
58 key=rule[self.Fields.url_regexp],
59 value=(
60 rule[self.Fields.url_ignore],
61 rule[self.Fields.del_args],
62 ),
63 ctx=self.ctx_name,
64 expire=None,
65 )
66

References searx.data.currencies.CurrenciesDB.cache, cache, and ctx_name.

Referenced by load().

Here is the caller graph for this function:

◆ clean_url()

bool | str searx.data.tracker_patterns.TrackerPatternsDB.clean_url ( self,
str url )
The URL arguments are normalized and cleaned of tracker parameters.

Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
If URL should be modified, the returned string is the new URL to use.

Definition at line 91 of file tracker_patterns.py.

91 def clean_url(self, url: str) -> bool | str:
92 """The URL arguments are normalized and cleaned of tracker parameters.
93
94 Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
95 If URL should be modified, the returned string is the new URL to use.
96 """
97
98 new_url = url
99 parsed_new_url = urlparse(url=new_url)
100
101 for rule in self.rules():
102
103 if not re.match(rule[self.Fields.url_regexp], new_url):
104 # no match / ignore pattern
105 continue
106
107 do_ignore = False
108 for pattern in rule[self.Fields.url_ignore]:
109 if re.match(pattern, new_url):
110 do_ignore = True
111 break
112
113 if do_ignore:
114 # pattern is in the list of exceptions / ignore pattern
115 # HINT:
116 # we can't break the outer pattern loop since we have
117 # overlapping urlPattern like ".*"
118 continue
119
120 # remove tracker arguments from the url-query part
121 query_args: list[tuple[str, str]] = list(parse_qsl(parsed_new_url.query))
122
123 for name, val in query_args.copy():
124 # remove URL arguments
125 for pattern in rule[self.Fields.del_args]:
126 if re.match(pattern, name):
127 log.debug("TRACKER_PATTERNS: %s remove tracker arg: %s='%s'", parsed_new_url.netloc, name, val)
128 query_args.remove((name, val))
129
130 parsed_new_url = parsed_new_url._replace(query=urlencode(query_args))
131 new_url = urlunparse(parsed_new_url)
132
133 if new_url != url:
134 return new_url
135
136 return True
137
138

References rules().

Here is the call graph for this function:

◆ init()

searx.data.tracker_patterns.TrackerPatternsDB.init ( self)

Definition at line 43 of file tracker_patterns.py.

43 def init(self):
44 if self.cache.properties("tracker_patterns loaded") != "OK":
45 self.load()
46 self.cache.properties.set("tracker_patterns loaded", "OK")
47 # F I X M E:
48 # do we need a maintenance .. rember: database is stored
49 # in /tmp and will be rebuild during the reboot anyway
50

References searx.data.currencies.CurrenciesDB.cache, cache, searx.data.currencies.CurrenciesDB.load(), and load().

Referenced by searx.sqlitedb.SQLiteAppl.connect(), searx.sqlitedb.SQLiteAppl.DB(), and rules().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ iter_clear_list()

Iterator[RuleType] searx.data.tracker_patterns.TrackerPatternsDB.iter_clear_list ( self)

Definition at line 72 of file tracker_patterns.py.

72 def iter_clear_list(self) -> Iterator[RuleType]:
73 resp = None
74 for url in self.CLEAR_LIST_URL:
75 resp = httpx.get(url, timeout=3)
76 if resp.status_code == 200:
77 break
78 log.warning(f"TRACKER_PATTERNS: ClearURL ignore HTTP {resp.status_code} {url}")
79
80 if resp is None:
81 log.error("TRACKER_PATTERNS: failed fetching ClearURL rule lists")
82 return
83
84 for rule in resp.json()["providers"].values():
85 yield (
86 rule["urlPattern"].replace("\\\\", "\\"), # fix javascript regex syntax
87 [exc.replace("\\\\", "\\") for exc in rule.get("exceptions", [])],
88 rule.get("rules", []),
89 )
90

References CLEAR_LIST_URL.

Referenced by load().

Here is the caller graph for this function:

◆ load()

searx.data.tracker_patterns.TrackerPatternsDB.load ( self)

Definition at line 51 of file tracker_patterns.py.

51 def load(self):
52 log.debug("init searx.data.TRACKER_PATTERNS")
53 for rule in self.iter_clear_list():
54 self.add(rule)
55

References add(), and iter_clear_list().

Referenced by init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rules()

Iterator[RuleType] searx.data.tracker_patterns.TrackerPatternsDB.rules ( self)

Definition at line 67 of file tracker_patterns.py.

67 def rules(self) -> Iterator[RuleType]:
68 self.init()
69 for key, value in self.cache.pairs(ctx=self.ctx_name):
70 yield key, value[0], value[1]
71

References searx.data.currencies.CurrenciesDB.cache, cache, ctx_name, searx.cache.ExpireCacheSQLite.init(), searx.data.currencies.CurrenciesDB.init(), and init().

Referenced by clean_url().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ cache

searx.data.tracker_patterns.TrackerPatternsDB.cache = get_cache()

Definition at line 41 of file tracker_patterns.py.

Referenced by add(), init(), and rules().

◆ CLEAR_LIST_URL

list searx.data.tracker_patterns.TrackerPatternsDB.CLEAR_LIST_URL
static
Initial value:
= [
# ClearURL rule lists, the first one that responds HTTP 200 is used
"https://rules1.clearurls.xyz/data.minify.json",
"https://rules2.clearurls.xyz/data.minify.json",
"https://raw.githubusercontent.com/ClearURLs/Rules/refs/heads/master/data.min.json",
]

Definition at line 27 of file tracker_patterns.py.

Referenced by iter_clear_list().

◆ ctx_name

str searx.data.tracker_patterns.TrackerPatternsDB.ctx_name = "data_tracker_patterns"
static

Definition at line 24 of file tracker_patterns.py.

Referenced by add(), and rules().

◆ json_file

str searx.data.tracker_patterns.TrackerPatternsDB.json_file = pathlib.Path(__file__).parent / "tracker_patterns.json"
static

Definition at line 25 of file tracker_patterns.py.


The documentation for this class was generated from the following file: