3"""During the initialization phase, the plugin checks whether a ``hostnames:``
4configuration exists. If this is not the case, the plugin is not included in the
5PluginStorage (it is not available for selection).
7- ``hostnames.replace``: A **mapping** of regular expressions to hostnames to be
8 replaced by other hostnames.
14 '(.*\\.)?youtube\\.com$': 'invidious.example.com'
15 '(.*\\.)?youtu\\.be$': 'invidious.example.com'
18- ``hostnames.remove``: A **list** of regular expressions of the hostnames whose
19 results should be taken from the results list.
25 - '(.*\\.)?facebook.com$'
28- ``hostnames.high_priority``: A **list** of regular expressions for hostnames
29 whose result should be given higher priority. The results from these hosts are
30 arranged higher in the results list.
36 - '(.*\\.)?wikipedia.org$'
39- ``hostnames.lower_priority``: A **list** of regular expressions for hostnames
40 whose result should be given lower priority. The results from these hosts are
41 arranged lower in the results list.
47 - '(.*\\.)?google(\\..*)?$'
50If the URL matches the pattern of ``high_priority`` AND ``low_priority``, the
51higher priority wins over the lower priority.
53Alternatively, you can also specify a file name for the **mappings** or
54**lists** to load these from an external file:
59 replace: 'rewrite-hosts.yml'
61 - '(.*\\.)?facebook.com$'
64 - '(.*\\.)?google(\\..*)?$'
67 - '(.*\\.)?wikipedia.org$'
70The ``rewrite-hosts.yml`` from the example above must be in the folder in which
71the ``settings.yml`` file is already located (``/etc/searxng``). The file then
72only contains the lists or the mapping tables without further information on the
73namespaces. In the example above, this would be a mapping table that looks
78 '(.*\\.)?youtube\\.com$': 'invidious.example.com'
79 '(.*\\.)?youtu\\.be$': 'invidious.example.com'
86from urllib.parse
import urlunparse, urlparse
88from flask_babel
import gettext
90from searx
import settings
104REPLACE: dict[re.Pattern, str] = {}
111 """Rewrite hostnames, remove results or prioritize them."""
119 name=gettext(
"Hostnames plugin"),
120 description=gettext(
"Rewrite hostnames and remove or prioritize results based on the hostname"),
121 preference_section=
"general",
124 def on_result(self, request:
"SXNG_Request", search:
"SearchWithPlugins", result:
"Result") -> bool:
126 for pattern
in REMOVE:
127 if result.parsed_url
and pattern.search(result.parsed_url.netloc):
134 result.filter_urls(filter_url_field)
136 if isinstance(result, (MainResult, LegacyResult)):
138 if result.parsed_url
and pattern.search(result.parsed_url.netloc):
139 result.priority =
"low"
142 if result.parsed_url
and pattern.search(result.parsed_url.netloc):
143 result.priority =
"high"
147 def init(self, app:
"flask.Flask") -> bool:
148 global REPLACE, REMOVE, HIGH, LOW
150 if not settings.get(self.
id):
162 setting_value = settings.get(self.
id, {}).get(settings_key)
164 if not setting_value:
168 if isinstance(setting_value, str):
169 setting_value = get_yaml_cfg(setting_value)
171 if isinstance(setting_value, list):
172 return {re.compile(r)
for r
in setting_value}
174 if isinstance(setting_value, dict):
175 return {re.compile(p): r
for (p, r)
in setting_value.items()}
180def filter_url_field(result:
"Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
181 """Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
182 If URL should be modified, the returned string is the new URL to use."""
185 log.debug(
"missing a URL in field %s", field_name)
188 url_src_parsed = urlparse(url=url_src)
190 for pattern
in REMOVE:
191 if pattern.search(url_src_parsed.netloc):
194 for pattern, replacement
in REPLACE.items():
195 if pattern.search(url_src_parsed.netloc):
196 new_url = url_src_parsed._replace(netloc=pattern.sub(replacement, url_src_parsed.netloc))
197 new_url = urlunparse(new_url)
bool init(self, "flask.Flask" app)
None __init__(self, "PluginCfg" plg_cfg)
bool on_result(self, "SXNG_Request" request, "SearchWithPlugins" search, "Result" result)
dict[re.Pattern, str]|set|None _load_regular_expressions(self, settings_key)
bool|str filter_url_field("Result|LegacyResult" result, str field_name, str url_src)