
    IZj"                     <   U d 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Z	 ddl	m
Z n# e$ r dZY nw xY wddlmZ ddlmZmZmZ d	d
lmZ  ej        e          ZddhZee         ed<    ej        ej        j                   ej                             ddg d          Z! ej"        e!          Z# edddd          Z$ ej%        dej&                  Z' ej%        dej&                  Z( edd           G d d                      Z)dee*z  ez  dz  de+fdZ,de*de+fdZ-de*de.e         fdZ/d e*ez  defd!Z0d"edefd#Z1d$ededz  fd%Z2d&ede+fd'Z3d(ed&edefd)Z4d*ededz  fd+Z5d*e*ez  ez  dedz  fd,Z6d-ed.e.e         defd/Z7d0edefd1Z8dS )2z7
Module bundling functions related to HTML processing.
    N)	dataclass)datetime)Any)detect)
from_bytes)HtmlElement
HTMLParser
fromstring   )MAX_FILE_SIZEutf-8utf_8UNICODE_ALIASES   )i  i  i  i  i  )totalconnectstatus_forcelist)retriesFT)collect_idsdefault_doctypeencoding
remove_pisz^< ?! ?DOCTYPE.+?/ ?>z(<html.*?)\s*/>)slotseqc                   F    e Zd ZU dZeed<   eed<   eed<   eed<   eed<   dS )	Extractorz0Defines a class to store all extraction options.	extensivemaxminoriginalformatN)__name__
__module____qualname____doc__bool__annotations__r   str     Z/Users/jameslopez/projects/MentorCore/.venv/lib/python3.11/site-packages/htmldate/utils.pyr   r   /   sC         66OOO	MMM	MMMNNNKKKKKr*   r   datareturnc                 8    |  pt          |           t          k    S )z6Check if the input object is suitable to be processed.)lenr   r,   s    r+   is_wrong_documentr1   :   s    80s4yy=00r*   c                 V    	 |                      d           n# t          $ r Y dS w xY wdS )zLSimple heuristic to determine if a bytestring uses standard unicode encodingzUTF-8FT)decodeUnicodeDecodeErrorr0   s    r+   isutf8r5   ?   sC    G   uu4s    
&&bytesobjectc                 V   t          |           rdgS g }t          >t          |           d         }|'|                    |                                           t	          | dd                   pt	          |           }|                    d |D                        d |D             S )z<Read all input or first chunk and return a list of encodingsr   Nr   i:  c                     g | ]	}|j         
S r)   )r   ).0rs     r+   
<listcomp>z#detect_encoding.<locals>.<listcomp>X   s    :::1AJ:::r*   c                 $    g | ]}|t           v|S r)   )r   )r9   gs     r+   r;   z#detect_encoding.<locals>.<listcomp>[   s"    ;;;!!?":":A":":":r*   )r5   cchardet_detectappendlowerr   extend)r6   guessescchardet_guessdetection_resultss       r+   detect_encodingrE   H   s     k yG"(55jA%NN>//11222 #;vv#677R:k;R;RNN::(9:::;;; <;w;;;;r*   filecontentc                    t          | t                    r| S d}t          |           D ]K}	 |                     |          } n2# t          t
          f$ r t                              d|           Y Hw xY w|pt          | dd          S )znGuess bytestring encoding and try to decode to Unicode string.
    Resort to destructive conversion otherwise.Nzwrong encoding detected: %sr   replace)r   errors)
isinstancer(   rE   r3   LookupErrorr4   LOGGERwarning)rF   htmltextguessed_encodings      r+   decode_filerP   ^   s     +s## H+K88  	"))*:;;H E /0 	L 	L 	LNN8:JKKKKK	L
 Ks;KKKKs   A,A21A2responsec                 Z    t          | d          r| j        n| }|rt          |          ndS )zRead the data from a response object exposing the body via ``.data``
    (e.g. urllib3 or a compatible response) or from a bytestring, then guess
    its encoding and decode it to return a unicode string.r,    )hasattrr,   rP   )rQ   r,   s     r+   decode_responserU   q   s6     $Hf55C8==8D $,;t",r*   urlc                    	 t                               d| d          }|j        dk    r"t                              d|j        |            nwt          |j                  rt                              d|            nGt          |j                  S # t          $ r&}t                              d| |           Y d}~nd}~ww xY wdS )	a,  Fetches page using urllib3 and decodes the response.

    Args:
        url: URL of the page to fetch.

    Returns:
        HTML code as string, or Urllib3 response object (headers + body), or empty string in case
        the result is invalid, or None if there was a problem with the network.

    GET   )timeout   z!not a 200 response: %s for URL %szincorrect input data for URL %szdownload error: %s %sN)		HTTP_POOLrequeststatusrL   errorr1   r,   rU   	Exception)rV   rQ   errs      r+   	fetch_urlrb   {   s    2 $$UC$<<
 ?c!!LL<hosSSSSx}-- 	2LL:C@@@@"8=111  8 8 8,c3777777778 4s   B 
