
    SjF6                        d dl mZmZmZmZ ddlmZmZmZm	Z	m
Z
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 d dlZd	ed
efdZdeee      d
ee	   fdZdeeef   d
eeef   fdZded	ed
efdZ	 d dddededee   dee    d
ef
dZ!dddededee    d
efdZ"	 d dddededee	   dee   dee    d
ee	   fdZ#deded
e$fdZ%ded	e
d
efdZ&deded
efdZ'ded
efdZ(y)!    )OptionalDictAnyList   )CrawlRequestCrawlJobCrawlResponseDocumentCrawlParamsRequestCrawlParamsDataWebhookConfigCrawlErrorsResponseActiveCrawlsResponseActiveCrawlPaginationConfig)handle_response_error)prepare_scrape_options)AsyncHttpClient)normalize_document_inputNrequestreturnc                 P   | j                   r| j                   j                         st        d      d| j                   i}| j                  r| j                  |d<   | j                  t        | j                        }|r||d<   | j                  It        | j                  t              r| j                  |d<   n| j                  j                  d      |d<   | j                  dd      }|j                  dd        |j                  dd        |j                  d	d        d
ddddddddddddd}|j                         D ]  \  }}||v s|j                  |      ||<     |j                  |       t        | dd       &t        t        | d            j                         |d<   |S )NURL cannot be emptyurlpromptscrapeOptionswebhookT)exclude_none)r   exclude_unsetscrape_optionsincludePathsexcludePathsmaxDiscoveryDepthsitemapignoreQueryParametersdeduplicateSimilarURLscrawlEntireDomainallowExternalLinksallowSubdomainsdelaymaxConcurrencyregexOnFullURLzeroDataRetention)include_pathsexclude_pathsmax_discovery_depthr%   ignore_query_parametersdeduplicate_similar_urlscrawl_entire_domainallow_external_linksallow_subdomainsr+   max_concurrencyregex_on_full_urlzero_data_retentionintegration)r   strip
ValueErrorr   r!   r   r   
isinstancestr
model_dumppopitemsupdategetattr)r   dataoptsrequest_datafield_mappingssnakecamels          Q/root/.hermes/venv/lib/python3.12/site-packages/firecrawl/v2/methods/aio/crawl.py_prepare_crawl_requestrK      s   ;;gkk//1.//7;;D~~ X)%g&<&<=$(D!"goos+%ooDO%oo88d8KDO%%4t%LLUD!Xt$%t,''2#:$<2 4-+-2N ',,. 2uL &**51DK2 	KKwt,8!''="ABHHJ]K    	data_listc                     g }| xs g D ]8  }t        |t              st        |      }|j                  t	        di |       : |S )N )r=   dictr   appendr   )rM   	documentsdoc_data
normalizeds       rJ   _parse_crawl_documentsrU   B   sL     "IO 5h%1(;JX3
345 rL   bodyc                 Z   | j                  d      st        | j                  dd            | j                  d      | j                  dd      | j                  dd      | j                  dd      | j                  d	      | j                  d
      t        | j                  dg             dS )NsuccesserrorUnknown error occurredstatus	completedr   totalcreditsUsed	expiresAtnextrD   r[   r\   r]   credits_used
