
    Sj              	           d dl mZmZmZmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZ  ed      Zd	ed
ede	fdZdee   dee   deeee
f      fdZd
edefdZd
edeeef   fdZy)    )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                 @  K   t        |      }	 | j                  d|       d{   }|j                  dk7  rt        |d       |j	                         }|j                  d      st        |d       |j                  di       xs i }t               }d|v rt        |d   t              |_	        d|v rt        |d   t              |_        d	|v rt        |d	   t              |_        |S 7 # t        $ r)}t        |d
      rt        t!        |d
      d       |d}~ww xY ww)a
  
    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           R/root/.hermes/venv/lib/python3.12/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   DC) C'CC) &D'C) )	D2$DDDarrresult_typec           
      $   g }| D ]  }|rlt        |t              r\d|v sd|v sd|v sd|v sd|v sd|v sd|v sd|v r$|j                  t        d
i t	        |             Y|j                   |d
i |       q|j                   ||	              |S )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 2JtT*d"$T!4$ D(T!$xI*B4*HIJ{2T23NN;401!2" N    c                     | j                   r| j                   j                         st        d      | j                  4| j                  dk  rt        d      | j                  dkD  rt        d      | j                  4| j                  dk  rt        d      | j                  dkD  rt        d      | j
                  qh d	}| j
                  D ]^  }t        |t              r||vst        d
| d|       t        |d      s6|j                  |vsEt        d
|j                   d|        | j                  Kt        | j                  t              r&t        | j                  j                               dk(  rt        d      | j                  Kt        | j                  t              r&t        | j                  j                               dk(  rt        d      | j                  t        | j                         | S )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#Location must be a non-empty stringztbs must be a non-empty string)querystrip
ValueErrorlimittimeoutsourcesr<   strr)   rD   locationlentbsscrape_optionsr   )r   valid_sourcessources      r/   _validate_search_requestrR   S   s    == 3 3 5011}} ==A566==3677"??a788??V#IJJ"1oo 	jF&#&.$'<VHOTaSb%cdd(;;m3$'<V[[MYfXg%hii	j #'**C0C8H8H8N8N8P4QUV4VBCC{{'++s+s7;;3D3D3F/G1/L=>>) 6 67NrA   c                 (   t        |       }|j                  dd      }d|vr|j                  |j                  |d<   d|vr|j                  |j                  |d<   |j                  !|j                  |d<   |j                  dd       |j                  .t        |j                        }|r||d<   |j                  d	d       t        |d
d      x}?t        |      j                         r&t        |j                        j                         |d
<   |S )z
    Prepare a search request payload.

    Args:
        request: Search request

    Returns:
        Request payload dictionary
    T)exclude_noneby_aliasrH   NrI   ignoreInvalidURLsignore_invalid_urlsscrapeOptionsrO   integration)rR   
model_dumprH   rI   rW   poprO   r   r*   rK   rF   rY   )r   validated_requestr   scrape_datavs        r/   r"   r"      s    19''TD'IDd066B)//W!2!:!:!F+33Y,,8$5$I$I !&-''3,->-M-MN$/D!!4(&t<<IcRSflln!"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'   rR   rK   r"   r;   rA   r/   <module>re      s    8 8  7 8 7 OCL%%% %N$s) $q' d5HCU>V 61m 1 1f] tCH~ rA   