C B;;C 	beginningc                 
    d| vS )zOAssess if the object is proper HTML (awith a corresponding tag or declaration).htmlr)   )rc   s    r+   is_dubious_htmlrf      s    ""r*   
htmlstringc                 T   d|v r<|                      d          \  }}}t                              d|d          dz   |z   } t          |                                           D ]E\  }}d|v r4|                    d          rt                              d| d          }  n	|d	k    r nF| S )
z>Repair faulty HTML strings to make then palatable for libxml2.doctype
rS   r   )countz<htmlz/>z\1>   )	partitionDOCTYPE_TAGsub	enumerate
splitlinesendswithFAULTY_HTML)rg   rc   	firstline_restilines          r+   repair_faulty_htmlry      s     I'11$77	1d __R!_<<tCdJ
Z224455  4d??t}}T22?$1EEJEq55E r*   
htmlobjectc                     	 t          |                     d          t                    S # t          $ r%}t                              d|           Y d}~nd}~ww xY wdS )z!Try to pass bytes to LXML parser.utf8parserzlxml parser bytestring %sN)r
   encodeHTML_PARSERr`   rL   r_   )rz   ra   s     r+   fromstring_bytesr      sp    7*++F33KHHHH 7 7 70#6666666674s   (+ 
AAAc                    t          | t                    r| S t          | t          t          f          st	          dt          |                      t          | t                    rY|                     d          rDd| vr@t                              d|            t          |           }|t          d|            |} d}t          |           } | dd                                         }t          | |          } d}	 t          | t          	          }nN# t          $ r d
}t!          |           }Y n1t"          $ r%}t                              d|           Y d}~nd}~ww xY w|t'          |          dk     r|st!          |           }|Lt)          |          r=t'          |          dk     r*t                              dt'          |                     d}|S )zkLoad object given as input and validate its type
    (accepted: lxml.html tree, bytestring and string)
    zincompatible input type: http zURL detected, downloading: %sNzURL couldn't be processed: 2   Fr}   Tzlxml parsing failed: %sr   rl   z9parsed tree length: %s, wrong data type or not valid HTML)rJ   r   bytesr(   	TypeErrortype
startswithrL   debugrb   
ValueErrorrP   r@   ry   r
   r   r   r`   r_   r/   rf   )rz   
downloadedtreerc   fallback_parsera   s         r+   	load_htmlr      s   
 *k** j5#,// HFD4D4DFFGGG 	:s##
 !!&))
  z!!4jAAAz**
G:GGHHH
DZ((J3B3%%''I#J	::JN5*[999 , , ,
++ 5 5 5.444444445 	D		A~
++ OI663t99q==GT	
 	
 	
 Ks   D E#5	E#>EE#r   elemlistc                     |                      |          D ]R}	 |                                 # t          $ r. |                                }||                    |           Y Ow xY w| S )zDelete selected elements.)iter	drop_treeAttributeError	getparentremove)r   r   elementparents       r+   
clean_htmlr      s    99X&& ' '	' 	' 	' 	'&&((F!g&&&	' Ks   .5A&%A&stringc                 P    d                     |                                           S )z7Remove superfluous space and normalize remaining space.r   )joinsplit)r   s    r+   	trim_textr      s    88FLLNN###r*   )9r%   loggingredataclassesr   r   typingr   urllib3cchardetr   r>   ImportErrorcharset_normalizerr   	lxml.htmlr   r	   r
   settingsr   	getLoggerr"   rL   r   setr(   r'   disable_warnings
exceptionsInsecureRequestWarningutilRetryRETRY_STRATEGYPoolManagerr\   r   compileIrn   rs   r   r   r&   r1   r5   listrE   rP   rU   rb   rf   ry   r   r   r   r   r)   r*   r+   <module>r      s      				 ! ! ! ! ! !             2222222   OOO ) ) ) ) ) ) 9 9 9 9 9 9 9 9 9 9 # # # # # #		8	$	$$g.S . . .  +B C C C##
... $  
  G777	juw4   bj0"$77bj+RT22 %          ! 1C%K+5< 1 1 1 1 1
 4    < <49 < < < <,LUS[ LS L L L L&-c -c - - - -3 3:    :#s #t # # # #
3 3 3      t);    2%#+3 2d8J 2 2 2 2j[ DI +    $c $c $ $ $ $ $ $s   * 44