expires_atr`   rD   )get	ExceptionrU   )rV   s    rJ   _parse_crawl_status_responserf   K   s    88I*BCDD ((8$XXk1-'1%2hh{+ &txx';< rL   clientc                 \  K   t        |      }| j                  d|       d{   }|j                  dk\  rt        |d       |j	                         }|j                  d      r+t        |j                  d      |j                  d            S t        |j                  d	d
            7 w)aV  
    Start a crawl job for a website.
    
    Args:
        client: Async HTTP client instance
        request: CrawlRequest containing URL and options
        
    Returns:
        CrawlResponse with job information
        
    Raises:
        ValueError: If request is invalid
        Exception: If the crawl operation fails to start
    z	/v2/crawlN  zstart crawlrX   idr   )rj   r   rY   rZ   )rK   poststatus_coder   jsonrd   r
   re   )rg   r   payloadresponserV   s        rJ   start_crawlrp   Z   s      %W-G[[g66Hs"h6==?Dxx	DHHUODD
DHHW&>?
@@ 7s   !B,B*BB,request_timeoutjob_idpagination_configrr   c          	        K   | j                  d| |       d{   }|j                  dk\  rt        |d       |j                         }t	        |      }|d   }|r|j
                  nd}|r |d   rt        | |d   |||	       d{   }t        |d
   |d   |d   |d   |d   |s|d   |      S d|      S 7 7 4w)a[  
    Get the status of a crawl job.
    
    Args:
        client: Async HTTP client instance
        job_id: ID of the crawl job
        pagination_config: Optional configuration for pagination limits
        request_timeout: Timeout (in seconds) for each individual HTTP request. When auto-pagination 
            is enabled (default) and there are multiple pages of results, this timeout applies to 
            each page request separately, not to the entire operation
        
    Returns:
        CrawlJob with job information
        
    Raises:
        Exception: If the status check fails
    
/v2/crawl/timeoutNri   zget crawl statusrD   Tr`   rq   r[   r\   r]   rb   rc   ra   )rd   rl   r   rm   rf   auto_paginate_fetch_all_pages_asyncr	   )	rg   rs   rt   rr   ro   rV   rn   rR   ry   s	            rJ   get_crawl_statusr{   s   s     0 ZZ*VH 5ZOOHs"h(:;==?D*40GI 8I%33dM0FO+
 
	 x +&g^,<($1WV_  8< ' P
s"   C B<A,C 	B>
3C >C next_urlc          
         K   | j                  ||       d{   }|j                  dk\  rt        |d       |j                         }t	        |      }t        |d   |d   |d   |d   |d	   |d
   |d         S 7 aw)a  
    Fetch a single page of crawl results using the provided next URL.

    Args:
        client: Async HTTP client instance
        next_url: Opaque next URL from a prior crawl status response
        request_timeout: Timeout (in seconds) for the HTTP request

    Returns:
        CrawlJob with the page data and next URL (if any)

    Raises:
        Exception: If the request fails or returns an error response
    rw   Nri   zget crawl status pager[   r\   r]   rb   rc   r`   rD   ra   )rd   rl   r   rm   rf   r	   )rg   r|   rr   ro   rV   rn   s         rJ   get_crawl_status_pager~      s     ( ZZ/ZBBHs"h(?@==?D*40Gx +&g^,<(V_V_  Cs   A=A;A"A=initial_documentsc                  K   |j                         }|}d}|r|j                  nd}|r|j                  nd}	|r|j                  nd}
t	        j
                         }|r|||k\  r	 |S |
t	        j
                         |z
  |
kD  r	 |S | j                  ||       d{   }|j                  dk\  r7ddl}|j                  d      }|j                  dd|j                  i       	 |S |j                         }	 t        |      }|d	   D ]%  }|	t        |      |	k\  r n|j                  |       ' |	t        |      |	k\  r	 |S |d
   }|dz  }|r|S 7 # t        $ r Y |S w xY ww)a  
    Fetch all pages of crawl results asynchronously.
    
    Args:
        client: Async HTTP client instance
        next_url: URL for the next page
        initial_documents: Documents from the first page
        pagination_config: Optional configuration for pagination limits
        request_timeout: Optional timeout (in seconds) for the underlying HTTP request
        
    Returns:
        List of all documents from all pages
    r   Nrw   ri   	firecrawlzFailed to fetch next pagerl   )extrarD   r`      )copy	max_pagesmax_resultsmax_wait_timetime	monotonicrd   rl   logging	getLoggerwarningrm   rf   re   lenrQ   )rg   r|   r   rt   rr   rR   current_url
page_countr   r   r   
start_timero   r   logger	page_datapage_payloaddocuments                     rJ   rz   rz      s    * "&&(IKJ 0A!++dI3D#//$K7H%33dM!J
!zY'>H E %DNN,<z,I]+ZB =  KII3&&&{3FNN6}hNbNb>cNd. + MMO		7	BL
 %V, 	'H'c)n.KX&		' ##i.K*G  #6*a
K N = J  	" %	s>   BE$EAE$8E AE$E$	E!E$ E!!E$c                    K   | j                  d|        d{   }|j                  dk\  rt        |d       |j                         }|j	                  d      dk(  S 7 Cw)a	  
    Cancel a crawl job.
    
    Args:
        client: Async HTTP client instance
        job_id: ID of the crawl job
        
    Returns:
        True if cancellation was successful
        
    Raises:
        Exception: If the cancellation operation fails
    rv   Nri   zcancel crawlr[   	cancelled)deleterl   r   rm   rd   )rg   rs   ro   rV   s       rJ   cancel_crawlr     s\      ]]Zx#899Hs"h7==?D88H,,	 :s   A AAA c                   K   |j                   r|j                   j                         st        d      |j                  r|j                  j                         st        d      |j                   |j                  d}| j	                  d|       d{   }|j
                  dk\  rt        |d       |j                         }|j                  d      st        |j                  d	d
            |j                  di       }i }ddddddddddddd}|j                         D ]  \  }}	||v s||   ||	<    d|v r
