
    _j              	           S SK JrJrJrJrJrJr  SSKJrJ	r	J
r
JrJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr  \" S5      rS	\S
\S\	4S jrS\\   S\\   S\\\\
4      4S jrS
\S\4S jrS
\S\\\4   4S jrg)    )DictAnyUnionListTypeVarType   )SearchRequest
SearchDataDocumentSearchResultWebSearchResultNewsSearchResultImages)AsyncHttpClient)handle_response_error)normalize_document_input)validate_scrape_optionsprepare_scrape_optionsTclientrequestreturnc                 n  #    [        U5      n U R                  SU5      I Sh  vN nUR                  S:w  a  [        US5        UR	                  5       nUR                  S5      (       d  [        US5        UR                  S0 5      =(       d    0 n[        5       nSU;   a  [        US   [        5      Ul	        SU;   a  [        US   [        5      Ul        S	U;   a  [        US	   [        5      Ul        U$  N! [         a.  n[        US
5      (       a  [        [!        US
5      S5        UeSnAff = f7f)z
Async search for documents.

Args:
    client: Async HTTP client instance
    request: Search request

Returns:
    SearchData with search results grouped by source type

Raises:
    FirecrawlError: If the search operation fails
z
/v2/searchN   searchsuccessdatawebnewsimagesresponse)_prepare_search_requestpoststatus_coder   jsongetr   _transform_arrayr   r   r   r   r   r    	Exceptionhasattrgetattr)r   r   request_datar!   response_datar   outerrs           T/home/rurouni/.local/lib/python3.13/site-packages/firecrawl/v2/methods/aio/search.pyr   r      s    " +73L\<@@3&!(H5   ++!(H5  ,2lD=&tE{ODCGT>'V6FGCHt)$x.:LMCJ
 A  3
##!'#z":HE	s9   D5C: C8CC: 7D58C: :
D2)D--D22D5arrresult_typec           
      Z   / nU  H  nU(       a  [        U[        5      (       an  SU;   d*  SU;   d$  SU;   d  SU;   d  SU;   d  SU;   d  SU;   d  SU;   a%  UR                  [        S
0 [	        U5      D65        Mt  UR                  U" S
0 UD65        M  UR                  U" US	95        M     U$ )a  
Transforms an array of items into a list of result_type or Document.
If the item dict contains any of the special keys, it is treated as a Document.
Otherwise, it is treated as result_type.
If the item is not a dict, it is wrapped as result_type with url=item.
markdownhtmlrawHtmllinks
screenshotchangeTrackingsummaryr%   )url )
isinstancedictappendr   r   )r0   r1   resultsitems       r/   r'   r'   8   s     )+GJtT**d"$T!4$ D(T!$xI*B4*HIJ{2T23NN;401! " N    c                    U R                   (       a  U R                   R                  5       (       d  [        S5      eU R                  b6  U R                  S::  a  [        S5      eU R                  S:  a  [        S5      eU R                  b6  U R                  S::  a  [        S5      eU R                  S:  a  [        S5      eU R
                  b  1 S	knU R
                   Hn  n[        U[        5      (       a  X!;  a  [        S
U SU 35      eM0  [        US5      (       d  MC  UR                  U;  d  MU  [        S
UR                   SU 35      e   U R                  (       a  U R                  (       a  [        S5      eU R                  bQ  [        U R                  [        5      (       a'  [        U R                  R                  5       5      S:X  a  [        S5      eU R                  bQ  [        U R                  [        5      (       a'  [        U R                  R                  5       5      S:X  a  [        S5      eU R                  b  [!        U R                  5        U $ )z
Validate and normalize search request.

Args:
    request: Search request to validate

Returns:
    Validated request

Raises:
    ValueError: If request is invalid
zQuery cannot be emptyr   zLimit must be positived   zLimit cannot exceed 100zTimeout must be positivei z*Timeout cannot exceed 300000ms (5 minutes)>   r   r   r    zInvalid source type: z. Valid types: typez<include_domains and exclude_domains cannot both be specifiedz#Location must be a non-empty stringztbs must be a non-empty string)querystrip
ValueErrorlimittimeoutsourcesr<   strr)   rD   include_domainsexclude_domainslocationlentbsscrape_optionsr   )r   valid_sourcessources      r/   _validate_search_requestrT   S   s    == 3 3 5 5011}} ==A566==3677"??a788??V#IJJ"1ooF&#&&.$'<VHOTaSb%cdd /((;;m3$'<V[[MYfXg%hii & 7#:#:J
 	
 #'**C00C8H8H8N8N8P4QUV4VBCC{{'++s++s7;;3D3D3F/G1/L=>>) 6 67NrA   c                    [        U 5      nUR                  SSS9nSU;  a  UR                  b  UR                  US'   SU;  a  UR                  b  UR                  US'   UR                  b!  UR                  US'   UR                  SS5        UR                  b!  UR                  US'   UR                  S	S5        UR                  b!  UR                  US
'   UR                  SS5        UR                  b2  [        UR                  5      nU(       a  X2S'   UR                  SS5        [        USS5      =nbD  [        U5      R                  5       (       a&  [        UR                  5      R                  5       US'   U$ )zo
Prepare a search request payload.

Args:
    request: Search request

Returns:
    Request payload dictionary
T)exclude_noneby_aliasrH   NrI   ignoreInvalidURLsignore_invalid_urlsincludeDomainsrL   excludeDomainsrM   scrapeOptionsrQ   integration)rT   
model_dumprH   rI   rY   poprL   rM   rQ   r   r*   rK   rF   r]   )r   validated_requestr   scrape_datavs        r/   r"   r"      sm    19''TD'IDd066B)//W!2!:!:!F+33Y,,8$5$I$I !&-((4!2!B!B"D)((4!2!B!B"D)''3,->-M-MN$/!!4(&t<<IcRSfllnn!"3"?"?@FFH]KrA   N)typingr   r   r   r   r   r   typesr
   r   r   r   r   r   utils.http_client_asyncr   utils.error_handlerr   utils.normalizer   utils.validationr   r   r   r   r'   rT   rK   r"   r;   rA   r/   <module>ri      s    8 8  7 8 7 OCL%%% %N$s) $q' d5HCU>V 66m 6 6p'] 'tCH~ 'rA   