
    $$}gtJ                     *   U d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZmZmZmZmZ d dlmZmZ dd	lmZmZ dd
lmZmZmZmZ ddlmZ ddlm Z  ddl!m"Z" dZ#dZ$dZ%dZ&i a'ee(e"f         e)d<   dee(e"f         fdZ*de(de"ddfdZ+de(ddfdZ, edd          	 d7de-de(de-fd            Z.de/fd Z0	 	 	 d8de-d!e/d"ee         dee(e"f         fd#Z1 G d$ d%e          Z2 G d& d'e2          Z3 G d( d)e2          Z4dddde3fd*ee(ef         de-d+e-d,e-d-e-d"ee         deeee(         e/ef         eddef         f         fd.Z5	 	 	 	 	 d9d*ee(ef         de-d+e-d,e-d-e-d"ee         dee(         fd/Z6	 	 	 	 	 	 d:d*ee(ef         de-d0e-d+e-d,e-d-e-d"ee         deee(e f                  fd1Z7	 	 	 	 	 d9d*ee(ef         de-d+e-d,e-d-e-d"ee         deed2         ee(e(e(f         f         fd3Z8	 	 	 d;d4ee(ef         d,e-d-e-d"ee         de-f
d5Z9d<de(ddfd6Z:dS )=    )unicode_literalsN)open)	lru_cache)isabs)DictListOptionalTupleTypeUnion)SplitResulturlsplit   )BaseTLDSourceParserRegistry)	TldBadUrlTldDomainNotFoundTldImproperlyConfigured
TldIOError)project_dir)Result)TriezArtur Barseghyanz2013-2023 Artur Barseghyanz,MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later)BaseMozillaTLDSourceParserget_fldget_tldget_tld_namesget_tld_names_containeris_tldMozillaTLDSourceParser MozillaPublicOnlyTLDSourceParser	parse_tldpop_tld_names_containerprocess_urlreset_tld_namesr   	tld_namesupdate_tld_namesupdate_tld_names_cliupdate_tld_names_containerr%   returnc                      t           S )zCGet container of all tld names.

    :return:
    :rtype dict:
    r%        I/var/www/py-google-trends/myenv/lib/python3.11/site-packages/tld/utils.pyr   r   3   s
     r-   tld_names_local_pathtrie_objc                     |t           | <   dS )ziUpdate TLD Names container item.

    :param tld_names_local_path:
    :param trie_obj:
    :return:
    Nr+   )r/   r0   s     r.   r(   r(   =   s     '/I"###r-   c                 <    t                               | d           dS )zTRemove TLD names container item.

    :param tld_names_local_path:
    :return:
    N)r%   popr/   s    r.   r"   r"   K   s     MM&-----r-      T)maxsizetypedFfail_silently
parser_uidc                 4   g }|j         }|r>t          j        |d          }|r&|j        r ||                    |                      nAt          j                    D ]-\  }}|r&|j        r ||                    |                      .t          |          S )zUUpdate TLD names.

    :param fail_silently:
    :param parser_uid:
    :return:
    Nr8   )appendr   get
source_urlr&   itemsall)r8   r9   resultsresults_append
parser_clss        r.   r&   r&   U   s     G^N \*d33
 	*/ 	N++-+HH   '/n&6&6 	 	"J
 j3 //m/LL   w<<r-   c                  :   t          j        d          } |                     dddd           |                     dd	d
dd           |                     t          j        dd                   }|j        }|j        }t          t          ||                     S )zCLI wrapper for update_tld_names.

    Since update_tld_names returns True on success, we need to negate the
    result to match CLI semantics.
    zUpdate TLD names)descriptionr9   ?Nz*UID of the parser to update TLD names for.)nargsdefaulthelpz--fail-silentlyr8   F
