
    i              	          d Z 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mZ ddlmZmZ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df                  fdZd	e	d
e	fdZd	e	d
eeef         fdZdS )z,
Search functionality for Firecrawl v2 API.
    )DictAnyUnionListTypeVarType   )SearchRequest
SearchDataDocumentSearchResultWebSearchResultNewsSearchResultImages)normalize_document_input_map_search_result_keys)
HttpClienthandle_response_errorvalidate_scrape_optionsprepare_scrape_optionsTclientrequestreturnc                    t          |          }	 |                     d|          }|j        dk    rt          |d           |                                }|                    d          st          |d           |                    di           pi }t                      }d|v r t          |d         t                    |_	        d|v r t          |d         t                    |_        d|v r t          |d         t                    |_        |S # t          $ r5}t          |d	          rt          t!          |d	          d           |d
}~ww xY w)a  
    Search for documents.
    
    Args:
        client: HTTP client instance
        request: Search request
        
    Returns:
        SearchData with search results grouped by source type
        
    Raises:
        FirecrawlError: If the search operation fails
    z
/v2/search   searchsuccessdatawebnewsimagesresponseN)_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           e/home/agentuser/.hermes/hermes-agent/venv/lib/python3.11/site-packages/firecrawl/v2/methods/search.pyr   r      sS   " +733L;;|\::3&&!(H555   ++ 	6!(H555  ,,2llD==&tE{ODDCGT>>'V6FGGCHt)$x.:LMMCJ
    3
## 	F!'#z":":HEEE	s   C=D 
E0E		Earrresult_typer   c           
         g }| D ]}|rt          |t                    rd|v sd|v sd|v sd|v sd|v sd|v sd|v sd|v r-|                    t          di t	          |                     fd	}|t
          k    rd
}n|t          k    rd}n|t          k    rd}|r,t          ||          }|                     |di |           |                     |di |           |                     ||                     |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&   Nr!   r    r   )url )	
isinstancedictappendr   r   r   r   r   r   )r1   r2   resultsitemresult_type_namenormalized_items         r0   r(   r(   5   sy    +-G 2 2 	2JtT** 	2d""$T!!4$$ D((T!!$xII*B4*H*HIIJJJJ#' "444'/$$ $444'-$$ O33',$# 8&=dDT&U&UONN;;#A#A#A#ABBBBNN;;#6#6#6#67777NN;;40001111N    c                    | j         r| j                                         st          d          | j        4| j        dk    rt          d          | j        dk    rt          d          | j        4| j        dk    rt          d          | j        dk    rt          d	          | j        ph d
}| j        D ]d}t          |t                    r||vrt          d| d|           1t          |d          r#|j	        |vrt          d|j	         d|           e| j
        ph d}| j
        D ]d}t          |t                    r||vrt          d| d|           1t          |d          r#|j	        |vrt          d|j	         d|           e| j        St          | j        t                    r*t          | j                                                  dk    rt          d          | j        St          | j        t                    r*t          | 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 emptyNr   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: type>   pdfgithubresearchzInvalid category type: z#Location must be a non-empty stringztbs must be a non-empty string)querystrip
ValueErrorlimittimeoutsourcesr=   strr*   rG   
categorieslocationlentbsscrape_optionsr   )r   valid_sourcessourcevalid_categoriescategorys        r0   _validate_search_requestr[   \   s    = 2 3 3 5 5 20111 } =A5666=36777 "?a7888?V##IJJJ "111o 	j 	jF&#&& j..$%cV%c%cTa%c%cddd /(( j;m33$%hV[%h%hYf%h%hiii %888* 	q 	qH(C(( q#333$%jx%j%jXh%j%jkkk 46** q=(888$%ox}%o%o]m%o%oppp #'*C00 	DC8H8N8N8P8P4Q4QUV4V4VBCCC {'+s++ 	?s7;3D3D3F3F/G/G1/L/L=>>> ) 6777NrD   c                    t          |           }|                    dd          }d|vr|j        
|j        |d<   d|vr|j        
|j        |d<   |j         |j        |d<   |                    dd           |j        1t          |j                  }|r||d<   |                    d	d           t          |d
d          }|(t          |          
                                }|r||d
<   |S )z
    Prepare a search request payload.
    
    Args:
        request: Search request
        
    Returns:
        Request payload dictionary
    T)exclude_noneby_aliasrN   NrO   ignoreInvalidURLsignore_invalid_urlsscrapeOptionsrV   integration)r[   
model_dumprN   rO   r`   poprV   r   r+   rQ   rL   )r   validated_requestr   scrape_dataintegration_valueintegration_strs         r0   r#   r#      s)    199''TD'IID d06B)/W!2!:!F+3Y ,8$5$I !&--- '3,->-MNN 	0$/D!!4(((   1=$GG$/006688 	2"1DKrD   N)__doc__typingr   r   r   r   r   r   typesr
   r   r   r   r   r   utils.normalizer   r   utilsr   r   r   r   r   r   r(   r[   rQ   r#   r<   rD   r0   <module>rn      s    9 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 n n n n n n n n n n n n n n n n O O O O O O O O f f f f f f f f f f f fGCLL''' ' ' ' 'R%$s) %$q' %d5JCW>X % % % %NCm C C C C CL)] )tCH~ ) ) ) ) ) )rD   