
    i              	       r   U d Z ddlZddlZddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZmZ d	d
lmZ ej*                  rddlZddlmZ ddlmZ ddlmZ ddlmZ i aeej@                  e!f   e"d<    e#       a$e#e"d<    e#       a%e#e"d<    e#       a&e#e"d<    G d de      Z'ddde!de!de(e!z  fdZ)y)a  During the initialization phase, the plugin checks whether a ``hostnames:``
configuration exists. If this is not the case, the plugin is not included in the
PluginStorage (it is not available for selection).

- ``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'

    N)
urlunparseurlparse)gettext)settings)
MainResultLegacyResult)get_yaml_cfg)Plugin
PluginInfo   )log)SearchWithPlugins)SXNG_Request)Result)	PluginCfgREPLACEREMOVEHIGHLOWc                        e Zd ZdZdZd fdZdddd	d
ddefdZdddefdZde	e
j                  ef   ez  dz  fdZ xZS )
SXNGPluginz5Rewrite hostnames, remove results or prioritize them.	hostnamesreturnNc                     t         |   |       t        | j                  t	        d      t	        d      d      | _        y )NzHostnames pluginzHRewrite hostnames and remove or prioritize results based on the hostnamegeneral)idnamedescriptionpreference_section)super__init__r   r   r   info)selfplg_cfg	__class__s     (/root/searxng/searx/plugins/hostnames.pyr!   zSXNGPlugin.__init__s   s8    !ww+, jk(	
	    requestr   searchr   resultr   c                    t         D ]6  }|j                  s|j                  |j                  j                        s6 y |j	                  t
               t        |t        t        f      rt        D ]<  }|j                  s|j                  |j                  j                        s6d|_
        > t        D ]<  }|j                  s|j                  |j                  j                        s6d|_
        > y)NFlowhighT)r   
parsed_urlr)   netlocfilter_urlsfilter_url_field
isinstancer   r   r   priorityr   )r#   r(   r)   r*   patterns        r&   	on_resultzSXNGPlugin.on_result|   s     	G  W^^F4E4E4L4L%M
 	 	+,fz<89 ,$$8I8I8P8P)Q&+FO,   -$$8I8I8P8P)Q&,FO- r'   appzflask.Flaskc                    t        j                  | j                        sy| j                  d      xs i a| j                  d      xs
 t               a| j                  d      xs
 t               a| j                  d      xs
 t               ay)NFreplaceremovehigh_prioritylow_priorityT)	r   getr   _load_regular_expressionsr   setr   r   r   )r#   r6   s     r&   initzSXNGPlugin.init   st     ||DGG$00;Ar//9BSU--o>G#%,,^<Er'   c                    t        j                  | j                  i       j                  |      }|sy t        |t              rt        |      }t        |t              r#|D ch c]  }t        j                  |       c}S t        |t              r7|j                         D ci c]  \  }}t        j                  |      | c}}S y c c}w c c}}w )N)r   r<   r   r2   strr	   listrecompiledictitems)r#   settings_keysetting_valuerps        r&   r=   z$SXNGPlugin._load_regular_expressions   s     TWWb155lC mS)(7MmT*+89aBJJqM99mT*3@3F3F3HI!QBJJqM1$II : Js   "C	$ C)r$   r   r   N)__name__
__module____qualname____doc__r   r!   boolr5   r?   rE   rC   PatternrA   r>   r=   __classcell__)r%   s   @r&   r   r   n   sl    ?	B
 9L V^ cg . $ bjj#o9NQT9TW[9[ r'   r   r*   zResult|LegacyResult
field_nameurl_srcr   c                    |st        j                  d|       yt        |      }t        D ]  }|j	                  |j
                        s y t        j                         D ][  \  }}|j	                  |j
                        s"|j                  |j                  ||j
                              }t        |      }|c S  y)zReturns bool ``True`` to use URL unchanged (``False`` to ignore URL).
    If URL should be modified, the returned string is the new URL to use.zmissing a URL in field %sT)urlF)r/   )r   debugr   r   r)   r/   r   rF   _replacesubr   )r*   rR   rS   url_src_parsedr4   replacementnew_urls          r&   r1   r1      s     		-z:'*N >>.//0 !( >>.//0$--W[[nNcNc5d-eG )GN	 r'   )*rN   typingtrC   urllib.parser   r   flask_babelr   searxr   searx.result_types._baser   r   searx.settings_loaderr	   searx.pluginsr
   r   _corer   TYPE_CHECKINGflasksearx.searchr   searx.extended_typesr   searx.result_typesr   r   r   rE   rP   rA   __annotations__r>   r   r   r   r   rO   r1    r'   r&   <module>rl      s   N`  	 -   = . , ??.1)'!#bjj#o	 #e Ec 5S C CL2  c VZ]`V` r'   