.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"
list CLEAR_LIST_URL

Detailed Description

Definition at line 20 of file tracker_patterns.py.

Constructor & Destructor Documentation

◆ __init__()

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

Definition at line 38 of file tracker_patterns.py.

38 def __init__(self):
39 self.cache = get_cache()
40

Member Function Documentation

◆ add()

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

Definition at line 55 of file tracker_patterns.py.

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

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 99 of file tracker_patterns.py.

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

References rules().

Here is the call graph for this function:

◆ init()

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

Definition at line 41 of file tracker_patterns.py.

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

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 71 of file tracker_patterns.py.

71 def iter_clear_list(self) -> Iterator[RuleType]:
72 resp = None
73 for url in self.CLEAR_LIST_URL:
74 log.debug("TRACKER_PATTERNS: Trying to fetch %s...", url)
75 try:
76 resp = http_get(url, timeout=3)
77
78 except HTTPError as exc:
79 log.warning("TRACKER_PATTERNS: HTTPError (%s) occured while fetching %s", url, exc)
80 continue
81
82 if resp.status_code != 200:
83 log.warning(f"TRACKER_PATTERNS: ClearURL ignore HTTP {resp.status_code} {url}")
84 continue
85
86 break
87
88 if resp is None:
89 log.error("TRACKER_PATTERNS: failed fetching ClearURL rule lists")
90 return
91
92 for rule in resp.json()["providers"].values():
93 yield (
94 rule["urlPattern"].replace("\\\\", "\\"), # fix javascript regex syntax
95 [exc.replace("\\\\", "\\") for exc in rule.get("exceptions", [])],
96 rule.get("rules", []),
97 )
98

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 50 of file tracker_patterns.py.

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

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 66 of file tracker_patterns.py.

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

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 39 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 25 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 23 of file tracker_patterns.py.

Referenced by add(), and rules().


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