store_truezFail silently)destrH   actionrI   r   )r9   r8   )
argparseArgumentParseradd_argument
parse_argssysargvr9   r8   intr&   )parserargsr9   r8   s       r.   r'   r'   q   s     $1CDDDF
9	          SXabb\**DJ&M
-PPPP  r-   retry_countparser_classc                 B    |st           }|                    | |          S )a  Build the ``tlds`` list if empty. Recursive.

    :param fail_silently: If set to True, no exceptions are raised and None
        is returned on failure.
    :param retry_count: If greater than 1, we raise an exception in order
        to avoid infinite loops.
    :param parser_class:
    :type fail_silently: bool
    :type retry_count: int
    :type parser_class: BaseTLDSourceParser
    :return: List of TLD names
    :rtype: obj:`tld.utils.Trie`
    r8   rV   )r   r   )r8   rV   rW   s      r.   r   r      s3    $  .-%%# &   r-   c                   R    e Zd Ze	 ddededeeee	f                  fd            Z
dS )	r   Fr   r8   rV   r)   c                    |dk    r|rdS t           t          }| j        |v r|| j                 |S 	 t          | j                  r| j        }nt	          | j                  }t          |dd          }t                      }|j        }d}| j        }	|D ]W}
d|
v rd}|r|	s nJd	|
v r|
	                                d         }
|
d
         dv r7 ||

                                 |           Xt          | j        |           |                                 n# t          $ rZ |                     |           |dz  }|                     ||          cY 	 |                                 S # t           $ r Y S w xY wt           $ r5}|r,Y d}~	 |                                 dS # t           $ r Y dS w xY w|d}~ww xY w	 	 |                                 n:# t           $ r Y n.w xY w# 	 |                                 w # t           $ r Y w w xY wxY w|S )z[Parse.

        :param fail_silently:
        :param retry_count:
        :return:
        r   Nrutf8)encodingFz===BEGIN PRIVATE DOMAINS===Tz// xn--r   )/
)privater;   rY   )r   r%   
local_pathr   r   codecs_openr   addinclude_privatesplitstripr(   closeIOErrorr&   r   	Exception)clsr8   rV   
_tld_namesrb   
local_filetrietrie_addprivate_sectionre   lineerrs               r.   r   z(BaseMozillaTLDSourceParser.get_tld_names   s    ?? !t   

 Nj((3>*62	S^$$ 9 ^

(88
$ZvFFFJ66DxH#O!1O" E E0D88&*O" ? E $$::<<?D7k))DJJLL*ODDDDD&s~t<<< 	 	 	  } ===1K$$+ %      """"     	 	 	 ttt  """""   	 			    """"     """"    s   C#D G! <F7G! E))
E65E69	F7F2G! 	F 
F-,F-0F22F77G! <G 
GG!H#G87H8
HHHHN)Fr   )__name__
__module____qualname__classmethodboolrS   r	   r   strr   r   r,   r-   r.   r   r      si        =>N N N7:N	$sDy/	"N N N [N N Nr-   r   c                   >    e Zd ZU dZdZeed<   dZeed<   dZeed<   dS )	r   Mozilla TLD source.mozillauidz4https://publicsuffix.org/list/public_suffix_list.datr>   zres/effective_tld_names.dat.txtrb   N)	rs   rt   ru   __doc__r|   rx   __annotations__r>   rb   r,   r-   r.   r   r      sH         CLJLLL7J77777r-   r   c                   L    e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed	<   d
S )r    rz   mozilla_public_onlyr|   z?https://publicsuffix.org/list/public_suffix_list.dat?publiconlyr>   z+res/effective_tld_names_public_only.dat.txtrb   Fre   N)rs   rt   ru   r}   r|   rx   r~   r>   rb   re   rw   r,   r-   r.   r    r      sd         $C$$$I     DJCCC!OT!!!!!r-   r    urlfix_protocolsearch_publicsearch_privatec                    |s|st          d          t          ||          }t          | t                    sM|r|                     d          sd|  } 	 t          |           }n"# t          $ r}|r
dd| fcY d}~S |d}~ww xY w| }|j        }	|	s|rdd|fS t          |           |		                                }	|	
                    d          r|	                    d          }	|	                    d          }