|d   }
|
|d<   d|v r|d   |d<   t        di |S 7 ȭw)au  
    Preview crawl parameters before starting a crawl job.
    
    Args:
        client: Async HTTP client instance
        request: CrawlParamsRequest containing URL and prompt
        
    Returns:
        CrawlParamsData containing crawl configuration
        
    Raises:
        ValueError: If request is invalid
        Exception: If the parameter preview fails
    r   zPrompt cannot be empty)r   r   z/v2/crawl/params-previewNri   zcrawl params previewrX   rY   rZ   rD   r/   r0   r1   r%   r2   r3   r4   r5   r6   r7   r!   r9   )r"   r#   r$   r%   r&   r'   r(   r)   r*   r,   r   r.   r   r   rO   )r   r;   r<   r   rk   rl   r   rm   rd   re   rA   r   )rg   r   rn   ro   rV   params_data	convertedmappingrI   rH   wks              rJ   crawl_params_previewr   ,  ss     ;;gkk//1.//>>!5!5!7122kkW^^<G[[!;WEEHs"h(>?==?D88I*BCDD((62&K "I''2!:"<24-+)2G   2uK*51Ie2 K#!	)D#I	)'Y''= Fs   BEEBE-0Ecrawl_idc                 B  K   | j                  d| d       d{   }|j                  dk\  rt        |d       |j                         }|j                  d|      }|j                  dg       |j                  d|j                  d	g             d
}t	        di |S 7 w)a"  
    Get errors from a crawl job.
    
    Args:
        client: Async HTTP client instance
        crawl_id: ID of the crawl job
        
    Returns:
        CrawlErrorsResponse with errors and robots blocked
        
    Raises:
        Exception: If the error check operation fails
    rv   z/errorsNri   zcheck crawl errorsrD   errorsrobotsBlockedrobots_blocked)r   r   rO   )rd   rl   r   rm   r   )rg   r   ro   rV   rn   rT   s         rJ   get_crawl_errorsr   a  s      ZZ*XJg >??Hs"h(<===?Dhhvt$G++h+!++ow{{CSUW7XYJ ,,, @s   BBBBc                 ^  K   | j                  d       d{   }|j                  dk\  rt        |d       |j                         }|j                  d      st	        |j                  dd            |j                  dg       }g }|D ]u  }t        |t              s|j                  |j                  d	      |j                  d
|j                  d            |j                  d      |j                  d      d       w t        d|D cg c]  }t        di | c}      S 7 c c}w w)z
    Get active crawl jobs.
    
    Args:
        client: Async HTTP client instance
        
    Returns:
        ActiveCrawlsResponse with active crawl jobs
        
    Raises:
        Exception: If the active crawl jobs operation fails
    z/v2/crawl/activeNri   zget active crawlsrX   rY   rZ   crawlsrj   teamIdteam_idr   options)rj   r   r   r   T)rX   r   rO   )
rd   rl   r   rm   re   r=   rP   rQ   r   r   )rg   ro   rV   	crawls_inrT   cncs          rJ   get_active_crawlsr   {  s	     ZZ 233Hs"h(;<==?D88I*BCDD2&IJ aeeDk55155+;<uuU|55+	   R\5]Bk6GB6G5]^^! 4  6^s)   D-D%BD-A.D-D(
	D-(D-)N))typingr   r   r   r   typesr   r	   r
   r   r   r   r   r   r   r   r   utils.error_handlerr   utils.validationr   utils.http_client_asyncr   utils.normalizer   r   rP   rK   rU   r>   rf   rp   floatr{   r~   rz   boolr   r   r   r   rO   rL   rJ   <module>r      s   , ,    9 6 6 7 )L )T )XhtCy&9 d8n tCH~ $sCx. Ao A A A8 593
 (,333   013
 e_3 3t (,	!!! e_	!
 !P 59	G (,GGG H~G   01	G e_G 
(^GT- - - -*2( 2(AS 2(Xg 2(j-? -c -FY -4_O _8L _rL   