.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.plugins.hostnames Namespace Reference

Functions

 _load_regular_expressions (settings_key)
 
 _matches_parsed_url (result, pattern)
 
 on_result (_request, _search, result)
 

Variables

 name = gettext('Hostnames plugin')
 
 description = gettext('Rewrite hostnames, remove results or prioritize them based on the hostname')
 
bool default_on = False
 
str preference_section = 'general'
 
str plugin_id = 'hostnames'
 
 logger = logger.getChild(plugin_id)
 
str parsed = 'parsed_url'
 
list _url_fields = ['iframe_src', 'audio_src']
 
 replacements = _load_regular_expressions('replace')
 
 removables = _load_regular_expressions('remove')
 
 high_priority = _load_regular_expressions('high_priority')
 
 low_priority = _load_regular_expressions('low_priority')
 

Detailed Description

.. attention::

    The **"Hostname replace"** plugin has been replace by **"Hostnames
    plugin"**, see :pull:`3463` & :pull:`3552`.

The **Hostnames plugin** can be enabled by adding it to the
``enabled_plugins`` **list** in the ``setting.yml`` like so.

  .. code:: yaml

     enabled_plugins:
       - 'Hostnames plugin'
       ...

- ``hostnames.replace``: A **mapping** of regular expressions to hostnames to be
  replaced by other hostnames.

  .. code:: yaml

     hostnames:
       replace:
         '(.*\\.)?youtube\\.com$': 'invidious.example.com'
         '(.*\\.)?youtu\\.be$': 'invidious.example.com'
         ...

- ``hostnames.remove``: A **list** of regular expressions of the hostnames whose
  results should be taken from the results list.

  .. code:: yaml

     hostnames:
       remove:
         - '(.*\\.)?facebook.com$'
         - ...

- ``hostnames.high_priority``: A **list** of regular expressions for hostnames
  whose result should be given higher priority. The results from these hosts are
  arranged higher in the results list.

  .. code:: yaml

     hostnames:
       high_priority:
         - '(.*\\.)?wikipedia.org$'
         - ...

- ``hostnames.lower_priority``: A **list** of regular expressions for hostnames
  whose result should be given lower priority. The results from these hosts are
  arranged lower in the results list.

  .. code:: yaml

     hostnames:
       low_priority:
         - '(.*\\.)?google(\\..*)?$'
         - ...

If the URL matches the pattern of ``high_priority`` AND ``low_priority``, the
higher priority wins over the lower priority.

Alternatively, you can also specify a file name for the **mappings** or
**lists** to load these from an external file:

.. code:: yaml

   hostnames:
     replace: 'rewrite-hosts.yml'
     remove:
       - '(.*\\.)?facebook.com$'
       - ...
     low_priority:
       - '(.*\\.)?google(\\..*)?$'
       - ...
     high_priority:
       - '(.*\\.)?wikipedia.org$'
       - ...

The ``rewrite-hosts.yml`` from the example above must be in the folder in which
the ``settings.yml`` file is already located (``/etc/searxng``). The file then
only contains the lists or the mapping tables without further information on the
namespaces.  In the example above, this would be a mapping table that looks
something like this:

.. code:: yaml

   '(.*\\.)?youtube\\.com$': 'invidious.example.com'
   '(.*\\.)?youtu\\.be$': 'invidious.example.com'

Function Documentation

◆ _load_regular_expressions()

searx.plugins.hostnames._load_regular_expressions ( settings_key)
protected

Definition at line 115 of file hostnames.py.

115def _load_regular_expressions(settings_key):
116 setting_value = settings.get(plugin_id, {}).get(settings_key)
117
118 if not setting_value:
119 return {}
120
121 # load external file with configuration
122 if isinstance(setting_value, str):
123 setting_value = get_yaml_cfg(setting_value)
124
125 if isinstance(setting_value, list):
126 return {re.compile(r) for r in setting_value}
127
128 if isinstance(setting_value, dict):
129 return {re.compile(p): r for (p, r) in setting_value.items()}
130
131 return {}
132
133

◆ _matches_parsed_url()

searx.plugins.hostnames._matches_parsed_url ( result,
pattern )
protected

Definition at line 140 of file hostnames.py.

140def _matches_parsed_url(result, pattern):
141 return parsed in result and pattern.search(result[parsed].netloc)
142
143

Referenced by searx.plugins.hostnames.on_result().

+ Here is the caller graph for this function:

◆ on_result()

searx.plugins.hostnames.on_result ( _request,
_search,
result )

Definition at line 144 of file hostnames.py.

144def on_result(_request, _search, result):
145 for pattern, replacement in replacements.items():
146 if _matches_parsed_url(result, pattern):
147 # logger.debug(result['url'])
148 result[parsed] = result[parsed]._replace(netloc=pattern.sub(replacement, result[parsed].netloc))
149 result['url'] = urlunparse(result[parsed])
150 # logger.debug(result['url'])
151
152 for url_field in _url_fields:
153 if not result.get(url_field):
154 continue
155
156 url_src = urlparse(result[url_field])
157 if pattern.search(url_src.netloc):
158 url_src = url_src._replace(netloc=pattern.sub(replacement, url_src.netloc))
159 result[url_field] = urlunparse(url_src)
160
161 for pattern in removables:
162 if _matches_parsed_url(result, pattern):
163 return False
164
165 for url_field in _url_fields:
166 if not result.get(url_field):
167 continue
168
169 url_src = urlparse(result[url_field])
170 if pattern.search(url_src.netloc):
171 del result[url_field]
172
173 for pattern in low_priority:
174 if _matches_parsed_url(result, pattern):
175 result['priority'] = 'low'
176
177 for pattern in high_priority:
178 if _matches_parsed_url(result, pattern):
179 result['priority'] = 'high'
180
181 return True

References searx.plugins.hostnames._matches_parsed_url().

+ Here is the call graph for this function:

Variable Documentation

◆ _url_fields

list searx.plugins.hostnames._url_fields = ['iframe_src', 'audio_src']
protected

Definition at line 112 of file hostnames.py.

◆ default_on

bool searx.plugins.hostnames.default_on = False

Definition at line 105 of file hostnames.py.

◆ description

searx.plugins.hostnames.description = gettext('Rewrite hostnames, remove results or prioritize them based on the hostname')

Definition at line 104 of file hostnames.py.

◆ high_priority

searx.plugins.hostnames.high_priority = _load_regular_expressions('high_priority')

Definition at line 136 of file hostnames.py.

◆ logger

searx.plugins.hostnames.logger = logger.getChild(plugin_id)

Definition at line 110 of file hostnames.py.

◆ low_priority

searx.plugins.hostnames.low_priority = _load_regular_expressions('low_priority')

Definition at line 137 of file hostnames.py.

◆ name

searx.plugins.hostnames.name = gettext('Hostnames plugin')

Definition at line 103 of file hostnames.py.

◆ parsed

str searx.plugins.hostnames.parsed = 'parsed_url'

Definition at line 111 of file hostnames.py.

◆ plugin_id

str searx.plugins.hostnames.plugin_id = 'hostnames'

Definition at line 108 of file hostnames.py.

◆ preference_section

str searx.plugins.hostnames.preference_section = 'general'

Definition at line 106 of file hostnames.py.

◆ removables

searx.plugins.hostnames.removables = _load_regular_expressions('remove')

Definition at line 135 of file hostnames.py.

◆ replacements

searx.plugins.hostnames.replacements = _load_regular_expressions('replace')

Definition at line 134 of file hostnames.py.