|j        }||         j        }d}d}d}t          |
          }t!          |d	z
  d
d
          D ]l}|
|         }|j         nZ||j        k    r nM|j                            |          }||j                            d          }| n|d	z  }|}|j        r|}|}m||j        r|s|j        r	|s|j        r|rdd|fS t-          |	          ||k    rd
}nt/          d	||z
            }|
||fS )zProcess URL.

    :param parser_class:
    :param url:
    :param fail_silently:
    :param fix_protocol:
    :param search_public:
    :param search_private:
    :return:
    zJEither `search_public` or `search_private` (or both) shall be set to True.)r8   rW   )z//zhttp://https://r   N)r   .r   r   *)domain_name)r   r   
isinstancer   
startswithr   
ValueErrorhostnamer   lowerendswithrstriprf   rb   rootlenrangechildren	exceptionr=   leafra   r   max)r   r8   r   r   r   rW   rl   
parsed_urler   domain_partsr/   nodecurrent_length
tld_lengthmatchlen_domain_partsipartchild
non_zero_is                        r.   r#   r#     s   $  
^ 
%
 
 	
 #,  J c;''  	#/L M M 	#"S""C	!#JJ 	 	 	 T3&&&&&&		 
 %K % 	%z))$$$$##%%K C   .!((--$$S))L'2 *+0DNJE<((#a'R00  A = E 4>!!E!!$'' =M%%c**E =E 	!9 	'JE 

 
 
&+m 
  
 $)= 

  	=z))#<<<<:%%

,z9::
Z//s$   A' '
B1A?7B=A??Bc                     d|v rt          d          |s|rt          nt          }t          | |||||          \  }}}	|dS |J |dk     r|	j        S d                    ||dz
  d                   S )a?  Extract the first level domain.

    Extract the top level domain based on the mozilla's effective TLD names
    dat file. Returns a string. May throw ``TldBadUrl`` or
    ``TldDomainNotFound`` exceptions if there's bad URL provided or no TLD
    match found respectively.

    :param url: URL to get top level domain from.
    :param fail_silently: If set to True, no exceptions are raised and None
        is returned on failure.
    :param fix_protocol: If set to True, missing or wrong protocol is
        ignored (https is appended instead).
    :param search_public: If set to True, search in public domains.
    :param search_private: If set to True, search in private domains.
    :param parser_class:
    :type url: str | SplitResult
    :type fail_silently: bool
    :type fix_protocol: bool
    :type search_public: bool
    :type search_private: bool
    :return: String with top level domain (if ``as_object`` argument
        is set to False) or a ``tld.utils.Result`` object (if ``as_object``
        argument is set to True); returns None on failure.
    :rtype: str
    	as_objectzH`as_object` argument is deprecated for `get_fld`. Use `get_tld` instead.r   r8   r   r   r   rW   Nr   r   r   )r   r   r    r#   r   join)
r   r8   r   r   r   rW   kwargsr   r   r   s
             r.   r   r     s    D f%
 
 	

  
 2""1 	 ,7#!#%!, , ,(L*j t !!!A~~""88La!1!12333r-   r   c                    |s|rt           nt          }t          | |||||          \  }}}	|dS |J |s*|dk     r|	j        S d                    ||d                   S |dk     rd}
d}|	j        
J d            |	j        }n]d                    |d|dz
                     }
d                    ||dz
  |                   }d                    ||d                   }t          |
