
    j                         d Z ddlZddlZddlmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZ  ej                  d	      Zd
ededefdZdedefdZdedefdZdeez  defdZdedz  dedz  fdZy)z
Utilities.    N)suppress)datetimetimezone)unescape)unquote   )DDGSExceptionz<.*?>
html_bytesqueryreturnc                     dD ]Y  \  }}}t        t              5  | j                  |      |z   }| j                  ||      }| || j                         cddd       c S  d|d}t	        |      # 1 sw Y   vxY w)zExtract vqd from html bytes.))s   vqd="      ")s   vqd=      &)s   vqd='r      'Nz_extract_vqd() query=z Could not extract vqd.)r   
ValueErrorindexdecoder	   )r
   r   c1c1_lenc2startendmsgs           5/usr/local/lib/python3.12/dist-packages/ddgs/utils.py_extract_vqdr      s     2FB
 j! 	2$$R(61E""2u-CeC(//1	2 	22 #E8#:
;C

	2 	2s   9A00A9	urlc                 @    | rt        |       j                  dd      S dS )z(Unquote URL and replace spaces with '+'. + )r   replace)r   s    r   _normalize_urlr$      s     -073<S)8b8    rawc                 `   | syt         j                  d|       }t        |      }t        j                  d|      }t        |      D ci c]*  }t        j                  |      d   dk(  st        |      d, }}|r|j                  |      }dj                  |j                               S c c}w )zNormalize text.

    Strip HTML tags, unescape HTML entities, normalize Unicode,
    remove "c" category characters, and collapse whitespace.
    r"   NFCr   CNr    )_REGEX_STRIP_TAGSsubr   unicodedata	normalizesetcategoryord	translatejoinsplit)r&   textch	c_to_nones       r   _normalize_textr7   $   s        S)D D>D   -D *-TY2k6J6J26Nq6QUX6XR$YIY~~i( 88DJJL!! Zs    B+)B+datec                     t        | t              r2t        j                  | t        j
                        j                         S | S )z8Normalize date from integer to ISO format if applicable.)
isinstanceintr   fromtimestampr   utc	isoformat)r8   s    r   _normalize_dater?   ?   s3    EOPTVYEZ8!!$5??Ad`ddr%   proxyc                     | dk(  rdS | S )z.Expand "tb" to a full proxy URL if applicable.tbzsocks5h://127.0.0.1:9150 )r@   s    r   _expand_proxy_tb_aliasrD   D   s    ).$%AEAr%   )__doc__rer,   
contextlibr   r   r   htmlr   urllib.parser   
exceptionsr	   compiler*   bytesstrr   r$   r7   r;   r?   rD   rC   r%   r   <module>rN      s     	   '    %BJJw' U 3 3  9 9 9
" " "6e#) e e
B#* Bt Br%   