|||	          S )	a  Extract the top level domain.

    Extract the top level domain based on the mozilla's effective TLD names
    dat file. Returns a string. May throw ``TldBadUrl`` or
    ``TldDomainNotFound`` exceptions if there's bad URL provided or no TLD
    match found respectively.

    :param url: URL to get top level domain from.
    :param fail_silently: If set to True, no exceptions are raised and None
        is returned on failure.
    :param as_object: If set to True, ``tld.utils.Result`` object is returned,
        ``domain``, ``suffix`` and ``tld`` properties.
    :param fix_protocol: If set to True, missing or wrong protocol is
        ignored (https is appended instead).
    :param search_public: If set to True, search in public domains.
    :param search_private: If set to True, search in private domains.
    :param parser_class:
    :type url: str | SplitResult
    :type fail_silently: bool
    :type as_object: bool
    :type fix_protocol: bool
    :type search_public: bool
    :type search_private: bool
    :return: String with top level domain (if ``as_object`` argument
        is set to False) or a ``tld.utils.Result`` object (if ``as_object``
        argument is set to True); returns None on failure.
    :rtype: str
    r   Nr   r    zNo hostname in URLr   )	subdomaindomaintldr   )r   r    r#   r   r   r   )r   r8   r   r   r   r   rW   r   r   r   r   r   _tlds                r.   r   r     sT   J  
 2""1 	 ,7#!#%!, , ,(L*j t !!! 3>>&&xxZ[[1222A~~	 "..0D..."HH\*:JN*:;<<	,zA~
'BCDDxxZ[[122F   r-   NNNc           	          |s|rt           nt          }	 t          | |d||||          }|dS |j        |j        |j        fS # t          t          t          t          f$ r Y nw xY wdS )zParse TLD into parts.

    :param url:
    :param fail_silently:
    :param fix_protocol:
    :param search_public:
    :param search_private:
    :param parser_class:
    :return: Tuple (tld, domain, subdomain)
    :rtype: tuple
    T)r8   r   r   r   r   rW   Nr   )
r   r    r   r   r   r   r   r   r   r   )r   r8   r   r   r   rW   objs          r.   r!   r!   &  s    &  
 2""1 	'%')%
 
 
 ;##w
CM11(*A:N    s   A A A"!A"valuec                 \    |s|rt           nt          }t          | dd|||          }| |k    S )ag  Check if given URL is tld.

    :param value: URL to get top level domain from.
    :param search_public: If set to True, search in public domains.
    :param search_private: If set to True, search in private domains.
    :param parser_class:
    :type value: str
    :type search_public: bool
    :type search_private: bool
    :return:
    :rtype: bool
    Tr   )r   r    r   )r   r   r   rW   r   s        r.   r   r   U  sX    $  
 2""1 	 #%!  D D=r-   c                 0    | rt          |            dS i adS )zReset the ``tld_names`` to empty value.

    If ``tld_names_local_path`` is given, removes specified
    entry from ``tld_names`` instead.

    :param tld_names_local_path:
    :type tld_names_local_path: str
    :return:
    N)r"   r%   r4   s    r.   r$   r$   y  s,       455555 			r-   )FN)Fr   N)FFTTN)FFFTTN)TTN)N);
__future__r   rM   rQ   codecsr   rc   	functoolsr   os.pathr   typingr   r   r	   r
   r   r   urllib.parser   r   baser   r   
exceptionsr   r   r   r   helpersr   resultr   rn   r   
__author____copyright____license____all__r%   rx   r~   r   r(   r"   rw   r&   rS   r'   r   r   r   r    r#   r   r   r!   r   r$   r,   r-   r.   <module>r      s1   ' ' ' ' ' ' '  



 & & & & & &             ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; . . . . . . . . / / / / / / / /            !                      
  
,<(  	4T	?   c4i    //)-/	/ / / /.# .$ . . . . 3d###37 -0	   $#6c    :  .2  *+ 
#t)_	   >P P P P P!4 P P Pf8 8 8 8 87 8 8 8" " " " "'A " " "$  .Ds0 s0	sK	 s0s0 s0 	s0
 s0 *+s0 5cC,-uT45L/MMNs0 s0 s0 s0p  .2B4 B4	sK	 B4B4 B4 	B4
 B4 *+B4 c]B4 B4 B4 B4N  .2Q Q	sK	 QQ Q 	Q
 Q Q *+Q eCK !Q Q Q Ql  .2, ,	sK	 ,, , 	,
 , *+, 5!"E#sC-$889, , , ,b .2	! !k!"!! ! *+	!
 
! ! ! !H #       r-   